conf_task.py 8.4 KB

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