macro.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. data = re.sub("\[br\]", '<br>', data)
  34. while 1:
  35. com = re.compile("\[(youtube|kakaotv)\(([^, )]*)(,[^)]*)?\)\]")
  36. m = com.search(data)
  37. if m:
  38. src = ''
  39. width = '560'
  40. height = '315'
  41. time = '0'
  42. result = m.groups()
  43. if result[1]:
  44. yudt = re.search('(?:\?v=(.*)|\/([^/?]*)|^([a-zA-Z0-9\-_]*))$', result[1])
  45. if yudt:
  46. if yudt.groups()[0]:
  47. src = yudt.groups()[0]
  48. elif yudt.groups()[1]:
  49. src = yudt.groups()[1]
  50. elif yudt.groups()[2]:
  51. src = yudt.groups()[2]
  52. else:
  53. src = ''
  54. if result[2]:
  55. mdata = re.search('width=([0-9%]*)', result[2])
  56. if mdata:
  57. width = mdata.groups()[0]
  58. mdata = re.search('height=([0-9%]*)', result[2])
  59. if mdata:
  60. height = mdata.groups()[0]
  61. mdata = re.search('start=([0-9]*)', result[2])
  62. if mdata:
  63. time = mdata.groups()[0]
  64. if result[0] == 'youtube':
  65. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://www.youtube.com/embed/' + src + '?start=' + time + '" frameborder="0" allowfullscreen></iframe><br>', data, 1)
  66. else:
  67. 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)
  68. else:
  69. break
  70. now_time = get_time()
  71. data = re.sub('\[date\]', now_time, data)
  72. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  73. time = time_data.groups()
  74. age_data = re.findall('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  75. for age in age_data:
  76. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  77. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  78. e_data = old - will
  79. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(int(int(e_data.days) / 365)), data, 1)
  80. dday_data = re.findall('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  81. for dday in dday_data:
  82. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  83. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  84. e_data = old - will
  85. if re.search('^-', str(e_data.days)):
  86. e_day = str(e_data.days)
  87. else:
  88. e_day = '+' + str(e_data.days)
  89. data = re.sub('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', e_day, data, 1)
  90. return data