parser.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/python3
  2. import sys
  3. import re
  4. import newspaper
  5. import pymongo
  6. import htmlmin
  7. import feedparser
  8. import datetime
  9. from frontpage import FrontPage
  10. from newspaper import Article
  11. from db import db
  12. from source import Source
  13. class LinkList():
  14. def __init__(self,url,selector=None,rss=False):
  15. self.links=[]
  16. if rss:
  17. feed = feedparser.parse(url)
  18. #print(feed["bozo"]) FIXME: If bozo==1 => error
  19. self.links=[i["link"] for i in feed["items"]]
  20. print(self.links)
  21. else:
  22. if selector is None:
  23. #s=newspaper.build(url,language="en",memoize_articles=False) #memoize puede salvarme la vida
  24. s = newspaper.Source(url,memoize_articles=False)
  25. s.download()
  26. s.parse()
  27. s.set_categories()
  28. s.download_categories()
  29. s.parse_categories()
  30. s.generate_articles()
  31. self.links=list(set([a.url for a in s.articles])) #avoid dupes
  32. else:
  33. f=FrontPage(url,selector)
  34. self.links=f.links
  35. self.links=self.purgeLinks(self.links)
  36. def purgeLinks(self,l):
  37. return [ sanitizeUrl(link) for link in l if not "presslist" in link.lower() and not "videolist" in link.lower() ]
  38. class News():
  39. r=re.compile(r"hours? ago|yesterday|today|last week|month",flags=re.IGNORECASE)
  40. r_noise=re.compile(r"posted|Product|Google",flags=re.IGNORECASE)
  41. URL=""
  42. AUTHORS=[]
  43. SCRAP_DATE=None
  44. PUBLISH_DATE=None
  45. SUMMARY=""
  46. TEXT=""
  47. HTML=""
  48. ERROR=False
  49. def __init__(self,url,lang="en"):
  50. url=sanitizeUrl(url)
  51. self.URL=url
  52. a = Article(url, language=lang,keep_article_html=True)
  53. a.download()
  54. a.parse()
  55. a.nlp()
  56. a.authors=self.dedup([self.fix_author(value) for value in a.authors if not self.isComment(value) and not self.isNoise(value)])
  57. self.SCRAP_DATE=datetime.datetime.now()
  58. self.AUTHORS=a.authors
  59. self.PUBLISH_DATE=a.publish_date
  60. self.SUMMARY=a.summary
  61. self.TEXT=a.text
  62. self.HTML=htmlmin.minify(a.article_html,remove_empty_space=True)
  63. def get(self):
  64. return {
  65. "authors": self.AUTHORS,
  66. "scrap_date": self.SCRAP_DATE,
  67. "publish_date": self.PUBLISH_DATE,
  68. "summary": self.SUMMARY,
  69. "text": self.TEXT,
  70. "html": self.HTML
  71. }
  72. def dedup(self,val):
  73. return list(set(val))
  74. def isNoise(self,v):
  75. if self.r_noise.match(v):
  76. return True
  77. return False
  78. def isComment(self,v):
  79. if self.r.match(v):
  80. return True
  81. return False
  82. def fix_author(self,a):
  83. return a.replace("_", " ").lower()
  84. unsharer=re.compile(r"\?share=.+$",flags=re.IGNORECASE)
  85. def sanitizeUrl(url):
  86. global unsharer
  87. return re.sub(unsharer, "",url)
  88. def sanitizeSelector(s):
  89. s=s.replace(">a","> a")
  90. if re.match(r'=\w+\]',s) is None:
  91. return s
  92. ret=re.sub(r"=(\w+)]", r'="\1"]', s)
  93. return ret
  94. d=db()
  95. for s in d.sources():
  96. print(s["name"])
  97. sel=None
  98. rss=False
  99. if "selector" in s:
  100. sel=s["selector"]
  101. if "rss" in s:
  102. rss=bool(["rss"])
  103. l=LinkList(s["link"],sel,rss=rss)
  104. if l is not None:
  105. for a in l.links:
  106. print(a)
  107. print(len(l.links))
  108. n=News(l.links[0])
  109. print(n.get())
  110. break