macro.py 4.1 KB

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