-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathapi.py
More file actions
54 lines (40 loc) · 1.7 KB
/
api.py
File metadata and controls
54 lines (40 loc) · 1.7 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
import json
import logging
from pathlib import Path
from typing import Optional
import twitter_openapi_python as api
def get_client() -> api.TwitterOpenapiPythonClient:
if Path("cookie.json").exists():
with open("cookie.json", "r") as f:
cookies_dict = json.load(f)
if isinstance(cookies_dict, list):
cookies_dict = {k["name"]: k["value"] for k in cookies_dict}
else:
raise Exception("cookie.json not found")
client = api.TwitterOpenapiPython()
client.additional_api_headers = {
"sec-ch-ua-platform": '"Windows"',
}
client.additional_browser_headers = {
"sec-ch-ua-platform": '"Windows"',
}
return client.get_client_from_cookies(cookies_dict)
def get_guest_client() -> api.TwitterOpenapiPythonClient:
client = api.TwitterOpenapiPython().get_guest_client()
return client
def print_tweet(tweet: api.TweetApiUtilsData) -> None:
print_legacy_tweet(tweet.user.legacy, tweet.tweet.legacy)
for reply in tweet.replies:
print_legacy_tweet(reply.user.legacy, reply.tweet.legacy)
def print_legacy_tweet(u: api.UserLegacy, t: Optional[api.TweetLegacy]) -> None:
text = f"{u.screen_name.rjust(20)}: {t.full_text if t else 'Deleted Tweet'}"
logging.info(text.replace("\n", " "))
def print_user(user: api.UserApiUtilsData) -> None:
print_legacy_user(user.user.legacy)
def print_legacy_user(u: api.UserLegacy) -> None:
logging.info(u.screen_name)
logging.info(f"listedCount: {u.listed_count}")
logging.info(f"followedBy: {u.followed_by} following: {u.following}")
text = f"friends_count: {u.friends_count} followers_count: {u.followers_count}"
logging.info(text)
logging.info("┄" * 50)