Created
May 4, 2016 00:12
-
-
Save murilobsd/493254c5d83f9ea53a63f69148f7058a to your computer and use it in GitHub Desktop.
bot politico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
import urllib2 | |
import argparse | |
import facebook | |
import time | |
import random | |
import re | |
from lxml import html | |
URL = "http://www1.folha.uol.com.br/poder/?cmpid=menutopo" | |
# caputamos algumas noticias | |
def news_political(timeout=10): | |
try: | |
resp = urllib2.urlopen(URL, timeout=timeout) | |
except urllib2.HTTPError as e: | |
if e.code == 404: | |
print u"Xii url não existe mais" | |
return None | |
except socket.timeout: | |
print "Excedeu o tempo limite." | |
return False | |
except urllib2.URLError as e: | |
print e | |
print "Error na captura do processo:" | |
return False | |
else: | |
return resp.read() | |
# filtramos o conteudo das noticias encontradas | |
def news_parser(content, expressoes=['temer', 'psdb', 'russomano']): | |
news_filter = [] | |
noticias = html.fromstring(content).xpath("//a[contains(@href, 'http://www1.folha.uol.com.br/poder/2016')]") | |
if noticias: | |
for num, noticia in enumerate(noticias): | |
for expressao in expressoes: | |
if expressao in noticia.xpath("./@href")[0]: | |
news_filter.append((noticia.xpath("./@href")[0], re.sub("[\r\n\t]", "", "".join(noticia.xpath(".//text()"))))) | |
return news_filter | |
else: | |
print "Não foram encontrado notícias" | |
return None | |
# publicamos no facebook | |
def public_facebook(token, link, description, timesleep=300): | |
attachment = { | |
'name': 'Robo Político', | |
'link': link, | |
'caption': description[:20]+' ...', | |
'description': description, | |
'picture': '' | |
} | |
# print attachment | |
graph = facebook.GraphAPI(token) | |
status = graph.put_wall_post(message='Veja isso!!!', attachment=attachment) | |
print "TROLANDO AMIGOS!!!" | |
time.sleep(timesleep) | |
return True | |
def main(token, timeout, expressoes): | |
content = news_political() | |
if content: | |
news = news_parser(content, expressoes) | |
print news | |
if news: | |
for n in range(len(news)): | |
public_facebook(token, n[0], n[1], timeout) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-t', '--token', type=str, | |
help="token facebook https://developers.facebook.com/apps", | |
required=True) | |
parser.add_argument('-ti', '--timeout', type=int, default=10, | |
help="time wait to new post in minutes") | |
parser.add_argument('-f', '--filter', nargs='+', type=str, | |
help="words for filter ex: 'pt e psdb' temer cunha bolsonaro", | |
required=True) | |
args = vars(parser.parse_args()) | |
main(args['token'], args['timeout']*60, args['filter']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment