Did facebook recently start sending data only compressed in Graph API - facebook-graph-api

For some reason this query always returned a standard string (json). For some reason starting today (Jan 24,2014) the results are GZIPped. Easy fix but I was not aware of any changes to the Graph API regarding output. Has anyone else heard of this or is experiencing this?
req = urllib2.Request(x)
req = 'https://graph.facebook.com/search?q=%22are%20eligible%20for%20disability%20compensation%20from%20the%20Department%20of%22&limit=250&type=post&until=1390665731&access_token=xxxxxxxxxxxxxxxxxxxxX2HTOgagZDZD&locale=en_US'
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
sys.exit(0)
Developer NOTE: Please do not post access tokens or secret keys in Q and A.

We were having the same issue. Resolved itself 15 mins ago.

Related

fetch the retweets for the tweets using python

I have to fetch the retweets for the tweets and create the JSON file with retweets,user id etc using the python script. Kindly help me to sort it our this issues.
Thanks in advance!!
This task require some fields of knowledge, and since you ask in a general way, I reckon you need a script to run immediately, but setting up this process requires sometime
This part to get connect to twitter API
from twython import Twython, TwythonError
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'
twitter = Twython(APP_KEY, APP_SECRET)
Use Twitter API call from Twython,
you can find a list here https://twython.readthedocs.io/en/latest/api.html, the param is the same as twitter API
response = twitter.get_retweets(id, 100)
Pagnation
each call to API have limit of returns, in example for engine.get_friends_ids was limited to 5000 (https://dev.twitter.com/rest/reference/get/friends/ids), if you want to get more than 5000, you have to use the cursor in the returned result (if cur = 0 in json returned means no more results), following is example of how to handling cursor
#Set a temp to loop
cur = -1
#Stop when no more result
while cur !=0:
response = twitter.get_friends_ids(user_id=user_id, cursor=cur)
#Some code to handle the response
cur = response["next_cursor"]
API key
Key expires after some calls (https://dev.twitter.com/rest/public/rate-limits), so you need to set some code to auto change your key, or wait for some period (key reached limit return error code 429)
Response
The response from API was in JSON format, which was easy to use, you can access data by selecting base on response[key], in example
reponse["ids"] or response["next_cursor"]

Tweepy raises bad authentication data error

I am working on a twitter bot using tweepy. The bot auto-replies to specific users whose tweet handle it receives as input. The bot was working fine for weeks and then suddenly started throwing this 'Bad Authentication Data' or the following to be more precise :
tweepy.error.TweepError: [{'message': 'Bad Authentication data.', 'code': 215}]
Apparently the problem is in this particular part of the code :
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ts=api.user_timeline(screen_name=s,count=1)
I have entered the correct keys for the twitter application. I read about this problem on blogs where people say that it is an issue with POSTFIELDS and that it can be fixed by passing the status as URL in the api.update_status function. Is that right? If yes, please give me an example of how it can be done. I'm passing the message and tweet reply id in the update_status function. Thanks in advance.
I wrote
auth.secure = True
and it fixed in my case, hope it helps!

Pandas: datareader unable to get historical stock data

I found that some of the stock exchanges is not supported for datareader. Example, Singapore. Any workaround?
query = web.DataReader(("SGX:BLA"), 'google', start, now) return such error`
IOError: after 3 tries, Google did not return a 200 for url 'http://www.google.com/finance/historical?q=SGX%3ABLA&startdate=Jan+01%2C+2015&enddate=Apr+20%2C+2016&output=csv
It works for IDX indonesia
query = web.DataReader(("IDX:CASS"), 'google', start, now)
That URL is a 404 - pandas isn't at fault, maybe just check the URL? Perhaps they're on different exchanges with different google finance support.

How to make use of Slack Web API?

I want to use the provided slack web API https://api.slack.com/web#basics to get some messages out of a channel. I looked at https://api.slack.com/methods/channels.history & used the Request API to call the service.
payload = {'token': 'XXXXXXXXXXXX', 'channel': '#scanbot' , 'count' : '10'}
r = requests.get('https://slack.com/api/channels.history', params=payload)
print r.status_code
print r.text
But i am getting the error:
200
{"ok":false,"error":"channel_not_found"}
I am pretty sure the channel exists and I am providing correct API key. Can someone point me at the correct directions please?
You need to pass the channel ID as the argument to the channels.history endpoint.
The channel IDs may be fetched by checking the channels.list endpoint.
See for example the source of the Slacker package.
The JSON-formatted response may be parsed with:
import json
data = json.loads(r.text)
print data

Is there any nicer way to get the full message from gmail with google-api

I'm working on a project where I, among other things, need to read the message in e-mails from my google account. I came up with a solution that works but wonder if there are any simpler ways?
The first part of the code is pretty standard to get access to the mailbox. But I post it so you can see what I did to get it to work.
SCOPES = 'https://www.googleapis.com/auth/gmail.modify'
CLIENT_SECRET ='A.json'
store =file.Storage('storage.json')
credz=store.get()
flags = tools.argparser.parse_args(args=[])
if not credz or credz.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET,SCOPES)
if flags:
credz = tools.run_flow(flow, store, flags)
GMAIL = build('gmail','v1',http=credz.authorize(Http()))
response = GMAIL.users().messages().list(userId='me',q='').execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
print len(messages)
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId='me', q=query,pageToken=page_token).execute()
messages.extend(response['messages'])
FromMeInd=0
for message in messages:
ReadMessage(GMAIL,'me',message['id'])
It is this part that I'm more interested to imporve. Is there any other way to more directly get the message with python and the gmail-api. I've looked through the api documentation but could not get any more efficient way to read it.
def ReadMessage(service,userID,messID):
message = service.users().messages().get(userId=userID, id=messID,format='full').execute()
decoded=base64.urlsafe_b64decode(message['payload']['body']['data'].encode('ASCII'))
print decoded
You can get the body as raw and then parse it using the standard Python email module
According to the official API: https://developers.google.com/gmail/api/v1/reference/users/messages/get:
import email
message = service.users().messages().get(userId='me', id=msg_id,
format='raw').execute()
print 'Message snippet: %s' % message['snippet']
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
mime_msg = email.message_from_string(msg_str)
You'll get a mime message with a payload containing mime parts, e.g. plain text, HTML, quoted printable, attachments, etc.