parser.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if rss:
  35. feed = feedparser.parse(url)
  36. print(feed["bozo"])
  37. #print(feed["channel"])
  38. for i in feed["items"]:
  39. print(i["date"])
  40. print(i["date_parsed"])
  41. print(i["title"])
  42. print(i["summary"])
  43. print(i["link"])
  44. self.links=[]
  45. return
  46. if selector is None:
  47. #s=newspaper.build(url,language="en",memoize_articles=False) #memoize puede salvarme la vida
  48. s = newspaper.Source(url,memoize_articles=False)
  49. s.download()
  50. s.parse()
  51. s.set_categories()
  52. s.download_categories()
  53. s.parse_categories()
  54. s.generate_articles()
  55. for a in s.articles:
  56. self.articlePrinter(a)
  57. print(s.size())
  58. else:
  59. f=FrontPage(url,selector)
  60. for l in f.links:
  61. print(l)
  62. self.links=f.links
  63. def articlePrinter(self,a):
  64. print(a.url)
  65. # def getList(self):
  66. # ret=[]
  67. # for art in self.source.articles:
  68. # print(art)
  69. # print(art.url)
  70. # ret.append(art.url)
  71. # return ret
  72. class News():
  73. r=re.compile(r"hours? ago|yesterday|today|last week|month",flags=re.IGNORECASE)
  74. def __init__(self,url,lang="en"):
  75. url=self.sanitizeUrl(url)
  76. a = Article(url, language=lang,keep_article_html=True)
  77. a.download()
  78. a.parse()
  79. a.nlp()
  80. a.authors=self.dedup([self.fix_author(value) for value in a.authors if not self.isComment(value)])
  81. print(a.authors)
  82. print(a.publish_date)
  83. print(a.summary)
  84. print(a.keywords)
  85. #print(a.text)
  86. print(a.top_image)
  87. print(a.movies)
  88. print("_#_#_#_#_#_#_")
  89. print(htmlmin.minify(a.article_html,remove_empty_space=True))
  90. def sanitizeUrl(self,url):
  91. return url.replace("?share=google-plus-1","")
  92. def dedup(self,val):
  93. return list(set(val))
  94. def isComment(self,v):
  95. if self.r.match(v):
  96. return True
  97. return False
  98. def fix_author(self,a):
  99. return a.replace("_", " ").lower()
  100. #d=db()
  101. #l=LinkList("http://9to5google.com")
  102. #l=LinkList("http://www.alibabagroup.com/en/news/press")
  103. #l=LinkList("http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-news", selector=".ccbnTblLnk")
  104. #l=LinkList("http://about.americanexpress.com/news/pr/releases.aspx")
  105. #NOT WORKING l=LinkList("http://www.americanbanker.com/")
  106. l=LinkList("http://officialandroid.blogspot.com/atom.xml",rss=True)
  107. #n=News("http://9to5google.com/2016/05/11/nest-opensource-openthread-networking-protocol/?share=google-plus-1")
  108. #n=News("http://www.alibabagroup.com/en/news/article?news=p150908b")
  109. #n=News("http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-newsArticle&ID=1250170")
  110. #n=News("http://about.americanexpress.com/news/pr/2016/amex-dividend-payable-august-10-2016.aspx")
  111. sys.exit(1)
  112. def sanitizeSelector(s):
  113. s=s.replace(">a","> a")
  114. if re.match(r'=\w+\]',s) is None:
  115. return s
  116. print(s)
  117. ret=re.sub(r"=(\w+)]", r'="\1"]', s)
  118. print(ret)
  119. return ret
  120. for s in d.sources():
  121. s["title"]=sanitizeSelector(s["title"])
  122. l=LinkList(s["link"],s["title"])
  123. if len(l.links)==0:
  124. print(s["link"])
  125. print(s["title"])
  126. print("###################################")
  127. #n=News("http://www.bloomberg.com/news/articles/2016-03-04/the-mystery-madoff-victims-who-left-2-5-billion-on-the-table")