Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add place info error #5

Open
burakoglakci opened this issue Jan 17, 2023 · 2 comments
Open

Add place info error #5

burakoglakci opened this issue Jan 17, 2023 · 2 comments

Comments

@burakoglakci
Copy link

Dear Foote,
I try to associate the place info with the tweet, just like the user info. But I get an error.
The sections I made changes and the line I got an error on are as follows. Thanks a lot.

client = tweepy.Client(bearer_token, wait_on_rate_limit=True) hoax_tweets = [] for response in tweepy.Paginator(client.search_all_tweets, query = 'deprem lang:tr', user_fields = ['id', 'name', 'username', 'public_metrics', 'description', 'location', 'verified'], tweet_fields = ['id', 'created_at', 'geo', 'public_metrics', 'text'], place_fields = ['full_name','place_type'], expansions = ['author_id', 'geo.place_id'], start_time = '2023-01-14T00:00:00Z', end_time = '2023-01-15T00:00:00Z', max_results=500): time.sleep(1) hoax_tweets.append(response)

`result = []
user_dict = {}
place_dict = {}

for response in hoax_tweets:
# Take all of the users, and put them into a dictionary of dictionaries with the info we want to keep
for user in response.includes['users']:
user_dict[user.id] = {'username': user.username,
'name': user.name,
'followers': user.public_metrics['followers_count'],
'tweets': user.public_metrics['tweet_count'],
'description': user.description,
'location': user.location,
'verified': user.verified
}

for place in response.includes['places']:
    place_dict[place.id] = {'full_name': place.full_name,
                            'place_type': place.place_type
                            }
      
for tweet in response.data:
    # For each tweet, find the author's information
    author_info = user_dict[tweet.author_id]
    # For each tweet, find the places information
    place_info = place_dict[tweet.place_id]  #**Error Line**
                            
                               
    # Put all of the information we want to keep in a single dictionary for each tweet
    result.append({'author_id': tweet.author_id, 
                   'username': author_info['username'],
                   'author_followers': author_info['followers'],
                   'author_tweets': author_info['tweets'],
                   'author_description': author_info['description'],
                   'author_location': author_info['location'],
                   'text': tweet.text,
                   'created_at': tweet.created_at,
                   'retweets': tweet.public_metrics['retweet_count'],
                   'replies': tweet.public_metrics['reply_count'],
                   'likes': tweet.public_metrics['like_count'],
                   'quote_count': tweet.public_metrics['quote_count'],
                   'tweet_id': tweet.id,
                   'author_name': author_info['name'],
                   'verified': author_info['verified'],
                   'full_name': place_info['full_name'],
                   'place_type': place_info['place_type']
                  })`
@jdfoote
Copy link
Owner

jdfoote commented Feb 14, 2023

It's a bit more complicated because not all tweets have place info.

I put up a new video at https://www.youtube.com/watch?v=HtB2CQpMk3g and code at https://github.com/jdfoote/Intro-to-Programming-and-Data-Science/blob/fall-2022/extra_topics/twitter_v2_with_place_and_media.ipynb

@burakoglakci
Copy link
Author

burakoglakci commented Feb 20, 2023

Dear Foote

Thank you very much for your answer, video, and code. The code works quite well in this form.
There's a point I'm curious about here. While the Place field does not contain point coordinates (lat, long), it contains bounding box (bbox) information and I can print it. But this information (point coordinate) is in the tweet field. Is there any way to introduce this into a single dictionary (tweet_dict.update)?

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants