Browse Source

switch to primarily aiohttp instead of requests

master
Purpur 4 years ago
committed by GitHub
parent
commit
788cff0d3a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      bot.py

45
bot.py

@ -62,13 +62,16 @@ async def on_message(message):
'Authorization': 'Bearer ' + message.content, 'Authorization': 'Bearer ' + message.content,
} }
response = requests.get(url, headers=headers, cookies=cookies)
# response = requests.get(url, headers=headers, cookies=cookies)
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, cookies=cookies) as response:
# If API token is verified to be correct: # If API token is verified to be correct:
if str(response) == "<Response [200]>":
if response.status == 200:
# Formats response of account in JSON format # Formats response of account in JSON format
json_response = response.json()
json_response = await response.json()
# Loads contents of users.json # Loads contents of users.json
file = open('users.json', 'r') file = open('users.json', 'r')
@ -108,13 +111,14 @@ async def on_message(message):
'Authorization': 'Bearer ' + message.content, 'Authorization': 'Bearer ' + message.content,
} }
response = requests.get(url, headers=headers, cookies=cookies)
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, cookies=cookies) as response:
# If API token is verified to be correct, continues # If API token is verified to be correct, continues
if str(response) == "<Response [200]>":
if response.status == 200:
# Formats response for servers in JSON format # Formats response for servers in JSON format
servers_json_response = response.json()
servers_json_response = await response.json()
user_client = False user_client = False
user_subuser = False user_subuser = False
@ -190,8 +194,11 @@ async def on_message(message):
if message.attachments[0].url.endswith( if message.attachments[0].url.endswith(
('.png', '.jpg', '.jpeg', '.mp4', '.mov', '.avi', '.gif', '.image')) == False: ('.png', '.jpg', '.jpeg', '.mp4', '.mov', '.avi', '.gif', '.image')) == False:
download = message.attachments[0].url download = message.attachments[0].url
r = requests.get(download, allow_redirects=True)
text = r.text
async with aiohttp.ClientSession() as session:
async with session.get(download, allow_redirects=True) as r:
# r = requests.get(download, allow_redirects=True)
text = await r.text()
text = "\n".join(text.splitlines()) text = "\n".join(text.splitlines())
if '' not in text: # If it's not an image/gif if '' not in text: # If it's not an image/gif
truncated = False truncated = False
@ -294,12 +301,11 @@ async def updater():
'Accept': 'application/json', 'Accept': 'application/json',
'Authorization': 'Bearer ' + api_key, 'Authorization': 'Bearer ' + api_key,
} }
response = requests.get(url, headers=headers, cookies=cookies)
# If API token is verified to be correct, continues
if str(response) == "<Response [200]>":
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, cookies=cookies) as response:
if response.status == 200:
# Formats response for servers in JSON format # Formats response for servers in JSON format
servers_json_response = response.json()
servers_json_response = await response.json()
user_client = False user_client = False
user_subuser = False user_subuser = False
@ -371,8 +377,10 @@ async def updater():
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': 'Bearer ' + application_api_key, 'Authorization': 'Bearer ' + application_api_key,
} }
response = requests.get(url, headers=headers, cookies=cookies)
servers_json_response = response.json()
#response = requests.get(url, headers=headers, cookies=cookies)
async with aiohttp.ClientSession() as session:
async with session.get(url, headers = headers, cookies = cookies) as response:
servers_json_response = await response.json()
file = open('modified_servers.json', 'r') file = open('modified_servers.json', 'r')
modified_servers = json.load(file) modified_servers = json.load(file)
@ -402,11 +410,10 @@ async def updater():
server['attributes']['feature_limits']['databases']) + ', "allocations": ' + str( server['attributes']['feature_limits']['databases']) + ', "allocations": ' + str(
server['attributes']['feature_limits']['allocations']) + ', "backups": 3 } }' server['attributes']['feature_limits']['allocations']) + ', "backups": 3 } }'
response = requests.patch(
'https://panel.birdflop.com/api/application/servers/' + str(server['attributes']['id']) + '/build',
headers=headers, cookies=cookies, data=data)
if (str(response)) == "<Response [200]>":
async with aiohttp.ClientSession() as session:
async with session.patch('https://panel.birdflop.com/api/application/servers/' + str(server['attributes']['id']) + '/build', headers=headers, cookies=cookies, data=data) as response:
if response.status == 200:
modified_servers['servers'].append({ modified_servers['servers'].append({
'uuid': str(server['attributes']['uuid']) 'uuid': str(server['attributes']['uuid'])
}) })

Loading…
Cancel
Save