-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
143 lines (125 loc) · 4.27 KB
/
Copy pathmodels.py
File metadata and controls
143 lines (125 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel
def _location_str(loc) -> str:
if isinstance(loc, dict):
return loc.get("location") or ""
return loc or ""
class UserOut(BaseModel):
id: str
username: str
fullname: str
bio: str = ""
location: str = ""
url: str = ""
userPic: str = ""
banner: str = ""
followers: int = 0
following: int = 0
tweets: int = 0
likes: int = 0
media: int = 0
listed: int = 0
verified: bool = False
blue: bool = False
protected: bool = False
joinDate: Optional[datetime] = None
pinnedTweetIds: List[str] = []
@classmethod
def from_twscrape(cls, u) -> "UserOut":
return cls(
id=u.id_str,
username=u.username,
fullname=u.displayname or "",
bio=u.rawDescription or "",
location=_location_str(u.location),
url=u.url or "",
userPic=u.profileImageUrl or "",
banner=u.profileBannerUrl or "",
followers=u.followersCount or 0,
following=u.friendsCount or 0,
tweets=u.statusesCount or 0,
likes=u.favouritesCount or 0,
media=u.mediaCount or 0,
listed=u.listedCount or 0,
verified=bool(u.verified),
blue=bool(u.blue),
protected=bool(u.protected),
joinDate=u.created,
pinnedTweetIds=[str(i) for i in (u.pinnedIds or [])],
)
class MediaOut(BaseModel):
kind: str
url: str
thumb: str = ""
altText: str = ""
class TweetOut(BaseModel):
id: str
url: str
user: UserOut
text: str = ""
lang: str = ""
date: Optional[datetime] = None
replies: int = 0
retweets: int = 0
likes: int = 0
quotes: int = 0
views: int = 0
bookmarks: int = 0
conversationId: Optional[str] = None
inReplyToTweetId: Optional[str] = None
inReplyToUsername: Optional[str] = None
hashtags: List[str] = []
cashtags: List[str] = []
mentions: List[str] = []
links: List[str] = []
media: List[MediaOut] = []
quote: Optional["TweetOut"] = None
retweet: Optional["TweetOut"] = None
@classmethod
def from_twscrape(cls, t) -> "TweetOut":
media: List[MediaOut] = []
if t.media:
for p in t.media.photos or []:
media.append(MediaOut(kind="photo", url=p.url, altText=getattr(p, "altText", "") or ""))
for v in t.media.videos or []:
best = ""
if getattr(v, "variants", None):
best = max(v.variants, key=lambda x: (getattr(x, "bitrate", 0) or 0)).url
media.append(MediaOut(kind="video", url=best or "", thumb=getattr(v, "thumbnailUrl", "") or ""))
for g in t.media.animated or []:
media.append(MediaOut(kind="gif", url=g.videoUrl or "", thumb=getattr(g, "thumbnailUrl", "") or ""))
return cls(
id=t.id_str,
url=t.url,
user=UserOut.from_twscrape(t.user),
text=t.rawContent or "",
lang=t.lang or "",
date=t.date,
replies=t.replyCount or 0,
retweets=t.retweetCount or 0,
likes=t.likeCount or 0,
quotes=t.quoteCount or 0,
views=t.viewCount or 0,
bookmarks=t.bookmarkedCount or 0,
conversationId=t.conversationIdStr,
inReplyToTweetId=t.inReplyToTweetIdStr,
inReplyToUsername=t.inReplyToScreenName,
hashtags=[h if isinstance(h, str) else h.text for h in (t.hashtags or [])],
cashtags=[c if isinstance(c, str) else c.text for c in (t.cashtags or [])],
mentions=[m.username for m in (t.mentionedUsers or [])],
links=[l if isinstance(l, str) else l.url for l in (t.links or [])],
media=media,
quote=cls.from_twscrape(t.quotedTweet) if t.quotedTweet else None,
retweet=cls.from_twscrape(t.retweetedTweet) if t.retweetedTweet else None,
)
TweetOut.model_rebuild()
class TimelinePage(BaseModel):
user: UserOut
tweets: List[TweetOut]
count: int
class SearchResponse(BaseModel):
kind: str = "tweets"
query: str
tweets: List[TweetOut]
count: int