2
0

macro.py 4.0 KB

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