ptwobrussell / Mining-the-Social-Web

The official online compendium for Mining the Social Web (O'Reilly, 2011)
http://bit.ly/135dHfs
Other
1.21k stars 491 forks source link

Arrggg. Twitter Trends #15

Closed ghost closed 12 years ago

ghost commented 12 years ago

I'm sorry to beat a dead horse... I've read the errata, and tried to understand how example 1.3 has changed - and then seemingly changed again. But it looks so dead simple, but nothing I do seems to make it work.

Here is where I've ended up:

import twitter twitter_api=twitter.Twitter(domain="api.twitter.com", api_version='1') trends=twitter_api.trends()

Traceback (most recent call last): File "", line 1, in File "build/bdist.macosx-10.7-intel/egg/twitter/api.py", line 165, in call File "build/bdist.macosx-10.7-intel/egg/twitter/api.py", line 180, in _handle_response twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1/trends.json using parameters: () details: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

(followed by a ton of HTML and CSS... ) Reading the API documentation, I'm pretty sure I need to be including some parameters in the line: > > > twitter_api=twitter.Twitter(domain="api.twitter.com", api_version='1') but no matter what I try it just doesn't work. I'm sure there's a simple solution, but I've bashed my head against it so long that I just can't see it. Anyone care to show me what I'm doing wrong? Thanks! John
ptwobrussell commented 12 years ago

Thanks for pointing out that this is still a point of confusion. It's amazing how much trouble this first example has been given how simple it was supposed to be as part of the whole warm up in the introduction. I'll take a few minutes tonight to try and make sure that code is updated in all of the right places.

The short story is that this should get you rolling:

import twitter
twitter_api=twitter.Twitter(domain="api.twitter.com", api_version='1')
WORLD_WOE_ID = 1 # The Yahoo! Where On Earth ID for the entire world
world_trends = twitter_api.trends._(WORLD_WOE_ID) # get back a callable
[ trend for trend in world_trends()[0]['trends'] ] # call the callable and iterate through the trends returned

# [{u'url': u'http://twitter.com/search/%23TheGame', u'query': u'%23TheGame', u'events': None, u'promoted_content': None, u'name': u'#TheGame'}, {u'url': u'http://twitter.com/search/%22Woman%20Thou%20Art%20Loosed%22', u'query': u'%22Woman%20Thou%20Art%20Loosed%22', u'events': None, u'promoted_content': None, u'name': u'Woman Thou Art Loosed'}, {u'url': u'http://twitter.com/search/%23FelicidadesNOTEAMA', u'query': u'%23FelicidadesNOTEAMA', u'events': None, u'promoted_content': None, u'name': u'#FelicidadesNOTEAMA'}, {u'url': u'http://twitter.com/search/%22Lmao%20Jason%22', u'query': u'%22Lmao%20Jason%22', u'events': None, u'promoted_content': None, u'name': u'Lmao Jason'}, {u'url': u'http://twitter.com/search/%22Pooch%20Hall%22', u'query': u'%22Pooch%20Hall%22', u'name': u'Pooch Hall', u'promoted_content': None, u'events': None}, {u'url': u'http://twitter.com/search/McHottie', u'query': u'McHottie', u'events': None, u'promoted_content': None, u'name': u'McHottie'}, {u'url': u'http://twitter.com/search/Brandy', u'query': u'Brandy', u'events': None, u'promoted_content': None, u'name': u'Brandy'}, {u'url': u'http://twitter.com/search/Moesha', u'query': u'Moesha', u'events': None, u'promoted_content': None, u'name': u'Moesha'}, {u'url': u'http://twitter.com/search/Derwin', u'query': u'Derwin', u'events': None, u'promoted_content': None, u'name': u'Derwin'}, {u'url': u'http://twitter.com/search/%23PumpedUpKicks', u'query': u'%23PumpedUpKicks', u'events': None, u'promoted_content': None, u'name': u'#PumpedUpKicks'}]

This snippet is captured as a gist at https://gist.github.com/1592859

The longer story is actually pointed out at http://oreilly.com/catalog/errata.csp?isbn=0636920010203 in the "Update #2" part of the errata submitted by "Shirley" on Oct 5,2011, but the way that O'Reilly exposes/formats errata is admittedly a disaster, so I'm not surprised that you and others have missed it. I'm not sure if this fix has made it into the ebook updates yet or not, and I'm going to double check that any code examples here on GitHub are updated accordingly. Thanks for your patience.

ptwobrussell commented 12 years ago

By the way, this recipe that's featured in 21 Recipes for Mining Twitter may also be helpful to anyone who is interested in this thread: https://github.com/ptwobrussell/Recipes-for-Mining-Twitter/blob/master/recipe__get_search_results_for_trending_topic.py

ghost commented 12 years ago

Thanks so much. I'm learning a lot from both Mining the Social Web and Recipes for Mining Twitter.

Ramaznaz commented 12 years ago

I've been trying a little bit of the code: https://github.com/ptwobrussell/Recipes-for-Mining-Twitter/blob/master/recipe__get_search_results_for_trending_topic.py

But I get this error:

Traceback (most recent call last): File "C:/Python27/trend.py", line 7, in from recipe__extract_tweet_entities import get_entities ImportError: No module named recipe__extract_tweet_entities

I'm using ActivePython with Python2.7 (on a pc), I've installed twitter, json... could run the oauth thing but I get stalled with different codes :-/

ptwobrussell commented 12 years ago

Did you checkout the entire repository? If the file recipe__extract_tweet_entities.py isn't in the same working directory as the script you are running, it'll fail with this message. (It appears that you are running this particular script in isolation, although the specifics of how you are executing with ActivePython may be different than how it would be run in a typical terminal window with the python interpreter.)

Ramaznaz commented 12 years ago

Couldn't, got this:

pypm install recipee__extract_tweet_entities.py

warning: no such package found for requirement "recipe-extract-tweet- entities.py"; try PyPM Index: http://code.activestate.com/pypm/search :recipe-extract-tweet-entities.py/

ptwobrussell commented 12 years ago

I think there's basically just a misunderstanding about how to get the latest version of this source code through GitHub. When I suggested "checkout the entire repository", what I meant was start at https://github.com/ptwobrussell/Mining-the-Social-Web/ and then use Git to clone the code repository as alluded to on that page, or if you prefer, use the "Download" link to download the latest snapshot of the entire repository. (The tool pypm won't help you do these things.)

