Created
August 8, 2016 17:06
-
-
Save pachecoder/7316a13d0a9ba272da769133d3e2fbe0 to your computer and use it in GitHub Desktop.
Bot para leer y publicar los enlaces al Chiguire Bipolar en Reddit
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 | |
import praw | |
import json | |
import OAuth2Util | |
import requests | |
import re | |
from lxml.cssselect import CSSSelector | |
import lxml.html | |
def check_none_type_object(obj): | |
if obj == None: | |
return "" | |
else: | |
return obj | |
def _concat_text(elements): | |
text = " " | |
for el in elements: | |
if check_none_type_object(el.text) != "": | |
text = text + _encode_text(el.text) | |
return text | |
def get_article(url): | |
req = requests.get(url) | |
statusCode = req.status_code | |
if statusCode == 200: | |
body = lxml.html.fromstring(req.content) | |
contenido = body.cssselect("div.entry > p") | |
titulo = body.xpath('//article/header/h1/text()') | |
fecha_articulo = body.xpath('//header/p/time/text()') | |
texto = _concat_text(contenido) | |
return texto | |
else: | |
print "Hubo problemas para adquirir la noticia." | |
def _encode_text(text): | |
return text.encode('utf-8') | |
user_agent = 'Publicar enlaces del Chiguire Bipolar' | |
r = praw.Reddit(user_agent) | |
o = OAuth2Util.OAuth2Util(r, print_log=True) | |
o.refresh() | |
subreddit = r.get_subreddit("vzla") | |
submissions = subreddit.get_hot(limit = None) | |
total = 0 | |
for post in submissions: | |
_flair = check_none_type_object(post.link_flair_text) | |
if _flair == "Humor" and post.ups >= 20: | |
url = post.url.encode('utf-8') | |
if "chiguirebipolar" in url: | |
total = total + 1 | |
print "url_article: ", post.permalink.encode('utf-8') | |
print "Title: ", post.title.encode('utf-8') | |
print "Post Url:", post.url.encode('utf-8') | |
print "\n" | |
contenido = get_article(url) | |
print contenido | |
print "\n" | |
print "link_flair: ", _flair.encode('utf-8') | |
print "author:", post.author | |
_flairAuthor = check_none_type_object(post.author_flair_text) | |
print "author flair text", _flairAuthor.encode('utf-8') | |
print "NSFW:", post.over_18 | |
print "Number of Comments:", post.num_comments | |
print "Upvote", post.ups | |
print "Downvote", post.downs | |
print "Title: ", post.title.encode('utf-8') | |
print "Text: ", post.selftext.encode('utf-8') | |
puntaje = post.score | |
print "Score: ", puntaje | |
print "---------------------------------\n" | |
print total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment