1
Fork 0
mirror of https://github.com/thegeneralist01/twitter-openapi synced 2026-01-11 23:50:26 +01:00

Merge branch 'dev' of github.com:fa0311/twitter-openapi into dev

Signed-off-by: ふぁ <yuki@yuki0311.com>

Signed-off-by: ふぁ <yuki@yuki0311.com>
This commit is contained in:
ふぁ 2023-09-14 13:55:05 +09:00
commit 49bfd77799
No known key found for this signature in database
GPG key ID: 83A8A5E74872A8AA
4 changed files with 117 additions and 99 deletions

View file

@ -71,6 +71,8 @@ components:
type: type:
enum: enum:
- photo - photo
- video
- animated_gif
type: string type: string
url: url:
format: uri format: uri
@ -83,7 +85,6 @@ components:
- media_url_https - media_url_https
- type - type
- url - url
- features
- sizes - sizes
- original_info - original_info
MediaExtended: MediaExtended:
@ -139,7 +140,6 @@ components:
- type - type
- url - url
- ext_media_availability - ext_media_availability
- features
- sizes - sizes
- original_info - original_info
MediaOriginalInfo: MediaOriginalInfo:

View file

@ -71,6 +71,8 @@ components:
type: type:
enum: enum:
- photo - photo
- video
- animated_gif
type: string type: string
url: url:
format: uri format: uri
@ -83,7 +85,6 @@ components:
- media_url_https - media_url_https
- type - type
- url - url
- features
- sizes - sizes
- original_info - original_info
MediaExtended: MediaExtended:
@ -139,7 +140,6 @@ components:
- type - type
- url - url
- ext_media_availability - ext_media_availability
- features
- sizes - sizes
- original_info - original_info
MediaOriginalInfo: MediaOriginalInfo:

View file

@ -325,7 +325,7 @@ components:
- "media_url_https" - "media_url_https"
- "type" - "type"
- "url" - "url"
- "features" # - "features"
- "sizes" - "sizes"
- "original_info" - "original_info"
properties: properties:
@ -347,7 +347,7 @@ components:
format: uri format: uri
type: type:
type: string type: string
enum: [photo] enum: [photo, video, animated_gif]
url: url:
type: string type: string
format: uri format: uri
@ -378,7 +378,7 @@ components:
- "type" - "type"
- "url" - "url"
- "ext_media_availability" - "ext_media_availability"
- "features" # - "features"
- "sizes" - "sizes"
- "original_info" - "original_info"
properties: properties:

View file

