conf_task.py 8.1 KB

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