Let me know if this makes sense.

amejiarosario commented 11 years ago

Unfortunately, the http://search.twitter.com/trends it's deprecated! So, I ended up using the new one is this: https://api.twitter.com/1/trends/1.json

jamesmusick commented 11 years ago

I'm also getting the dreaded 'module' object has no attribute 'Twitter' error. After reading through the comment threads & misc web forums I can confirm that

  1. The dependent libraries are installed, in easy-install.pth & recognized by my IDE
  2. I downloaded the entire repository (message 2 above this) to make sure that there wasn't a dependency I didn't realize
  3. Using Pything 2.7.2 on a Mac OS 10.8.2 (and PyCharm, for what it's worth)
    • Tried both the http://code.google.com version of the latest & the github version (I'm assuming github is most recent based on comments on the other site).
    • Tried removing all files, the egg, etc & re-tracing my steps - same result.

Thanks for any help/suggestions.

ptwobrussell commented 11 years ago

@jamesmusick - can you confirm that you're using this particular twitter module at https://github.com/sixohsix/twitter

If you have pip installed, you can get it via

$ pip install -e git+http://github.com/sixohsix/twitter.git#egg=github-pip-install

If you don't have pip already installed, you can simply

$ easy_install pip

to get pip installed.

Once installed, you should be able to do the following in an interpreter in order to make sure that the version of the twitter package that you think is installed is in fact being loaded. If you can report back that info, I might be able to help you further in the event that your PYTHONPATH is loading another package

import twitter print twitter.file

kimskams80 commented 11 years ago

Dear folks, I have tried attempting example 1.3 but yet not successful. I am doing in Interactive Python Shell. In first attempt, I tried the example as given in book and it gave error of API version (with suggestion I should use API 1.1) then I searched for latest code on github and repeated with code given above then it is giving authentication error: Twitter sent status 400 error "bad authentication data", code=215

Any help please? Thanks

ptwobrussell commented 11 years ago

@kimskams80 - The text of the example code in the book you have is almost certainly dated, and you'll want to stick with the code as listed in this repository as you follow along. It should be up to date so far as I know, but please report issues as you encounter them, and I'll be happy to address.

As far as this particular example goes, you might want to open a new issue and include your exact stack trace and include the URL link in this repository to what you refer to as "code given above" just so we are both looking at the exact same code.

Also, did you know that a 2nd Edition of the book and code is almost ready? All of the code is actually already out on GitHub here: https://github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition - I would encourage everyone who is able to start migrating to this 2nd Edition repo.

Take a look at Examle 9.3 from the 2nd Edition for what corresponds to Example 1-3 out of the 1st Edition if it's helpful: http://nbviewer.ipython.org/urls/raw.github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition/master/ipynb/Chapter%209%20-%20Twitter%20Cookbook.ipynb

Let me know how this goes. I want to help you work through whatever is going on. Thanks!

nikraveshucb commented 9 years ago

My problem is that: I can get the code work for WORLD_WOE_ID = 1 and US_WOE_ID = 23424977.

however, when I changed the IDs to another WOE IDs, I got error, no matter what IDs do I use. I used all the current WOE IDs and even I called the IDs from python so to make sure they are the most recent ones. Please try the following IDs for example and let me know if you get the same errors. thanks

CALI_WOE_ID = 2347563

North_WOE_ID = 24865672

TwitterHTTPError: Twitter sent status 404 for URL: 1.1/trends/place.json using parameters: