From b4f8bd97347f9c9be57cb8343a6d05ee6cc684f7 Mon Sep 17 00:00:00 2001 From: Peace Munyai <47421951+PeaceTheeCoder@users.noreply.github.com> Date: Sun, 25 Sep 2022 12:20:03 +0200 Subject: [PATCH] Create tweets.py I have added the code that I have used to only get tweeter feeds from a single page, via a username. --- get_tweets_from_page/tweets.py | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 get_tweets_from_page/tweets.py diff --git a/get_tweets_from_page/tweets.py b/get_tweets_from_page/tweets.py new file mode 100644 index 0000000..8fba60e --- /dev/null +++ b/get_tweets_from_page/tweets.py @@ -0,0 +1,64 @@ +import tweepy +import json +from datetime import datetime +from datetime import timedelta + +def main(): + bearer_token = "Your own tocken" + + client = tweepy.Client(bearer_token=bearer_token) + + response = client.search_recent_tweets(query="from:googleafrica -is:retweet", media_fields=['preview_image_url', 'url'], user_fields=['profile_image_url','verified'], tweet_fields=['created_at','lang'], expansions=['author_id','attachments.media_keys']) + + users = {u['id']: u for u in response.includes['users']} + media = {m["media_key"]: m for m in response.includes['media']} + + tweets_json_arr = [] + + for tweet in response.data: + id = tweet.id + text = tweet.text + + date = tweet.created_at + + username = None + name = None + profile_pic = None + verified = False + media_pic = None + + if users[tweet.author_id]: + user = users[tweet.author_id] + + username = user.username + name = user.name + profile_pic = user.profile_image_url + verified = user.verified + + try: + media_keys = tweet.data['attachments']['media_keys'] + if media[media_keys[0]].url: + media_pic = media[media_keys[0]].url + except: + pass + + tweet_obj = { + "id": id, + "name": name, + "username" : username, + "profilePic" : profile_pic, + "verified": verified, + + "text": text, + "date": date, + "photo": media_pic + } + + tweets_json_arr.append(tweet_obj) + + + jsoned_tweets = json.dumps(tweets_json_arr) + return jsoned_tweets + + +