From ae885317a1d0a524c0821eb8f8ef6bccbb381b84 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Wed, 3 Nov 2021 16:33:54 -0400 Subject: [PATCH] [+] Create function to get user tweets --- collect/twitter.py | 22 ++++++++++++++++++++++ collect/twitter_individual.py | 5 +++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/collect/twitter.py b/collect/twitter.py index b400778..a4c355c 100644 --- a/collect/twitter.py +++ b/collect/twitter.py @@ -1,8 +1,13 @@ +import json from dataclasses import dataclass from datetime import datetime from typing import Union +import demjson as demjson +import json5 import tweepy +from tweepy import API +from tweepy.models import Status from collect.utils import Config @@ -67,4 +72,21 @@ def tweepy_login(conf: Config) -> tweepy.API: return api +def get_user_tweets(api: API, screen_name: str) -> list[Tweet]: + """ + Get all tweets from a specific individual + :param api: Tweepy API object + :param screen_name: Screen name of that individual + :return: All tweets + """ + tweets = api.user_timeline(screen_name=screen_name, count=200, tweet_mode='extended') + # parsed_tweets: list[Tweet] = [] + # + # for tweet in tweets: + # json_str = str(tweet._json).replace("\"", "\\\"").replace('\'', '\"') + # print(tweet._json) + # obj = demjson.decode(str(tweet._json)) + # parsed_tweets.append(Tweet(**obj)) + + return tweets diff --git a/collect/twitter_individual.py b/collect/twitter_individual.py index 79cb412..dc61c42 100644 --- a/collect/twitter_individual.py +++ b/collect/twitter_individual.py @@ -4,6 +4,7 @@ import json5 import tweepy from tweepy.models import Status +from collect.twitter import tweepy_login, get_user_tweets from collect.utils import * @@ -11,8 +12,8 @@ if __name__ == '__main__': conf = load_config() api = tweepy_login(conf) - tweets: list[Status] = api.user_timeline(screen_name='voxdotcom', count=200, tweet_mode = 'extended') + tweets = get_user_tweets(api, 'voxdotcom') for tweet in tweets: - print(tweet._json) + print(tweet)