news.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if html is not None:
  46. a.download(html=html)
  47. else:
  48. a.download()
  49. a.parse()
  50. a.nlp()
  51. except Exception as e:
  52. print("ERROR parsing/downloading/nlp article")
  53. print(e)
  54. print(url)
  55. print(sourcename)
  56. if html is not None:
  57. print(len(html))
  58. self.ERROR = True
  59. self.MATCHES = False
  60. return
  61. a.authors = self.dedup([self.fix_author(
  62. value) for value in a.authors if not self.isComment(value) and not self.isNoise(value)])
  63. self.SCRAP_DATE = datetime.datetime.now()
  64. self.AUTHORS = a.authors
  65. self.PUBLISH_DATE = a.publish_date
  66. self.SUMMARY = a.summary
  67. self.TEXT = a.text
  68. self.TITLE = self.fix_title(a.title)
  69. self.HTML = htmlmin.minify(a.article_html, remove_empty_space=True)
  70. # if html is None:
  71. # self.HTML=htmlmin.minify(a.article_html,remove_empty_space=True)
  72. # else:
  73. # self.HTML=htmlmin.minify(html,remove_empty_space=True)
  74. self.ERROR = False
  75. lowertext = self.TEXT.lower()
  76. ret = match_keywords(kw, lowertext)
  77. self.MATCHES = ret["matches"]
  78. self.WEIGHT_SUM = ret["weight"]
  79. self.MATCHING_KW = ret["kw"]
  80. def get(self):
  81. return {
  82. "url": self.URL,
  83. "lowerurl": self.LOWERURL,
  84. "source": self.SOURCE,
  85. "source_name": self.SOURCENAME,
  86. "authors": self.AUTHORS,
  87. "scrap_date": self.SCRAP_DATE,
  88. "publish_date": self.PUBLISH_DATE,
  89. "summary": self.SUMMARY,
  90. "text": self.TEXT,
  91. "matches": self.MATCHES,
  92. "weight": self.WEIGHT_SUM,
  93. "kw": self.MATCHING_KW,
  94. "html": self.HTML,
  95. "error": self.ERROR,
  96. "deleted": False,
  97. "category": self.CATEGORY,
  98. "title": self.TITLE
  99. }
  100. def fix_title(self, n):
  101. while(html.unescape(n) != n):
  102. n = html.unescape(n)
  103. n = n.replace("&Dquot", '"')
  104. return n
  105. def dedup(self, val):
  106. return list(set(val))
  107. def isNoise(self, v):
  108. if self.r_noise.match(v):
  109. return True
  110. return False
  111. def isComment(self, v):
  112. if self.r.match(v):
  113. return True
  114. return False
  115. def fix_author(self, a):
  116. return a.replace("_", " ").lower()
  117. def match_keywords(keywords, lowertext):
  118. w_sum = 0
  119. matching = []
  120. matches = False
  121. for k in keywords:
  122. num_matches = len([1 for kwp in k["pattern"] if kwp.search(lowertext)])
  123. if num_matches == len(k["pattern"]):
  124. w_sum += k["weight"]
  125. matching.append(k["kw"])
  126. matches = True
  127. ret = ({"matches": matches, "weight": w_sum, "kw": matching})
  128. return ret