parser.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. else:
  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. self.links=self.purgeLinks(self.links)
  54. def purgeLinks(self,l):
  55. return [ link for link in l if not "presslist" in link.lower() and not "videolist" in link.lower() ]
  56. class News():
  57. r=re.compile(r"hours? ago|yesterday|today|last week|month",flags=re.IGNORECASE)
  58. r_noise=re.compile(r"posted|Product|Google",flags=re.IGNORECASE)
  59. def __init__(self,url,lang="en"):
  60. url=self.sanitizeUrl(url)
  61. a = Article(url, language=lang,keep_article_html=True)
  62. a.download()
  63. a.parse()
  64. a.nlp()
  65. a.authors=self.dedup([self.fix_author(value) for value in a.authors if not self.isComment(value) and not self.isNoise(value)])
  66. print(a.authors)
  67. print(a.publish_date)
  68. print(a.summary)
  69. print(a.keywords)
  70. #print(a.text)
  71. print(a.top_image)
  72. print(a.movies)
  73. print("_#_#_#_#_#_#_")
  74. print(htmlmin.minify(a.article_html,remove_empty_space=True))
  75. def sanitizeUrl(self,url):
  76. return url.replace("?share=google-plus-1","")
  77. def dedup(self,val):
  78. return list(set(val))
  79. def isNoise(self,v):
  80. if self.r_noise.match(v):
  81. return True
  82. return False
  83. def isComment(self,v):
  84. if self.r.match(v):
  85. return True
  86. return False
  87. def fix_author(self,a):
  88. return a.replace("_", " ").lower()
  89. l=None
  90. #d=db()
  91. #l=LinkList("http://9to5google.com")
  92. #l=LinkList("http://www.alibabagroup.com/en/news/press")
  93. #l=LinkList("http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-news", selector=".ccbnTblLnk")
  94. #l=LinkList("http://about.americanexpress.com/news/pr/releases.aspx")
  95. #NOT WORKING l=LinkList("http://www.americanbanker.com/")
  96. #l=LinkList("http://officialandroid.blogspot.com/atom.xml",rss=True)
  97. #l=LinkList("https://www.apple.com/pr/feeds/pr.rss",rss=True)
  98. #l=LinkList("http://appleinsider.com/rss/news/",rss=True)
  99. #l=LinkList("http://newsroom.bankofamerica.com/feeds/press_release/all/rss.xml",rss=True)
  100. #l=LinkList("https://newsroom.fb.com/feed/",rss=True)
  101. #n=News("http://appleinsider.com/articles/16/05/11/apple-again-rumored-to-turn-la-landmark-into-retail-store")
  102. #n=News("http://9to5google.com/2016/05/11/nest-opensource-openthread-networking-protocol/?share=google-plus-1")
  103. #n=News("http://www.alibabagroup.com/en/news/article?news=p150908b")
  104. #n=News("http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-newsArticle&ID=1250170")
  105. #n=News("http://about.americanexpress.com/news/pr/2016/amex-dividend-payable-august-10-2016.aspx")
  106. #n=News("http://www.apple.com/pr/library/2015/06/08Apple-Pay-Giving-Shoppers-Even-More-Ways-to-Pay.html")
  107. #n=News("http://newsroom.bankofamerica.com/press-releases/small-business-banking/bank-america-survey-finds-small-business-owners-confidence-dow")
  108. #n=News("http://newsroom.fb.com/news/2016/04/news-feed-fyi-more-articles-you-want-to-spend-time-viewing/")
  109. #l=LinkList("https://www.blogger.com/feeds/10861780/posts/default",rss=True)
  110. #n=News("http://googleblog.blogspot.com/2016/05/translate-where-you-need-it-in-any-app.html") #FIXME
  111. #l=LinkList("https://apps.shareholder.com/rss/rss.aspx?channels=14&companyid=ONE",rss=True)
  112. #n=News("http://investor.shareholder.com/jpmorganchase/press/releasedetail.cfm?ReleaseID=970074")
  113. l=LinkList("http://www.samsungmobilepress.com/press/pressList.asp")
  114. if l is not None:
  115. for a in l.links:
  116. print(a)
  117. print(len(l.links))
  118. sys.exit(1)
  119. def sanitizeSelector(s):
  120. s=s.replace(">a","> a")
  121. if re.match(r'=\w+\]',s) is None:
  122. return s
  123. print(s)
  124. ret=re.sub(r"=(\w+)]", r'="\1"]', s)
  125. print(ret)
  126. return ret
  127. for s in d.sources():
  128. s["title"]=sanitizeSelector(s["title"])
  129. l=LinkList(s["link"],s["title"])
  130. if len(l.links)==0:
  131. print(s["link"])
  132. print(s["title"])
  133. print("###################################")
  134. #n=News("http://www.bloomberg.com/news/articles/2016-03-04/the-mystery-madoff-victims-who-left-2-5-billion-on-the-table")