task2.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. @author: luojunhui
  3. """
  4. from concurrent.futures.thread import ThreadPoolExecutor
  5. from tqdm import tqdm
  6. from applications import AIDTApi, DeNetMysql, PQMySQL, Functions
  7. from config import poolLevelConfig
  8. class ColdStartTask(object):
  9. """
  10. 冷启分配任务
  11. """
  12. AidApi = AIDTApi()
  13. DeMysql = DeNetMysql()
  14. PqMysql = PQMySQL()
  15. Fun = Functions()
  16. pool4 = poolLevelConfig['4']
  17. @classmethod
  18. def getTopArticles(cls, category, limit_count):
  19. """
  20. 获取高分享的文章list
  21. :return:
  22. """
  23. sql = f"""
  24. select content_id, content_link, title
  25. from cold_start_article_pool
  26. where category = '{category}'
  27. order by view_count DESC, publish_time_stamp DESC
  28. limit {limit_count};
  29. """
  30. result = cls.PqMysql.select(sql)
  31. return result
  32. @classmethod
  33. def computeScore(cls):
  34. """
  35. 和每个账号计算相关性分数
  36. :return:
  37. """
  38. # category_list = ["军事政法", "健康养生", "搞笑幽默"]
  39. category_list = ["军事政法", "健康养生"]
  40. L = []
  41. for category in category_list:
  42. article_tuple = cls.getTopArticles(category)
  43. title_list = [article[2] for article in article_tuple]
  44. score_list = cls.Fun.getTitleScore(title_list, "指尖奇文")['指尖奇文']['score_list']
  45. for index, score in enumerate(score_list):
  46. obj = {
  47. "id": article_tuple[index][0],
  48. "url": article_tuple[index][1],
  49. "title": article_tuple[index][2],
  50. "cate": category,
  51. "score": score
  52. }
  53. L.append(obj)
  54. result = [i for i in L if i['score'] >= 0.4]
  55. return result
  56. @classmethod
  57. def sendToColdPool(cls):
  58. """
  59. 把文章send至第四层
  60. :return:
  61. """
  62. result = cls.computeScore()
  63. army = [i for i in result if i['cate'] == '军事政法']
  64. healthy = [i for i in result if i['cate'] == '健康养生']
  65. print(len(army))
  66. print(len(healthy))
  67. cst = ColdStartTask()
  68. res = cst.getTopArticles(category="军事政法", limit_count=20)
  69. print(res)