macro.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from flask import session, request
  2. from urllib import parse
  3. import time
  4. import datetime
  5. import re
  6. import json
  7. def get_time():
  8. now = time.localtime()
  9. date = "%04d-%02d-%02d %02d:%02d:%02d" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
  10. return date
  11. def ip_check():
  12. if session and ('Now' and 'DREAMER') in session and session['Now'] == 1:
  13. ip = session['DREAMER']
  14. else:
  15. try:
  16. ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
  17. except:
  18. ip = 'None'
  19. return str(ip)
  20. def savemark(data):
  21. data = re.sub("\[date\(now\)\]", get_time(), data)
  22. if not re.search("\.", ip_check()):
  23. name = '[[사용자:' + ip_check() + '|' + ip_check() + ']]'
  24. else:
  25. name = ip_check()
  26. data = re.sub("\[name\]", name, data)
  27. return data
  28. def macro(data):
  29. data = savemark(data)
  30. data = re.sub("\[anchor\((?P<in>[^\[\]]*)\)\]", '<span id="\g<in>"></span>', data)
  31. data = re.sub("\[nicovideo\((?P<in>[^,)]*)(?:(?:,(?:[^,)]*))+)?\)\]", "[[http://embed.nicovideo.jp/watch/\g<in>]]", data)
  32. data = re.sub('\[ruby\((?P<in>[^\,]*)\,\s?(?P<out>[^\)]*)\)\]', '<ruby>\g<in><rp>(</rp><rt>\g<out></rt><rp>)</rp></ruby>', data)
  33. while 1:
  34. com = re.compile("\[(youtube|kakaotv)\(([^, )]*)(,[^)]*)?\)\]")
  35. m = com.search(data)
  36. if m:
  37. src = ''
  38. width = '560'
  39. height = '315'
  40. time = '0'
  41. result = m.groups()
  42. if result[1]:
  43. yudt = re.search('(?:\?v=(.*)|\/([^/?]*)|^([a-zA-Z0-9\-_]*))$', result[1])
  44. if yudt:
  45. if yudt.groups()[0]:
  46. src = yudt.groups()[0]
  47. elif yudt.groups()[1]:
  48. src = yudt.groups()[1]
  49. elif yudt.groups()[2]:
  50. src = yudt.groups()[2]
  51. else:
  52. src = ''
  53. if result[2]:
  54. mdata = re.search('width=([0-9%]*)', result[2])
  55. if mdata:
  56. width = mdata.groups()[0]
  57. mdata = re.search('height=([0-9%]*)', result[2])
  58. if mdata:
  59. height = mdata.groups()[0]
  60. mdata = re.search('start=([0-9]*)', result[2])
  61. if mdata:
  62. time = mdata.groups()[0]
  63. if result[0] == 'youtube':
  64. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://www.youtube.com/embed/' + src + '?start=' + time + '" frameborder="0" allowfullscreen></iframe><br>', data, 1)
  65. else:
  66. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://tv.kakao.com/embed/player/cliplink/' + src + '?service=kakao_tv&start=' + time + '" allowfullscreen frameborder="0" scrolling="no"></iframe><br>', data, 1)
  67. else:
  68. break
  69. now_time = get_time()
  70. data = re.sub('\[date\]', now_time, data)
  71. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  72. time = time_data.groups()
  73. age_data = re.findall('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  74. for age in age_data:
  75. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  76. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  77. e_data = old - will
  78. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(int(int(e_data.days) / 365)), data, 1)
  79. dday_data = re.findall('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  80. for dday in dday_data:
  81. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  82. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  83. e_data = old - will
  84. if re.search('^-', str(e_data.days)):
  85. e_day = str(e_data.days)
  86. else:
  87. e_day = '+' + str(e_data.days)
  88. data = re.sub('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', e_day, data, 1)
  89. return data