server3.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. import pickle
  4. from flask import Flask, render_template, send_from_directory
  5. from flask import request, redirect, Response
  6. from flask_cors import *
  7. from multiprocessing import Process, Queue
  8. import configs
  9. from structure.structure_main import WordParseStructure
  10. import os, datetime, hashlib
  11. import time, json, random
  12. import pprint
  13. from utils.ruku_opera_old import Ruku
  14. logger = configs.myLog(__name__).getlog()
  15. #
  16. app = Flask(__name__)
  17. app.debug = True
  18. CORS(app, supports_credentials=True) #
  19. @app.route('/word_structure', methods=["GET", "POST"])
  20. def word_structure():
  21. # logger.info("==request.POST.dict==>{}\n".format(request.form.to_dict()))
  22. mydata = request.json.get("sci_html_data", "")
  23. is_reparse = request.json.get("is_reparse", "0")
  24. word_id = request.json.get("paper_id", 0)
  25. print(mydata)
  26. # time_str = datetime.datetime.strftime(datetime.datetime.now(), '%Y_%m_%d_%H_%M_%S')
  27. # new_fpath = os.path.join(file1, str(time_str)+".html")
  28. # re_f = open(new_fpath, 'w', encoding='utf-8')
  29. # re_f.write(mydata)
  30. # try:
  31. if mydata and word_id:
  32. res, paper_type = WordParseStructure(mydata, str(word_id), int(is_reparse)).structure()
  33. pprint.pprint(res)
  34. return json.dumps(res, ensure_ascii=False)
  35. # except:
  36. # # 先保存文件
  37. # now_time = datetime.datetime.now()
  38. # time_str = datetime.datetime.strftime(now_time, '%Y_%m_%d_%H_%M_%S')
  39. # aft_modify = (str(random.random())).encode("utf-8")
  40. # aft_name = hashlib.md5(aft_modify).hexdigest() + '__' + time_str + '.json'
  41. #
  42. # new_fpath = os.path.join(file1, aft_name)
  43. # re_f = open(new_fpath, 'w', encoding='utf-8')
  44. # json.dump(mydata, re_f)
  45. # # return "解析失败"
  46. # return json.dumps({"items": []}, ensure_ascii=False)
  47. @app.route('/ruku', methods=["GET","POST"])
  48. def ruku():
  49. # is_ruku = request.json.get("is_ruku", 1)
  50. wordid = request.json.get("paper_id", "")
  51. items_list = request.json.get("structured_items", "")
  52. html_data = request.json.get("html_data", "")
  53. # print(wordid)
  54. # pickle.dump(items_list, open("F:/zwj/Text_Structure/img_folder/struct_items.pickle", 'wb'))
  55. # pickle.dump(html_data, open("F:/zwj/Text_Structure/img_folder/html_data.pickle", 'wb'))
  56. if wordid and items_list:
  57. res = ruku_upload_img(items_list, html_data, str(wordid))
  58. print(res)
  59. return json.dumps(res, ensure_ascii=False)
  60. @app.route('/ser_static/<path:file_path>', methods=["GET"])
  61. def ser_static(file_path): # endpoint的位置是函数接口名,不能用static,与flask内部变量重名
  62. """
  63. :param file_path: 图片的本地绝对路径
  64. :return:
  65. """
  66. return send_from_directory(configs.IMG_FOLDER, file_path)
  67. if __name__ == "__main__":
  68. app.run(host=configs.server_ip, port=configs.server_port, threaded=True, debug=True)
  69. # 5fc64a0a4994183dda7e74b9