@ -7,6 +7,8 @@ from pathlib import Path
import time import time
import glob import glob
import aenum import aenum
import concurrent.futures
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s") logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger("test_serialize") logger = logging.getLogger("test_serialize")
@ -17,22 +19,6 @@ SLEEP_TIME = float(os.environ.get("SLEEP", "0"))
CUESOR_TEST_COUNT = int(os.environ.get("CUESOR_TEST_COUNT", "3")) CUESOR_TEST_COUNT = int(os.environ.get("CUESOR_TEST_COUNT", "3"))
STRICT_MODE = os.environ.get("STRICT_MODE", "false").lower() == "true" STRICT_MODE = os.environ.get("STRICT_MODE", "false").lower() == "true"
if Path("cookie.json").exists():
with open("cookie.json", "r") as f:
cookies = json.load(f)
elif TWITTER_SESSION is not None:
data = base64.b64decode(TWITTER_SESSION).decode("utf-8")
cookies = json.loads(data)
else:
commands = ["python -m pip install tweepy_authlib", "python tools/login.py"]
raise Exception(f'cookie.json not found. Please run `{"; ".join(commands)}` first.')
cookies_str = "; ".join([f"{k}={v}" for k, v in cookies.items()])
with open("src/config/placeholder.json", "r") as f:
placeholder = json.load(f)
def get_key(snake_str): def get_key(snake_str):
components = snake_str.split("_") components = snake_str.split("_")
@ -120,15 +106,15 @@ def save_cache(data):
json.dump(data, f, indent=4) json.dump(data, f, indent=4)
# ====== Load cache ====== def task_callback(file):
for file in glob.glob("cache/*.json"): try:
with open(file, "r") as f: with open(file, "r") as f:
cache = json.load(f) cache = json.load(f)
data = pt.__dict__[cache["type"]].from_json(cache["raw"]) data = pt.__dict__[cache["type"]].from_json(cache["raw"])
rate = match_rate(data.to_dict(), json.loads(cache["raw"])) rate = match_rate(data.to_dict(), json.loads(cache["raw"]))
logger.info(f"Match rate: {rate}") return f"Match rate: {rate}"
except Exception as e:
logger.info(f"Success: {len(glob.glob('cache/*.json'))}") return f"Error: {e} {file}"
def error_dump(e): def error_dump(e):
@ -142,6 +128,31 @@ def error_dump(e):
logger.info("================================") logger.info("================================")
if __name__ == "__main__":
if Path("cookie.json").exists():
with open("cookie.json", "r") as f:
cookies = json.load(f)
elif TWITTER_SESSION is not None:
data = base64.b64decode(TWITTER_SESSION).decode("utf-8")
cookies = json.loads(data)
else:
commands = ["python -m pip install tweepy_authlib", "python tools/login.py"]
raise Exception(
f'cookie.json not found. Please run `{"; ".join(commands)}` first.'
)
cookies_str = "; ".join([f"{k}={v}" for k, v in cookies.items()])
with open("src/config/placeholder.json", "r") as f:
placeholder = json.load(f)
with concurrent.futures.ProcessPoolExecutor() as executor:
tasks = [executor.submit(task_callback, x) for x in glob.glob("cache/*.json")]
for task in concurrent.futures.as_completed(tasks):
logger.info(task.result())
logger.info(f"Success: {len(glob.glob('cache/*.json'))}")
api_conf = pt.Configuration( api_conf = pt.Configuration(
api_key={ api_key={
"ClientLanguage": "en", "ClientLanguage": "en",
@ -175,11 +186,18 @@ for x in [pt.DefaultApi, pt.TweetApi, pt.UserApi, pt.UsersApi, pt.UserListApi]:
cursor_history.add(cursor) cursor_history.add(cursor)
logger.info(f"Try: {key} {cursor}") logger.info(f"Try: {key} {cursor}")
kwargs = get_kwargs(key, {} if cursor is None else {"cursor": cursor}) kwargs = get_kwargs(
key, {} if cursor is None else {"cursor": cursor}
)
res: pt.ApiResponse = getattr(x(api_client), props)(**kwargs) res: pt.ApiResponse = getattr(x(api_client), props)(**kwargs)
data = res.data.to_dict() data = res.data.to_dict()
save_cache({"raw": res.raw_data, "type": res.data.__class__.__name__}) save_cache(
{
"raw": res.raw_data,
"type": res.data.__class__.__name__,
}
)
new_cursor = set(get_cursor(data, find_cursor)) - cursor_history new_cursor = set(get_cursor(data, find_cursor)) - cursor_history
cursor_list.update(new_cursor) cursor_list.update(new_cursor)
@ -199,7 +217,6 @@ for x in [pt.DefaultApi, pt.TweetApi, pt.UserApi, pt.UsersApi, pt.UserListApi]:
error_dump(e) error_dump(e)
error_count += 1 error_count += 1
try: try:
logger.info(f"Try: Self UserByScreenName Test") logger.info(f"Try: Self UserByScreenName Test")
kwargs = get_kwargs("UserByScreenName", {"screen_name": "a810810931931"}) kwargs = get_kwargs("UserByScreenName", {"screen_name": "a810810931931"})
@ -208,7 +225,8 @@ try:
rate = match_rate(data, json.loads(res.raw_data)) rate = match_rate(data, json.loads(res.raw_data))
logger.info(f"Match rate: {rate}") logger.info(f"Match rate: {rate}")
if not data["data"]["user"]["result"]["legacy"]["screen_name"] == "a810810931931": screen_name = data["data"]["user"]["result"]["legacy"]["screen_name"]
if not screen_name == "a810810931931":
raise Exception("UserByScreenName failed") raise Exception("UserByScreenName failed")
except Exception as e: except Exception as e:
error_dump(e) error_dump(e)