User:Lidia.Pereira/PNMII/GS

From XPUB & Lens-Based wiki
< User:Lidia.Pereira‎ | PNMII
Revision as of 22:39, 2 March 2014 by Lidia.Pereira (talk | contribs) (Lidia.Pereira moved page GS to User:Lidia.Pereira/PNMII/GS)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Mix Script (posts both on Facebook and Twitter) which also posts images on Facebook:
Seeing that Twitter's interface is not the best for posting pictures, I decided to only post them on Facebook.

import sqlite3, re, time, os
import urllib2
from bs4 import BeautifulSoup
import tweepy, facepy
from facepy import GraphAPI
from urlparse import urljoin



consumer_key="aJiJkOEdd3YKiXVRyfnWDg"
consumer_secret="8TDG4KqBLZPqcvjNFZb7qtJ7brSyqnuTNd1Y9yQrEM"
access_token="2308513993-50VDXIyyKvmITBrGEyvzzVa00v6y3KZoihtf5xs"
access_token_secret="53g8fdSl2URB4SY294XncUgAjGcmojWfRjacc3Hjuk2XD"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

graph= GraphAPI("CAATZCEv2dRlABABZBEjBMq3SudDcSoP5MzZBrccwM7eDhnelmgELB3LBHJ17Fez31GC0YZCYFJparvjUXXZCDQb1p\
    LFZCmSWA9yZCDOQY5MfUvvv9Xc2YgQ9OvEGazwutXYZAad1Po4tIdnuAOSR0ZBHVX3VIgOV1WBHAuxZB7Wi87Y11GEcuDKwiXVZCUbuQPOIHMZD")
places = sqlite3.connect("places.sqlite")
database = places.cursor()

database.execute('''SELECT url FROM moz_places''')

january = re.compile(r"http://\S+\w")

lista = []
n = 0

#get the urls
for data in database:
    urls = database.fetchall()
    for url in urls:
        february = str(url)
        march = january.findall(february)
        april = str(march)
        may = april.strip("['']")
        if may != "":
            lista.append(may)
        database.execute('''DELETE FROM moz_places WHERE url=?''',(url))
        places.commit()

for item in lista:
    try: 
        june = urllib2.urlopen(item)
        july = june.read()
        august = BeautifulSoup(july)
        images = august.findAll("img")
        n = n + 1
        if august.title != None:
            september = august.title.string
            if september:
                october = september.encode("utf-8")
                try:
                    api.update_status(october) 
                    if images:
                            imagetopost = None
                            for image in images:
                                    imgsrc = image.get("src")
                                    if imgsrc:
                                        imgsrc = urljoin(item,imgsrc)
                                        tuesday = urllib2.urlopen(imgsrc)
                                        info = tuesday.info() #getting the MIME type of a page
                                        typ = info.gettype()
                                        wednesday = tuesday.read()
                                        if typ == "image/jpeg":
                                            print october
                                            print typ
                                            imagetopost = "file%04d.jpg" % n
                                            f = open(imagetopost,"wb")
                                            f.write(wednesday)
                                            f.close()
                                            break
                            if imagetopost:
                                graph.post(path ="me/photos", source= open(imagetopost), message=october)
                                os.remove("file%04d.jpg" % n)
                            else:
                                graph.post(path="me/feed", message = october)                                   
                    time.sleep(3)
                except tweepy.error.TweepError or facepy.exceptions.FacebookError or httplib.BadStatusLine:
                    print "Caca!"                               
    except urllib2.HTTPError:
        print "Bah!"