2
0

api_topic.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from .tool.func import *
  2. from .bbs_w_post import bbs_w_post_make_thread
  3. def api_topic(topic_num = 1, tool = 'normal', num = '', render = ''):
  4. with get_db_connect() as conn:
  5. curs = conn.cursor()
  6. topic_num = str(topic_num)
  7. if acl_check('', 'topic_view', topic_num) != 1:
  8. if tool == 'normal':
  9. if num != '':
  10. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? and id + 0 = ? + 0 order by id + 0 asc"), [topic_num, num])
  11. else:
  12. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? order by id + 0 asc"), [topic_num])
  13. elif tool == 'top':
  14. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? and top = 'O' order by id + 0 asc"), [topic_num])
  15. else:
  16. # tool == 'length'
  17. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [topic_num])
  18. db_data = curs.fetchall()
  19. if db_data:
  20. return flask.jsonify({ 'length' : db_data[0][0] })
  21. else:
  22. return flask.jsonify({})
  23. data = curs.fetchall()
  24. if data:
  25. data_a = {}
  26. admin = admin_check(3)
  27. curs.execute(db_change("select ip from topic where code = ? order by id + 0 asc limit 1"), [topic_num])
  28. data_f = curs.fetchall()
  29. data_f = data_f[0][0] if data_f else ''
  30. data_a['data_main'] = {
  31. "ip_first" : ip_pas(data_f, 1),
  32. "admin" : str(admin)
  33. }
  34. data_a['data'] = []
  35. ip_a = ip_pas([i[3] for i in data])
  36. ip_a_2 = ip_pas([i[3] for i in data], 1)
  37. for i in data:
  38. data_v = i[1] if i[4] != 'O' or admin == 1 else ''
  39. if data_v != '':
  40. data_v = render_set(
  41. doc_data = data_v,
  42. data_type = 'api_view',
  43. data_in = 'topic_' + topic_num + '_' + i[0],
  44. doc_acl = 0
  45. )
  46. data_v[0] = re.sub(
  47. r'&lt;topic_a&gt;(?P<in>(?:(?!&lt;\/topic_a&gt;).)+)&lt;\/topic_a&gt;',
  48. '<a href="\g<in>">\g<in></a>',
  49. data_v[0]
  50. )
  51. data_v[0] = re.sub(
  52. r'&lt;topic_call&gt;@(?P<in>(?:(?!&lt;\/topic_call&gt;).)+)&lt;\/topic_call&gt;',
  53. '<a href="/w/user:\g<in>">@\g<in></a>',
  54. data_v[0]
  55. )
  56. else:
  57. data_v = ['', '']
  58. data_a['data'] += [{
  59. "id" : i[0],
  60. "data" : data_v,
  61. "date" : i[2],
  62. "ip" : ip_a_2[i[3]],
  63. "blind" : i[4],
  64. "ip_pas" : ip_a[i[3]],
  65. "data_pas" : data_v
  66. }]
  67. if render == '':
  68. return flask.jsonify(data_a)
  69. else:
  70. data_r = ''
  71. if 'data' in data_a:
  72. for for_a in data_a['data']:
  73. if tool == 'top':
  74. color = 'red'
  75. else:
  76. if data_a['data_main']["ip_first"] == for_a["ip"]:
  77. color = 'green'
  78. else:
  79. color = 'default'
  80. data_r += bbs_w_post_make_thread(
  81. for_a["ip_pas"],
  82. '<a href="/thread/' + topic_num + '/comment/' + for_a["id"] + '/tool">(' + load_lang('tool') + ')</a> ' + for_a["date"],
  83. for_a["data_pas"][0] + '<script>' + for_a["data_pas"][1] + '</script>',
  84. for_a["id"],
  85. color = color,
  86. blind = for_a["blind"],
  87. add_style = ''
  88. )
  89. data_r += '<hr class="main_hr">'
  90. return flask.jsonify({ "data" : data_r })
  91. else:
  92. return flask.jsonify({})
  93. else:
  94. return flask.jsonify({})