Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # coding: utf-8
- # Most of this code I found on the Internet, but I don't recall where. Don't ask me how it works.
- # It just does. At least it just does as of today, 22 Aug 2013.
- #
- # As-is, some weather data from my weather station and an image are posted on
- # Facebook under my user ID.
- # The machine sending the data can post the data without any manual involvement on my part.
- #
- # I hope this helps somebody.
- #
- # GDN
- import facebook
- import urllib
- import urlparse
- import subprocess
- import warnings
- # Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError).
- warnings.filterwarnings('ignore', category=DeprecationWarning)
- # Parameters of your app and the id of the profile you want to mess with.
- FACEBOOK_APP_ID = 'your app id here'
- FACEBOOK_APP_SECRET = 'your app secret here'
- FACEBOOK_PROFILE_ID = 'your profile ID number here'
- # Trying to get an access token. Very awkward. (original author's comment, not mine. GDN)
- oauth_args = dict(client_id = FACEBOOK_APP_ID,
- client_secret = FACEBOOK_APP_SECRET,
- grant_type = 'client_credentials')
- oauth_curl_cmd = ['curl',
- 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)]
- oauth_response = subprocess.Popen(oauth_curl_cmd,
- stdout = subprocess.PIPE,
- stderr = subprocess.PIPE).communicate()[0]
- try:
- oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'] [0]
- except KeyError:
- print('Unable to grab an access token!')
- exit()
- facebook_graph = facebook.GraphAPI(oauth_access_token)
- # The following line reads a local file containing weather data to post on facebook. GDN
- linestring = open('/var/www/weewx/mobile.html', 'r').read()
- print linestring
- # The following lines attach an image and link to the posting. GDN
- attachment = {
- "name": "nincehelser.com",
- "link": "http://nincehelser.com",
- "caption": "Current Peru Weather",
- "description": linestring,
- "picture": "http://nincehelser.com/kneperu2/image.jpg"
- }
- # Try to post something on the wall.
- try:
- fb_response = facebook_graph.put_wall_post(linestring, attachment, profile_id = FACEBOOK_PROFILE_ID )
- print fb_response
- except facebook.GraphAPIError as e:
- print 'Something went wrong:', e.type, e.message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement