conf_task.py 9.0 KB

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