strategy.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. from tqdm import tqdm
  6. from applications.functions import Functions
  7. from config import accountBaseInfo, pool_level_detail
  8. class ArticlePoolStrategy(object):
  9. """
  10. 长文策略池
  11. """
  12. Fun = Functions()
  13. @classmethod
  14. def getData(cls, article_list):
  15. """
  16. :param article_list: 每天召回的文章list
  17. :return: {
  18. "Level1": [],
  19. "Level2": [],
  20. "Level3": []
  21. }
  22. """
  23. detail_list = []
  24. print("查询文章url......")
  25. for i in tqdm(article_list):
  26. detail = cls.Fun.matchLinkById(i['article_id'])
  27. i['gh_id'], i['url'], i['index'] = detail
  28. detail_list.append(i)
  29. print("查询完成, 开始排序")
  30. return detail_list
  31. @classmethod
  32. def splitByStrategy(cls, detail_list):
  33. """
  34. 账号-位置-阅读倍数
  35. :return:
  36. """
  37. L = []
  38. for line in detail_list:
  39. key = "{}_{}".format(line['gh_id'], line['index'])
  40. article_read = line['increase_read_count']
  41. if accountBaseInfo.get(key):
  42. avg_read = accountBaseInfo[key]['readAvg']
  43. # 计算比率
  44. level_rate = article_read / avg_read - 1
  45. obj = {
  46. "key": key,
  47. "avg_read": avg_read,
  48. "article_read": article_read,
  49. "level_rate": level_rate,
  50. "url": line['url']
  51. }
  52. L.append(obj)
  53. L = sorted(L, key=lambda x: x["level_rate"], reverse=True)
  54. for index, i in enumerate(L):
  55. print(index,"\t", i['key'], "\t", i['level_rate'])
  56. result = {
  57. "Level1": [],
  58. "Level2": []
  59. }
  60. c = 0
  61. for line in L:
  62. print(json.dumps(line, ensure_ascii=False, indent=4))
  63. if line['level_rate'] > 0.2:
  64. c += 1
  65. gh_key = line['key']
  66. if pool_level_detail.get(gh_key):
  67. now_level = pool_level_detail[gh_key]
  68. if now_level == "3":
  69. result['Level2'].append(line['url'])
  70. elif now_level == "2":
  71. result['Level1'].append(line['url'])
  72. else:
  73. continue
  74. else:
  75. result['Level2'].append(line['url'])
  76. print(c)
  77. return result