1

I'm using twittersearch to iterate over user input topics and location of interest that will then be visualized in a leaflet map. So using 'place' I can get a map, no problem. I'm trying to increase my odds of getting points by using 'location' in another for loop. Do I need to geocode the location data, if so what am I missing?

Geocode piece..

from geopy import geocoders

def geo(location):
    g = geocoders.Nominatim()
    loc = g.geocode(location)
    return loc.latitude, loc.longitude

original 'place' piece that produces results..

for tweet in ts.search_tweets_iterable(tso):
    tweetText = ( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode("ascii", "ignore")
    print tweetText
    if tweet['place'] is not None:
        (lat, lng) = geo(tweet['place']['full_name'])
        tw = tweet['text']
        user= tweet['user']['screen_name']
        #map has markers placed at the location of the tweet, the pop up text is the contenet of the the tweet and the poster/user
        #this is in the form of username said tweet content
        folium.Marker([lat, lng], popup= user + " said " + tw,icon=folium.Icon(color='red',icon='info-sign')).add_to(mapLeafletPython)
        print( '@%s tweeted: %s' % (user, tw) + 'And they said it from (' + str(lat) +', ' +str(lng)+')').encode("ascii", "ignore")

this is the location piece that produces this error...AttributeError: 'NoneType' object has no attribute 'latitude' which occurs on the (lat1, lng1) line.

for tweet in ts.search_tweets_iterable(tso):
    tweetText = ( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode("ascii", "ignore")
    if tweet['user'] is not None:
        (lat1, lng1) = geo(tweet['user']['location'])
        tw = tweet['text']
        user= tweet['user']['screen_name']
        #map has markers placed at the location of the tweet, the pop up text is the contenet of the the tweet and the poster/user
        #this is in the form of username said tweet content
        folium.Marker([lat1, lng1], popup= user + " said " + tw,icon=folium.Icon(color='blue',icon='info-sign')).add_to(mapLeafletPython)
        print( '@%s tweeted: %s' % (user, tw) + 'And they said it from (' + str(lat) +', ' +str(lng)+')').encode("ascii", "ignore")

"tweetSearchUser" is the input from user as well as "where"

last piece that I think you need for testing besides variable ts which is the token..

tso = TwitterSearchOrder() # create a TwitterSearchOrder object
tso.set_keywords([tweetSearchUser]) # let's define all words we would like to have a look for
#tso.set_count(200) #limit return records to a number
tso.set_include_entities(False) # and don't give us all those entity information

Using this as a reference for twitter api and json format

http://socialmedia-class.org/twittertutorial.html

https://github.com/MichaelMartinez/blog/blob/master/content/iPython-Notebooks/Python-Twitter-Tools.md

I'm baffled here...

Updated with error...

3

The geopy geocoder will return a None object if a location can't be geocoded. For example:

>>> g = geocoders.Nominatim()
>>> loc = g.geocode("This place doesn't exist")
>>> type(loc)
NoneType

You'll need to take a look at the locations coming in from the user location, and probably through a try...except block around getting those results.

  • That thought had occurred to me but I've tried so many different things...kind of got lost in the fray...thanks a lot. – Staley Mar 1 '16 at 4:49
  • Is location going to be iterable and provide coordinates? – Staley Mar 1 '16 at 4:54
  • @Staley Not 100% sure, but I don't think so - certainly not in all cases. I believe it's the bit of location text that appears under the profile name on twitter which can be set to anything - the example in the twittersearch API is 'location': u"SwiekeCity\u2022PinkBabyRoom's",. – om_henners Mar 1 '16 at 5:00
  • So really I need to find another route if there is one...thanks again – Staley Mar 1 '16 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.