Skip to content

Make the _get_gql_ops.py way faster by keeping connection alive #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions _get_gql_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,30 @@ async def get_scripts():
]

scripts = []
for i, x in enumerate(urls, 1):
cache_path = os.path.join(cache_dir, x.split("/")[-1].split("?")[0])
if os.path.exists(cache_path):
with open(cache_path) as fp:
scripts.append(fp.read())
continue

print(f"({i:3d} / {len(urls):3d}) {x}")
rep = await httpx.AsyncClient().get(x)
rep.raise_for_status()

with open(cache_path, "w") as fp:
fp.write(rep.text)
scripts.append(rep.text)
async with httpx.AsyncClient() as clit:
for i, x in enumerate(urls, 1):
cache_path = os.path.join(cache_dir, x.split("/")[-1].split("?")[0])
if os.path.exists(cache_path):
with open(cache_path) as fp:
scripts.append(fp.read())
continue

print(f"({i:3d} / {len(urls):3d}) {x}")
rep = await clit.get(x)
rep.raise_for_status()

with open(cache_path, "w") as fp:
fp.write(rep.text)
scripts.append(rep.text)

return scripts


async def main():
with open("./twscrape/api.py") as fp:

with open("./twscrape/api.py") as fp:
ops = [x.strip() for x in fp.read().split("\n")]
ops = [x.split("=")[0].removeprefix("OP_").strip() for x in ops if x.startswith("OP_")]

all_pairs = {}
for txt in await get_scripts():
pairs = re.findall(r'queryId:"(.+?)".+?operationName:"(.+?)"', txt)
Expand Down