from dataclasses import dataclass
@dataclass
class TwitterTweet:
rest_id: str
user_id: str
full_text: str
created_at: str
retweet_count: int
favorite_count: int
reply_count: int
lang: str
in_reply_to_status_id_str: str
hashtags: list[str]
user_mentions: list[str]
urls: list[str]
sentiment: str = ''
@classmethod
def from_payload(cls, payload):
legacy_data = payload.get('legacy', {})
entities_data = legacy_data.get('entities', {})
return cls(rest_id=payload.get('rest_id', ''),
user_id=legacy_data.get('user_id_str', ''),
full_text=legacy_data.get('full_text', ''),
created_at=legacy_data.get('created_at', ''),
retweet_count=legacy_data.get('retweet_count', 0),
favorite_count=legacy_data.get('favorite_count', 0),
reply_count=legacy_data.get('reply_count', 0),
lang=legacy_data.get('lang', ''),
in_reply_to_status_id_str=legacy_data.get(
'in_reply_to_status_id_str', ''),
hashtags=[
tag['text']
for tag in entities_data.get('hashtags', [])
],
user_mentions=[
mention['id_str']
for mention in entities_data.get('user_mentions', [])
],
urls=[
url['expanded_url']
for url in entities_data.get('urls', [])
])
from dataclasses import dataclass
@dataclass
class TwitterUser:
id: str
name: str
screen_name: str
statuses_count: int
followers_count: int
friends_count: int
favourites_count: int
listed_count: int
default_profile: bool
default_profile_image: bool
location: str
description: str
description_has_url: bool
description_url: str
followers_to_following_ratio: float
verified_type: str
verified: bool
is_blue_verified: bool
has_graduated_access: bool
can_dm: bool
media_count: int
has_custom_timelines: bool
has_verification_info: bool
possibly_sensitive: bool
@classmethod
def from_payload(cls, payload):
legacy_data = payload.get('legacy', {})
urls = [
url['expanded_url'] for url in legacy_data.get('entities', {}).get(
'description', {}).get('urls', [])
]
followers_count = legacy_data.get('followers_count', 0)
friends_count = legacy_data.get('friends_count', 0)
return cls(
id=payload.get('rest_id', ''),
name=legacy_data.get('name', ''),
screen_name=legacy_data.get('screen_name', ''),
statuses_count=legacy_data.get('statuses_count', 0),
followers_count=followers_count,
friends_count=friends_count,
favourites_count=legacy_data.get('favourites_count', 0),
listed_count=legacy_data.get('listed_count', 0),
default_profile=legacy_data.get('default_profile', False),
default_profile_image=legacy_data.get('default_profile_image',
False),
location=legacy_data.get('location', ''),
description=legacy_data.get('description', ''),
description_has_url=bool(urls),
description_url=','.join(urls) if urls else '',
followers_to_following_ratio=followers_count /
friends_count if friends_count != 0 else 0,
verified_type=legacy_data.get('verified_type', ''),
verified=legacy_data.get('verified', False),
is_blue_verified=payload.get('is_blue_verified', False),
has_graduated_access=payload.get('has_graduated_access', False),
can_dm=legacy_data.get('can_dm', False),
media_count=legacy_data.get('media_count', 0),
has_custom_timelines=legacy_data.get('has_custom_timelines',
False),
has_verification_info=payload.get('verification_info', ''),
possibly_sensitive=legacy_data.get('possibly_sensitive', False),
)