[scripts] rewrite Python scripts to not use the requests module

This commit is contained in:
Dmytro Meleshko 2021-03-18 20:25:15 +02:00
parent 35b1e9446e
commit 173bf5851e
2 changed files with 21 additions and 15 deletions

View file

@ -3,9 +3,9 @@
import sys import sys
import os import os
from configparser import ConfigParser from configparser import ConfigParser
import requests
import json import json
import urllib.parse import urllib.parse
import urllib.request
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "script-resources")) sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "script-resources"))
@ -48,9 +48,8 @@ def emote_downloader_and_iterator():
with open(registry_dump_file, "r") as f: with open(registry_dump_file, "r") as f:
emote_registry_data = json.load(f) emote_registry_data = json.load(f)
except FileNotFoundError: except FileNotFoundError:
response = requests.get(registry_dump_url, timeout=10) with urllib.request.urlopen(registry_dump_url, timeout=10) as response:
response.raise_for_status() emote_registry_data = json.load(response)
emote_registry_data = response.json()
assert emote_registry_data["version"] == 1 assert emote_registry_data["version"] == 1

View file

@ -6,10 +6,13 @@
import sys import sys
import os import os
import requests import urllib.request
import urllib.error
import colorama import colorama
import time import time
import argparse import argparse
import json
import typing
DISCORD_EPOCH = 1420070400000 # milliseconds DISCORD_EPOCH = 1420070400000 # milliseconds
@ -52,19 +55,23 @@ if not (image_size is None or (image_size > 0 and image_size & (image_size - 1))
parser.error("image_size must be greater than zero and a power of two") parser.error("image_size must be greater than zero and a power of two")
# no timeout here, sadly, due to this genius: <https://github.com/psf/requests/issues/3099#issuecomment-215522806>
response = requests.get(
"https://discordapp.com/api/users/{}".format(user_snowflake),
headers={"Authorization": "Bot {}".format(bot_token)},
timeout=3,
)
try: try:
response.raise_for_status() opener = urllib.request.build_opener()
except requests.HTTPError as err: # Don't send the User-Agent header, Discord blocks the default one
print(response.json(), file=sys.stderr) opener.addheaders = []
with opener.open(
urllib.request.Request(
"http://discord.com/api/users/{}".format(user_snowflake),
headers={"Authorization": "Bot {}".format(bot_token)},
),
timeout=10,
) as response:
raw_data = json.load(response)
except urllib.error.HTTPError as err:
print(err, file=sys.stderr)
print(err.read(), file=sys.stderr)
raise err raise err
raw_data = response.json()
data = {} data = {}
data["ID"] = raw_data["id"] data["ID"] = raw_data["id"]