1
Fork 0
mirror of https://github.com/thegeneralist01/twitter-openapi synced 2026-01-11 07:30:37 +01:00
Signed-off-by: ふぁ <yuki@yuki0311.com>
This commit is contained in:
ふぁ 2023-07-30 03:17:47 +09:00
parent 976f6ffb14
commit 86315f25a0
No known key found for this signature in database
GPG key ID: 83A8A5E74872A8AA
16 changed files with 209 additions and 14 deletions

99
.github/workflows/test-python.yaml vendored Normal file
View file

@ -0,0 +1,99 @@
name: test-python
on:
# auto review
pull_request:
paths-ignore:
- "test/**"
- ".github/**"
branches:
- master
env:
TWITTER_SESSION: ${{secrets.TWITTER_SESSION}}
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Openapi Generator Setup
- name: Get OpenJDK 11
uses: actions/setup-java@v2
with:
distribution: "temurin"
java-version: 11
- name: Get Java version
run: java -version
- name: Get Openapi Generator cache file
id: openapi-generator-cache
run: echo "file=openapi-generator-cli.jar" >> $GITHUB_OUTPUT
- name: Openapi Generator cache
uses: actions/cache@v3
with:
path: ${{ steps.openapi-generator-cache.outputs.file }}
key: ${{ runner.os }}-openapi-generator-${{ hashFiles('openapi-generator-cli.jar') }}
restore-keys: |
${{ runner.os }}-openapi-generator-
- name: Get Openapi Generator
run: |
wget https://github.com/fa0311/openapi-generator/releases/download/twitter_openapi_python_1.2.0/openapi-generator-cli.jar -O openapi-generator-cli.jar --no-verbose --show-progress --no-clobber
# Python Setup
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
architecture: "x64"
- name: Get Python version
run: python -V
- name: Get pip cache dir
id: pip-cache
run: echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: pip cache
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# generate
- name: Build twitter-openapi
run: |
python tools/build.py
# clean up dependencies
- name: Clean up dependencies
run: |
pip uninstall -y -r requirements.txt
# generate
- name: Generate Python client
run: |
java -jar openapi-generator-cli.jar generate -c test/python/openapi-generator-config.yaml -g python
# test
- name: Test Python client
run: |
pip install ./python_generated
- name: Run Python client test
run: |
python test/python/test_serialize.py
env:
TWITTER_SESSION: ${{secrets.TWITTER_SESSION}}

5
.gitignore vendored
View file

@ -1,2 +1,5 @@
.venv .venv
__pycache__/ __pycache__/
cookie.json
*_generated
*.jar

View file

@ -4,6 +4,6 @@
"yaml.validate": true, "yaml.validate": true,
"yaml.hover": true, "yaml.hover": true,
"yaml.schemas": { "yaml.schemas": {
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json": "twitter-openapi/**/*.yaml" "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json": "src/**/*.yaml"
} }
} }

View file

@ -15,7 +15,6 @@ components:
UserResultByScreenName: UserResultByScreenName:
properties: properties:
id: id:
pattern: ^[0-9a-zA-Z]+$
type: string type: string
result: result:
$ref: '#/components/schemas/UserResultByScreenNameResult' $ref: '#/components/schemas/UserResultByScreenNameResult'

View file

@ -44,7 +44,7 @@ paths:
/graphql/{pathQueryId}/Favoriters: /graphql/{pathQueryId}/Favoriters:
get: get:
description: get tweet favoriters description: get tweet favoriters
operationId: getTweetFavoriters operationId: getFavoriters
parameters: parameters:
- in: path - in: path
name: pathQueryId name: pathQueryId
@ -128,7 +128,7 @@ paths:
/graphql/{pathQueryId}/Retweeters: /graphql/{pathQueryId}/Retweeters:
get: get:
description: get tweet retweeters description: get tweet retweeters
operationId: getTweetRetweeters operationId: getRetweeters
parameters: parameters:
- in: path - in: path
name: pathQueryId name: pathQueryId

View file

@ -60,7 +60,6 @@ components:
ModuleItem: ModuleItem:
properties: properties:
entryId: entryId:
pattern: ^[a-z\-]+[0-9]+$
type: string type: string
item: item:
$ref: '#/components/schemas/ModuleEntry' $ref: '#/components/schemas/ModuleEntry'

View file

@ -15,7 +15,6 @@ components:
UserResultByScreenName: UserResultByScreenName:
properties: properties:
id: id:
pattern: ^[0-9a-zA-Z]+$
type: string type: string
result: result:
$ref: '#/components/schemas/UserResultByScreenNameResult' $ref: '#/components/schemas/UserResultByScreenNameResult'

View file

