| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #!/usr/bin/python3
- import sys
- import re
- import newspaper
- import pymongo
- import htmlmin
- import feedparser
- import datetime
- from frontpage import FrontPage
- from newspaper import Article
- from db import db
- from source import Source
- class LinkList():
- def __init__(self,url,selector=None,rss=False):
- self.links=[]
- if rss:
- feed = feedparser.parse(url)
- #print(feed["bozo"]) FIXME: If bozo==1 => error
- self.links=[i["link"] for i in feed["items"]]
- print(self.links)
- else:
- if selector is None:
- #s=newspaper.build(url,language="en",memoize_articles=False) #memoize puede salvarme la vida
- s = newspaper.Source(url,memoize_articles=False)
- s.download()
- s.parse()
- s.set_categories()
- s.download_categories()
- s.parse_categories()
- s.generate_articles()
-
- self.links=list(set([a.url for a in s.articles])) #avoid dupes
- else:
- f=FrontPage(url,selector)
- self.links=f.links
- self.links=self.purgeLinks(self.links)
- def purgeLinks(self,l):
- return [ sanitizeUrl(link) for link in l if not "presslist" in link.lower() and not "videolist" in link.lower() ]
- class News():
- r=re.compile(r"hours? ago|yesterday|today|last week|month",flags=re.IGNORECASE)
- r_noise=re.compile(r"posted|Product|Google",flags=re.IGNORECASE)
-
- URL=""
- AUTHORS=[]
- SCRAP_DATE=None
- PUBLISH_DATE=None
- SUMMARY=""
- TEXT=""
- HTML=""
- ERROR=False
- def __init__(self,url,lang="en"):
- url=sanitizeUrl(url)
- self.URL=url
- a = Article(url, language=lang,keep_article_html=True)
- a.download()
- a.parse()
- a.nlp()
-
- a.authors=self.dedup([self.fix_author(value) for value in a.authors if not self.isComment(value) and not self.isNoise(value)])
-
- self.SCRAP_DATE=datetime.datetime.now()
- self.AUTHORS=a.authors
- self.PUBLISH_DATE=a.publish_date
- self.SUMMARY=a.summary
- self.TEXT=a.text
- self.HTML=htmlmin.minify(a.article_html,remove_empty_space=True)
- def get(self):
- return {
- "authors": self.AUTHORS,
- "scrap_date": self.SCRAP_DATE,
- "publish_date": self.PUBLISH_DATE,
- "summary": self.SUMMARY,
- "text": self.TEXT,
- "html": self.HTML
- }
- def dedup(self,val):
- return list(set(val))
- def isNoise(self,v):
- if self.r_noise.match(v):
- return True
- return False
- def isComment(self,v):
- if self.r.match(v):
- return True
- return False
-
- def fix_author(self,a):
- return a.replace("_", " ").lower()
- unsharer=re.compile(r"\?share=.+$",flags=re.IGNORECASE)
- def sanitizeUrl(url):
- global unsharer
- return re.sub(unsharer, "",url)
- def sanitizeSelector(s):
- s=s.replace(">a","> a")
- if re.match(r'=\w+\]',s) is None:
- return s
- ret=re.sub(r"=(\w+)]", r'="\1"]', s)
- return ret
- d=db()
- for s in d.sources():
- print(s["name"])
- sel=None
- rss=False
- if "selector" in s:
- sel=s["selector"]
- if "rss" in s:
- rss=bool(["rss"])
- l=LinkList(s["link"],sel,rss=rss)
- if l is not None:
- for a in l.links:
- print(a)
- print(len(l.links))
- n=News(l.links[0])
- print(n.get())
- break
|