Trouble with Twitter API using Python - python

I'm having some trouble in this python code:
import twitter
twitter_search = twitter.Twitter(domain="search.twitter.com")
trends = twitter_search.trends()
The error (404 page not found) is right here:
I'm using this package: http://github.com/sixohsix/twitter

Because of the new API change, you need to use this program:
import twitter
twitter_api = twitter.Twitter(domain="api.twitter.com", api_version='1')
WORLD_WOE_ID = 1
world_trends = twitter_api.trends._(WORLD_WOE_ID)
trends = world_trends()
print [trend['name'] for trend in trends[0]['trends']]
This is based, in part, on the book errata website.

According to the Twitter API documentation for the trends call the domain should be api.twitter.com:
import twitter
twitter_search = twitter.Twitter(domain="api.twitter.com")
print twitter_search.trends()
{u'trends': [{u'url': u'http://search.twitter.com/search?q=%23weedcommandments',
u'name': u'#weedcommandments'}, ...

You may want to checkout the updated IPython Notebook for Chapter 1 of Mining the Social Web that shows an updated example for this workflow and API call as well as a little bit of other context that is compatible with Twitter's v1.1 API. See http://nbviewer.ipython.org/urls/raw.github.com/ptwobrussell/Mining-the-Social-Web/master/ipython_notebooks/Chapter1.ipynb for a read-only version of the notebook.

Related

How to get 'user' data with stackoverflow api?

I want to see the 'user(specifically, user's display_name') data using Stackoverflow's API.
I'm using and reading the docs about StackExchange API, and still didn't get the idea about 'fetch' and anything about get the data.
Using 'beautifulsoup' or any crawling code, is it the only way to get the data?
from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
users = SITE.fetch('users')
print(users['items'])
Following the documentation for the StackAPI python library, the method skeleton is fetch(endpoint=None, page=1, key=None, filter='default', **kwargs).
So your api call will look something like:
from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
SITE.page_size = 5 # limits to 5 returned results for demo purpose
users = SITE.fetch('users', filter='!)mYVom7)TA9')
print(users['items'])
where the filter code was obtained from the get users docs for the api.
Returns:
[{'display_name': 'Jon Skeet'}, {'display_name': 'Gordon Linoff'}, {'display_name': 'VonC'}, {'display_name': 'BalusC'}, {'display_name': 'Darin Dimitrov'}]

LinkedIn Job Search REST API Issue(s)

I see there has been questions similar to this but the responses to them explains my asking a similar question here. I am wanting to utilize LinkedIn's REST API for my personal job hunt, specifically utilizing the job search API capabilities.
My problem comes in with retrieving an access token and autorisation to actually use the app. I have tried the below code - the first cell is to retrieve autorisation and the second is to retrieve an access token:
from linkedin import linkedin
APPLICATON_KEY = 'XXXXXX'
APPLICATON_SECRET = 'XXXXX'
RETURN_URL = 'http://localhost:8000'
authentication = linkedin.LinkedInAuthentication(APPLICATON_KEY, APPLICATON_SECRET, RETURN_URL,
linkedin.PERMISSIONS.enums.values())
print (authentication.authorization_url) #open this url on your browser
Access token:
authentication = linkedin.LinkedInAuthentication(
APPLICATON_KEY,
APPLICATON_SECRET,
RETURN_URL,
linkedin.PERMISSIONS.enums.values()
)
authentication.authorization_code = '#############################################'
result = authentication.get_access_token()
print ("Access Token:", result.access_token)
print ("Expires in (seconds):", result.expires_in)
When I attempt to retreieve authorisation (required for access token) the following error occurs:
File "/usr/local/lib/python3.6/dist-packages/linkedin/linkedin.py", line 294
except (requests.ConnectionError, requests.HTTPError), error:
^
SyntaxError: invalid syntax
It's an obvious syntax error, however am ignorant to what it is exactly - I have a feeling it might be my localhost
That error points to the comma separating the exception classes from the variable name.
A comma there is Python 2.x syntax, so that means the linkedin library you're using is not compatible with Python 3.
If you're using this library, you can see it's not been updated in 5 years. This (PyPI) seems like a slightly fresher fork.

PyBingSearch Module: Bing API error

I am trying to use the PyBingSearch module from https://github.com/tristantao/py-bing-search to use the Bing API to return search results. In the github page they have an example of how to use it that goes.
from py_bing_search import PyBingWebSearch
search_term = "Python Software Foundation"
bing_web = PyBingWebSearch('Your-Api-Key-Here', search_term, web_only=False) # web_only is optional, but should be true to use your web only quota instead of your all purpose quota
first_fifty_result= bing_web.search(limit=50, format='json') #1-50
When I try and run the line that declares first_fifity_result I get an error stating Request returned with code 401, error msg: The authorization type you provided is not supported. Only Basic and OAuth are supported
Why do I get this error? In my code I made sure to change Your-Api-Key-Here to my API key.
Did you swap your API key? Where is says "Your-API-Key-Here"
bing_web = PyBingWebSearch('Your-Api-Key-Here', search_term, web_only=False) # web_only is optional, but should be true to use your web only quota instead of your all purpose quota

Not getting amazon SalesRank using amazon API

I was using ItemLookup to get SalesRank using Amazon Product API. I am using python amazon product api
import amazonproduct
from lxml import objectify
config = {
'access_key': 'access key',
'secret_key': 'secret key',
'associate_tag': 'associate tag',
'locale': 'us'
}
amazon = amazonproduct.API(cfg=config)
result = amazon.item_lookup('B01B83IWH0', ResponseGroup="SalesRank")
but when i print the results their is no SalesRank, has amazon stopped giving SalesRank?
AttributeError: no such child: {http://webservices.amazon.com/AWSECommerceService/2011-08-01}SalesRank
I tried using amazon scratchpad but it also didn't give SalesRank but they have mention in their documentation to get SalesRank. How do I get SalesRank or if their is any alternative(I tried web scraping but amazon blocks me frequently). Please help, I am a beginner in using API's.
Using this library:
https://pypi.python.org/pypi/python-amazon-simple-product-api
from amazon.api import AmazonAPI
api = AmazonAPI(ACCESS_KEY, ACCESS_KEY_SECRET, amazon_tag)
item= api.item_lookup('B01B83IWH0')
# Get Sales Rank
rank = item.sales_rank
This gets rank, but doesn't seem to reflect the current rank listed on Amazon's website. I'm only just familiarizing myself with the Amazon API, will update with elaboration and details.

gdata autentication trouble python

I ran for awhile a python script to post articles on my blogspot blog.
everything ran smoothly until I started to get this auth error
RequestError: {'status': 401, 'body': 'User does not have permission to create new post', 'reason': 'Unauthorized'}
I really can't understand how to fix it reading gdata documentation.
Could you please suggest me how to do?
Thank you
Here the part of my code that doesn't work anymore:
from gdata import service
import gdata
import atom
blogger_service = service.GDataService('xxxxxx','xxxxxx')
blogger_service.service = 'blogger'
blogger_service.account_type = 'GOOGLE'
blogger_service.server = 'www.blogger.com'
blogger_service.ProgrammaticLogin()
def CreatePublicPost(blogger_service, blog_id, title, content,tags):
entry = gdata.GDataEntry()
entry.title = atom.Title('xhtml', title)
entry.content = atom.Content(content_type='html', text=content)
for tag in tags :
category = atom.Category(term=tag, scheme="http://www.blogger.com/atom/ns#")
entry.category.append(category)
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
Now there is a API version 3.0...
This old version is obsolete and no longer works, apparently...
You can find more about it here:
https://developers.google.com/blogger/
https://developers.google.com/blogger/docs/3.0/using/
And if you have questions about authentication, maybe those links help:
Having trouble trying to use gdata and oauth2 in python
Authentication with the Google Docs List API, Python and OAuth 2

Categories