news.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import re
  2. import htmlmin
  3. import datetime
  4. import html
  5. from functions import sanitizeUrl
  6. from newspaper import Article, Config
  7. class News():
  8. r = re.compile(r"hours? ago|yesterday|today|last week|month",
  9. flags=re.IGNORECASE)
  10. r_noise = re.compile(r"posted|Product|Google", flags=re.IGNORECASE)
  11. URL = ""
  12. LOWERURL = ""
  13. AUTHORS = []
  14. SCRAP_DATE = None
  15. PUBLISH_DATE = None
  16. SUMMARY = ""
  17. TEXT = ""
  18. HTML = ""
  19. ERROR = False
  20. SOURCE = ""
  21. SOURCENAME = ""
  22. WEIGHT_SUM = 0
  23. MATCHES = False
  24. MATCHING_KW = []
  25. TITLE = ""
  26. CATEGORY = 0
  27. def __init__(self, url, sourceid, sourcename, sourcecategory, kw, lang="en", html=None):
  28. url = sanitizeUrl(url)
  29. self.URL = url
  30. self.LOWERURL = url.lower()
  31. self.SOURCENAME = sourcename
  32. self.SOURCE = sourceid
  33. self.CATEGORY = sourcecategory
  34. self.MATCHING_KW = []
  35. config = Config()
  36. config.language = lang
  37. config.memoize_articles = False
  38. config.keep_article_html = True
  39. config.request_timeout = 15
  40. config.fetch_images = False
  41. config.browser_user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"
  42. a = None
  43. try:
  44. a = Article(url=url, config=config)
  45. a.download(html=html)
  46. a.parse()
  47. a.nlp()
  48. except Exception as e:
  49. print("ERROR parsing/downloading/nlp article")
  50. print(e)
  51. print(url)
  52. print(sourcename)
  53. if html is not None:
  54. print(len(html))
  55. self.ERROR = True
  56. self.MATCHES = False
  57. return
  58. a.authors = self.dedup([self.fix_author(
  59. value) for value in a.authors if not self.isComment(value) and not self.isNoise(value)])
  60. self.SCRAP_DATE = datetime.datetime.now()
  61. self.AUTHORS = a.authors
  62. self.PUBLISH_DATE = a.publish_date
  63. self.SUMMARY = a.summary
  64. self.TEXT = a.text
  65. self.TITLE = self.fix_title(a.title)
  66. self.HTML = htmlmin.minify(a.article_html, remove_empty_space=True)
  67. # if html is None:
  68. # self.HTML=htmlmin.minify(a.article_html,remove_empty_space=True)
  69. # else:
  70. # self.HTML=htmlmin.minify(html,remove_empty_space=True)
  71. self.ERROR = False
  72. lowertext = self.TEXT.lower()
  73. ret = match_keywords(kw, lowertext)
  74. self.MATCHES = ret["matches"]
  75. self.WEIGHT_SUM = ret["weight"]
  76. self.MATCHING_KW = ret["kw"]
  77. def get(self):
  78. return {
  79. "url": self.URL,
  80. "lowerurl": self.LOWERURL,
  81. "source": self.SOURCE,
  82. "source_name": self.SOURCENAME,
  83. "authors": self.AUTHORS,
  84. "scrap_date": self.SCRAP_DATE,
  85. "publish_date": self.PUBLISH_DATE,
  86. "summary": self.SUMMARY,
  87. "text": self.TEXT,
  88. "matches": self.MATCHES,
  89. "weight": self.WEIGHT_SUM,
  90. "kw": self.MATCHING_KW,
  91. "html": self.HTML,
  92. "error": self.ERROR,
  93. "deleted": False,
  94. "category": self.CATEGORY,
  95. "title": self.TITLE
  96. }
  97. def fix_title(self, n):
  98. while(html.unescape(n) != n):
  99. n = html.unescape(n)
  100. n = n.replace("&Dquot", '"')
  101. return n
  102. def dedup(self, val):
  103. return list(set(val))
  104. def isNoise(self, v):
  105. if self.r_noise.match(v):
  106. return True
  107. return False
  108. def isComment(self, v):
  109. if self.r.match(v):
  110. return True
  111. return False
  112. def fix_author(self, a):
  113. return a.replace("_", " ").lower()
  114. def match_keywords(keywords, lowertext):
  115. w_sum = 0
  116. matching = []
  117. matches = False
  118. for k in keywords:
  119. num_matches = len([1 for kwp in k["pattern"] if kwp.search(lowertext)])
  120. if num_matches == len(k["pattern"]):
  121. w_sum += k["weight"]
  122. matching.append(k["kw"])
  123. matches = True
  124. ret = ({"matches": matches, "weight": w_sum, "kw": matching})
  125. return ret