parser.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/python3
  2. import sys
  3. import re
  4. import newspaper
  5. import pymongo
  6. import htmlmin
  7. import feedparser
  8. from frontpage import FrontPage
  9. from newspaper import Article
  10. from db import db
  11. from source import Source
  12. class LinkList():
  13. def __init__(self,url,selector=None,rss=False):
  14. self.links=[]
  15. if rss:
  16. feed = feedparser.parse(url)
  17. #print(feed["bozo"]) FIXME: If bozo==1 => error
  18. self.links=[i["link"] for i in feed["items"]]
  19. else:
  20. if selector is None:
  21. #s=newspaper.build(url,language="en",memoize_articles=False) #memoize puede salvarme la vida
  22. s = newspaper.Source(url,memoize_articles=False)
  23. s.download()
  24. s.parse()
  25. s.set_categories()
  26. s.download_categories()
  27. s.parse_categories()
  28. s.generate_articles()
  29. self.links=[a.url for a in s.articles]
  30. else:
  31. f=FrontPage(url,selector)
  32. self.links=f.links
  33. self.links=self.purgeLinks(self.links)
  34. def purgeLinks(self,l):
  35. return [ link for link in l if not "presslist" in link.lower() and not "videolist" in link.lower() ]
  36. class News():
  37. r=re.compile(r"hours? ago|yesterday|today|last week|month",flags=re.IGNORECASE)
  38. r_noise=re.compile(r"posted|Product|Google",flags=re.IGNORECASE)
  39. def __init__(self,url,lang="en"):
  40. url=self.sanitizeUrl(url)
  41. a = Article(url, language=lang,keep_article_html=True)
  42. a.download()
  43. a.parse()
  44. a.nlp()
  45. a.authors=self.dedup([self.fix_author(value) for value in a.authors if not self.isComment(value) and not self.isNoise(value)])
  46. print(a.authors)
  47. print(a.publish_date)
  48. print(a.summary)
  49. print(a.keywords)
  50. #print(a.text) #FIXME
  51. print(a.top_image)
  52. print(a.movies)
  53. print(htmlmin.minify(a.article_html,remove_empty_space=True))
  54. def sanitizeUrl(self,url):
  55. return url.replace("?share=google-plus-1","")
  56. def dedup(self,val):
  57. return list(set(val))
  58. def isNoise(self,v):
  59. if self.r_noise.match(v):
  60. return True
  61. return False
  62. def isComment(self,v):
  63. if self.r.match(v):
  64. return True
  65. return False
  66. def fix_author(self,a):
  67. return a.replace("_", " ").lower()
  68. l=None
  69. #d=db()
  70. #l=LinkList("http://9to5google.com")
  71. #l=LinkList("http://www.alibabagroup.com/en/news/press")
  72. #l=LinkList("http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-news", selector=".ccbnTblLnk")
  73. #l=LinkList("http://about.americanexpress.com/news/pr/releases.aspx")
  74. #NOT WORKING l=LinkList("http://www.americanbanker.com/")
  75. #l=LinkList("http://officialandroid.blogspot.com/atom.xml",rss=True)
  76. #l=LinkList("https://www.apple.com/pr/feeds/pr.rss",rss=True)
  77. #l=LinkList("http://appleinsider.com/rss/news/",rss=True)
  78. #l=LinkList("http://newsroom.bankofamerica.com/feeds/press_release/all/rss.xml",rss=True)
  79. #l=LinkList("https://newsroom.fb.com/feed/",rss=True)
  80. #n=News("http://appleinsider.com/articles/16/05/11/apple-again-rumored-to-turn-la-landmark-into-retail-store")
  81. #n=News("http://9to5google.com/2016/05/11/nest-opensource-openthread-networking-protocol/?share=google-plus-1")
  82. #n=News("http://www.alibabagroup.com/en/news/article?news=p150908b")
  83. #n=News("http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-newsArticle&ID=1250170")
  84. #n=News("http://about.americanexpress.com/news/pr/2016/amex-dividend-payable-august-10-2016.aspx")
  85. #n=News("http://www.apple.com/pr/library/2015/06/08Apple-Pay-Giving-Shoppers-Even-More-Ways-to-Pay.html")
  86. #n=News("http://newsroom.bankofamerica.com/press-releases/small-business-banking/bank-america-survey-finds-small-business-owners-confidence-dow")
  87. #n=News("http://newsroom.fb.com/news/2016/04/news-feed-fyi-more-articles-you-want-to-spend-time-viewing/")
  88. #l=LinkList("https://www.blogger.com/feeds/10861780/posts/default",rss=True)
  89. #n=News("http://googleblog.blogspot.com/2016/05/translate-where-you-need-it-in-any-app.html") #FIXME
  90. #l=LinkList("https://apps.shareholder.com/rss/rss.aspx?channels=14&companyid=ONE",rss=True)
  91. #n=News("http://investor.shareholder.com/jpmorganchase/press/releasedetail.cfm?ReleaseID=970074")
  92. #l=LinkList("http://www.samsungmobilepress.com/press/pressList.asp")
  93. #n=News("http://www.samsungmobilepress.com/press/Samsungs-Connected-Ecosystem-Accelerates-the-Growth-of-the-Gear-S2-Applications?2016-03-22")
  94. #l=LinkList("https://news.starbucks.com/feeds/news")
  95. #l=LinkList("http://www.techinsider.io/rss",rss=True)
  96. l=LinkList("https://newsroom.uber.com/feed/", rss=True)
  97. if l is not None:
  98. for a in l.links:
  99. print(a)
  100. print(len(l.links))
  101. n=News(l.links[0])
  102. sys.exit(1)
  103. def sanitizeSelector(s):
  104. s=s.replace(">a","> a")
  105. if re.match(r'=\w+\]',s) is None:
  106. return s
  107. print(s)
  108. ret=re.sub(r"=(\w+)]", r'="\1"]', s)
  109. print(ret)
  110. return ret
  111. for s in d.sources():
  112. s["title"]=sanitizeSelector(s["title"])
  113. l=LinkList(s["link"],s["title"])
  114. if len(l.links)==0:
  115. print(s["link"])
  116. print(s["title"])
  117. print("###################################")
  118. #n=News("http://www.bloomberg.com/news/articles/2016-03-04/the-mystery-madoff-victims-who-left-2-5-billion-on-the-table")