parser.py 5.2 KB

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