Skip to content

Instantly share code, notes, and snippets.

@mariaanko mariaanko/lasky.py

Created May 6, 2017
Embed
What would you like to do?
bot posting to facebook
#!/usr/bin/python
import random
import facebook
import urllib2
from bs4 import BeautifulSoup
def post_to_fb(rand_quote):
cfg = {
"page_id":"333666",
"access_token":"inyourdreamscunt"
}
print "Posting to FB:"
print rand_quote
graph = facebook.GraphAPI(access_token=cfg["access_token"], version="2.9")
graph.put_object(parent_object="me", connection_name="feed", message=rand_quote)
print "Posted."
def read_quotes():
quotes_url = "http://citaty.vychytane.sk/k/37/o-laske/"
quotes_url2 = "http://citaty.vychytane.sk/k/37/o-laske/s/2/"
quotes_url3 = "http://citaty.vychytane.sk/k/37/o-laske/s/3/"
quotes_url4 = "http://citaty.vychytane.sk/k/37/o-laske/s/4/"
print "fetching pages..."
quotes_page = urllib2.urlopen(quotes_url)
quotes_page2 = urllib2.urlopen(quotes_url2)
quotes_page3 = urllib2.urlopen(quotes_url3)
quotes_page4 = urllib2.urlopen(quotes_url4)
soup = BeautifulSoup(quotes_page, "lxml")
soup2 = BeautifulSoup(quotes_page2, "lxml")
soup3 = BeautifulSoup(quotes_page3, "lxml")
soup4 = BeautifulSoup(quotes_page4, "lxml")
clear_quotes = []
clear_quotes.append(parse_quotes(soup))
clear_quotes.append(parse_quotes(soup2))
clear_quotes.append(parse_quotes(soup3))
clear_quotes.append(parse_quotes(soup4))
rand_quote = random.choice(clear_quotes)
return rand_quote
def parse_quotes(soup):
print "parsing..."
soup.prettify("utf-8")
quotes = []
for row in soup.find_all(class_="item"):
quotes.append(row.find("p"))
clear_quotes = []
for quote in quotes:
clear_quotes.append(str(quote).split("<br/>")[0].replace("<p>", ""))
return clear_quotes
def main():
rand_quote = read_quotes()
post_to_fb(rand_quote)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.