conf_task.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import os
  2. import sys
  3. import time
  4. import requests
  5. from flask import Flask, request
  6. from flask import jsonify
  7. sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "..")))
  8. from common.db.mysql_help import MysqlHelper
  9. from user_spider.user_info import *
  10. app = Flask(__name__)
  11. app.config['JSON_AS_ASCII'] = False
  12. # 只接受get方法访问
  13. @app.route("/v1/crawler/source/getall", methods=["GET"])
  14. def getSource():
  15. # 获取传入的params参数
  16. get_data = request.args.to_dict()
  17. fields = get_data.get('fields')
  18. # # 对参数进行操作
  19. sql = 'select source, task_type, spider_name, machine, source_desc, task_type_desc, spider_name_desc from crawler_source'
  20. result = MysqlHelper.get_values(sql)
  21. if not result:
  22. return jsonify({'code': '200', 'result': [], 'message': '没有更多数据'})
  23. source_list = list()
  24. for source, task_type, spider_name, machine, source_desc, task_type_desc, spider_name_desc in result:
  25. source_dict = {
  26. 'task_type': [
  27. {
  28. 'description': task_type_desc,
  29. 'author': [
  30. {
  31. 'spider_name': spider_name,
  32. 'description': spider_name_desc
  33. }
  34. ]
  35. }
  36. ],
  37. 'description': source_desc,
  38. 'source': source
  39. }
  40. source_list.append(source_dict)
  41. return jsonify({'code': '200', 'result': source_list})
  42. @app.route("/v1/crawler/task/insert", methods=["POST"])
  43. def insertTask():
  44. try:
  45. data = request.form
  46. outer_info = data.get(('spider_link'))
  47. source = data.get('source')
  48. exist_outer_info = list()
  49. for link in outer_info:
  50. s_sql = f"""select spider_link from crawler_task where source={source}"""
  51. result = MysqlHelper.get_values(s_sql)
  52. if link in eval(result[0]):
  53. exist_outer_info.append(link)
  54. if exist_outer_info:
  55. return jsonify({'code': 200, 'message': '名单重复', 'repeat_list': exist_outer_info})
  56. # 获取到一个以键且为逗号分隔的字符串,返回一个字符串
  57. keys = ','.join(data.keys())
  58. values = ','.join(['%s'] * len(data))
  59. sql = 'insert into {table}({keys}) VALUES({values})'.format(table='crawler_task', keys=keys, values=values)
  60. MysqlHelper.insert_values(sql, tuple(data.values()))
  61. except Exception as e:
  62. return jsonify({'code': 400, 'message': '任务写入失败'})
  63. return jsonify({'code': 200, 'message': 'task create success'})
  64. @app.route("/v1/crawler/task/gettask", methods=["GET"])
  65. def getAllTask():
  66. try:
  67. get_data = request.args.to_dict()
  68. page = int(get_data.get('page', 1))
  69. offset = int(get_data.get('offset', 10))
  70. start_count = (page * offset) - offset
  71. end_count = page * offset
  72. sql = f"""select task_id, task_name from crawler_task limit {start_count}, {end_count}"""
  73. result = MysqlHelper.get_values(sql)
  74. if not result:
  75. return jsonify({'code': '200', 'result': [], 'message': 'no data'})
  76. source_list = list()
  77. for task_id, task_name in result:
  78. data = dict(
  79. task_id=task_id,
  80. task_name=task_name,
  81. )
  82. source_list.append(data)
  83. except Exception as e:
  84. return jsonify({"code": "400", 'message': "任务列表获取失败"})
  85. return jsonify({'code': '200', 'result': source_list})
  86. @app.route("/v1/crawler/task/getone", methods=["GET"])
  87. def getOneTask():
  88. try:
  89. get_data = request.args.to_dict()
  90. task_id = get_data['task_id']
  91. sql = f'select task_id, spider_link from crawler_task where task_id={task_id}'
  92. result = MysqlHelper.get_values(sql)
  93. if not result:
  94. return jsonify({'code': '200', 'result': [], 'message': 'no data'})
  95. for task_id, spider_link in result:
  96. data = dict(
  97. task_id=task_id,
  98. spider_link=spider_link,
  99. )
  100. except Exception as e:
  101. return jsonify({'code': '400', "message": "获取任务信息失败"})
  102. return jsonify({'code': '200', 'result': data})
  103. @app.route("/v1/crawler/task/update", methods=["POST"])
  104. def updateTask():
  105. try:
  106. task_id = request.form.get('task_id')
  107. spider_link = request.form.get('spider_link')
  108. print(spider_link, task_id)
  109. sql = f"""UPDATE crawler_task SET spider_link='{spider_link}' where task_id = {task_id}"""
  110. print(sql)
  111. result = MysqlHelper.update_values(sql)
  112. if result:
  113. return jsonify({'code': 200, 'message': 'task update success'})
  114. else:
  115. return jsonify({'code': 400, 'message': 'task update faild'})
  116. except Exception as e:
  117. return jsonify({'code': 400, 'message': '任务更新失败'})
  118. def get_user_info(source):
  119. source_spider = {
  120. 'xigua': xigua_user_info
  121. }
  122. return source_spider.get(source)
  123. @app.route("/v1/crawler/author/create", methods=["POST"])
  124. def createUser():
  125. get_media_url = 'http://videotest-internal.yishihui.com/longvideoapi/user/virtual/crawler/registerVirtualUser'
  126. print(request.form.to_dict())
  127. spider_link = request.form.get('spider_link')
  128. print(111111, spider_link, type(spider_link))
  129. source = request.form.get('source')
  130. task_type = request.form.get('task_type')
  131. applets_status = request.form.get('applets_status')
  132. app_status = request.form.get('app_status')
  133. user_tag = request.form.get('user_tag')
  134. user_content_tag = request.form.get('user_content_tag')
  135. success_list = list()
  136. fail_list = list()
  137. for author_url in eval(spider_link):
  138. try:
  139. post_data = {
  140. # 'count': 1, # (必须)账号个数:传1
  141. # 'accountType': 4, # (必须)账号类型 :传 4 app虚拟账号
  142. 'pwd': '', # 密码 默认 12346
  143. 'nickName': '', # 昵称 默认 vuser......
  144. 'avatarUrl': '',
  145. # 头像Url 默认 http://weapppiccdn.yishihui.com/resources/images/pic_normal.png
  146. 'tagName': user_tag, # 多条数据用英文逗号分割
  147. }
  148. response = requests.post(url=get_media_url, params=post_data)
  149. media_id = response.json()['data']
  150. f_sql = f"""select spider_link from crawler_author_map where spider_link="{author_url}" """
  151. result = MysqlHelper.get_values(f_sql)
  152. if result:
  153. success_list.append(author_url)
  154. continue
  155. else:
  156. data = dict(
  157. spider_link=author_url,
  158. media_id=media_id,
  159. source=source,
  160. task_type=task_type,
  161. applets_status=applets_status,
  162. app_status=app_status,
  163. user_tag=user_tag,
  164. user_content_tag=user_content_tag,
  165. insert_time=int(time.time()),
  166. update_time=int(time.time())
  167. )
  168. keys = ','.join(data.keys())
  169. values = ','.join(['%s'] * len(data))
  170. table = 'crawler_author_map'
  171. sql = f"""insert into {table}({keys}) VALUES({values})"""
  172. result = MysqlHelper.insert_values(sql, tuple(data.values()))
  173. if not result:
  174. fail_list.append(author_url)
  175. else:
  176. success_list.append(author_url)
  177. except Exception as e:
  178. fail_list.append(author_url)
  179. continue
  180. return jsonify({'code': 200, 'result': {'success': success_list, 'fail': fail_list}})
  181. if __name__ == "__main__":
  182. app.run(debug=True, port=5050)