@ -44,7 +44,7 @@ paths:
/graphql/{pathQueryId}/Favoriters: /graphql/{pathQueryId}/Favoriters:
get: get:
description: get tweet favoriters description: get tweet favoriters
operationId: getTweetFavoriters operationId: getFavoriters
parameters: parameters:
- in: path - in: path
name: pathQueryId name: pathQueryId
@ -223,7 +223,7 @@ paths:
/graphql/{pathQueryId}/Retweeters: /graphql/{pathQueryId}/Retweeters:
get: get:
description: get tweet retweeters description: get tweet retweeters
operationId: getTweetRetweeters operationId: getRetweeters
parameters: parameters:
- in: path - in: path
name: pathQueryId name: pathQueryId

View file

@ -60,7 +60,6 @@ components:
ModuleItem: ModuleItem:
properties: properties:
entryId: entryId:
pattern: ^[a-z\-]+[0-9]+$
type: string type: string
item: item:
$ref: '#/components/schemas/ModuleEntry' $ref: '#/components/schemas/ModuleEntry'

View file

@ -39,7 +39,7 @@ components:
properties: properties:
id: id:
type: string type: string
pattern: "^[0-9a-zA-Z]+$" # pattern: "^[0-9a-zA-Z]+$"
result: result:
$ref: "#/components/schemas/UserResultByScreenNameResult" $ref: "#/components/schemas/UserResultByScreenNameResult"

View file

@ -20,7 +20,7 @@ paths:
/graphql/{pathQueryId}/Favoriters: /graphql/{pathQueryId}/Favoriters:
get: get:
operationId: getTweetFavoriters operationId: getFavoriters
description: get tweet favoriters description: get tweet favoriters
responses: responses:
"200": "200":
@ -34,7 +34,7 @@ paths:
/graphql/{pathQueryId}/Retweeters: /graphql/{pathQueryId}/Retweeters:
get: get:
operationId: getTweetRetweeters operationId: getRetweeters
description: get tweet retweeters description: get tweet retweeters
responses: responses:
"200": "200":

View file

@ -95,7 +95,7 @@ components:
properties: properties:
entryId: entryId:
type: string type: string
pattern: '^[a-z\-]+[0-9]+$' # pattern: '^[a-z\-]+[0-9]+$'
item: item:
$ref: "#/components/schemas/ModuleEntry" $ref: "#/components/schemas/ModuleEntry"

6
test/local.sh Normal file
View file

@ -0,0 +1,6 @@
python3 tools/build.py
java -jar openapi-generator-cli.jar generate -c test/python/openapi-generator-config.yaml -g python
python3 -m pip install ./python_generated
python3 test/python/test_serialize.py

12
test/python/login.py Normal file
View file

@ -0,0 +1,12 @@
import json
from tweepy_authlib import CookieSessionUserHandler
auth_handler = CookieSessionUserHandler(
screen_name=input("screen_name: "),
password=input("password: "),
)
cookies = auth_handler.get_cookies()
print(json.dumps(cookies.get_dict()))

View file

@ -0,0 +1,8 @@
inputSpec: dist/compatible/openapi-3.0.yaml
outputDir: python_generated
packageName: python_generated
packageVersion: 0.0.1
projectName: python_generated
useOneOfDiscriminatorLookup: true

View file

@ -0,0 +1,71 @@
import json
import os
import logging
import python_generated as pt
from pathlib import Path
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger("test_serialize")
if Path("cookie.json").exists():
with open("cookie.json", "r") as f:
cookies = json.load(f)
else:
cookies = json.loads(os.environ["TWITTER_SESSION"])
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 getKey(snake_str):
components = snake_str.split("_")
return "".join(x.title() for x in components[1:])
api_conf = pt.Configuration(
api_key={
"ClientLanguage": "en",
"ActiveUser": "yes",
"AuthType": "OAuth2Session",
"CsrfToken": cookies["ct0"],
},
)
api_conf.access_token = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
api_client = pt.ApiClient(configuration=api_conf, cookie=cookies_str)
api_client.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
error_count = 0
for x in [pt.DefaultApi, pt.TweetApi, pt.UserApi, pt.UserListApi]:
for props, fn in x.__dict__.items():
if not callable(fn):
continue
if props.startswith("__") or props.endswith("_with_http_info"):
continue
key = getKey(props)
logger.info(f"Try: {key}")
kwargs = {"path_query_id": placeholder[key]["queryId"]}
if placeholder[key].get("variables") is not None:
kwargs["variables"] = json.dumps(placeholder[key]["variables"])
if placeholder[key].get("features") is not None:
kwargs["features"] = json.dumps(placeholder[key]["features"])
if placeholder[key].get("fieldToggles") is not None:
kwargs["field_toggles"] = json.dumps(placeholder[key]["fieldToggles"])
try:
res = getattr(x(api_client), props)(**kwargs)
except Exception as e:
logger.error(f"{key}")
logger.error(e)
error_count += 1
if error_count > 0:
exit(1)