import facebook import requests import twitter from bureau import Bureau, add_command, add_api class TWrapper(): pass class PublicRelations(Bureau): """ The Public relations department manages the flow of information between the screenless office and the general public. It provides interfaces for Twitter, Facebook and other electronic PR platforms. """ name = "Public Relations" prefix = "PR" version = 0 def __init__(self): Bureau.__init__(self) self.auth = twitter.OAuth( "423391766-bhjHwzD1eJfxB9s7ZvmeHReNVJM0d93kJ8KSrHEZ", "qSvSYMs4lpSKbZtiuOjB1Gon5rzye8whvadcOdDpFocwa", "arI4UCnfMeKZwAnyvJJIHVnUN", "agrtzvh3eqSNlZpmPJZ0otclqSUjLKI3JrqugCPrp1Ch9dqsTl") self.t = TWrapper() self.t.t = twitter.Twitter(auth=self.auth) @add_command("tweetpic", "Post a Document Camera Image to Twitter") def tweet_pic(self): """ Takes a photograph using the document camera and posts it to Twitter. """ photo = self.send("PX", "photo")["photo"] with open(photo, "rb") as imagefile: imagedata = imagefile.read() t_up = twitter.Twitter(domain='upload.twitter.com', auth=self.auth) id_img1 = t_up.media.upload(media=imagedata)["media_id_string"] self.t.t.statuses.update(status="#screenless", media_ids=id_img1) @add_command("fbpost", "Post to Facebook") def post_fb(self): """ Takes a photograph using the document camera and posts it to Facebook. """ photo = self.send("PX", "photo")["photo"] access_token = 'EAADixisn70ABADh2rEMZAYA8nGzd6ah8RFZA3URba263aCQ63ajLeTiZC5sgZCyIVSmRZBWReVsO9IuaLibX5RjW9Ja2tTZAbxgrDr1dPJzyGwcGTSV9bW1W4NigN0d9dFIH35W2fZBOkhvuLqOCDCBacIPjXPMxF7DRGyrz5lVHxTc04OlBeRX' page_id = "screenless" graph = facebook.GraphAPI(access_token) #graph.put_object(parent_object=page_id, connection_name='feed', # message='testing screenless post') print("uploading photo " + photo) graph.put_photo(image=open(photo, 'rb'), album_path=page_id + "/photos", message='test photo') #files = {"file": ("cam.jpg", open(photo, 'rb'))} #args = {} #args["access_token"] = access_token #args["caption"] = "test cam" #resp = requests.post(facebook.FACEBOOK_GRAPH_URL + graph.version + "/"\ # + "me" + "/photos", # files=files, # params=args, # timeout=graph.timeout, # proxies=graph.proxies) #print(resp.json()) @add_command("twtimeline", "Print Recent Tweets") def tw_timeline(self, data=None): """ Print some recent tweets from your home timeline. Default is 10. """ if data: try: count = data["count"] except KeyError as err: print("You need to specify how many tweets you want!") else: count = 10 # TODO: add fancier formatting i.e. inverted text for username/handle tweets = self.t.t.statuses.home_timeline(count=count) out = "" for t in tweets: out += t["user"]["name"] + " : " + t["text"] + "\r\n\r\n" self.print_small(out) # TODO: download and print image entities: # https://dev.twitter.com/overview/api/entities-in-twitter-objects def main(): pr = PublicRelations() pr.run() if __name__ == "__main__": main()