43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import requests
|
|
import json
|
|
import os.path
|
|
import logging
|
|
|
|
LOGGER=logging.getLogger(__name__)
|
|
|
|
def download(url, path):
|
|
if os.path.isfile(path):
|
|
return path
|
|
|
|
try:
|
|
req = requests.get(url, stream=True)
|
|
if req.status_code == 200:
|
|
dirname = os.path.dirname(path)
|
|
if not os.path.isdir(dirname):
|
|
os.makedirs(dirname, exist_ok=True)
|
|
with open(f'{path}.part', 'wb') as output:
|
|
for chunk in req.iter_content(1024):
|
|
output.write(chunk)
|
|
|
|
os.replace(f'{path}.part', path)
|
|
return path
|
|
LOGGER.warning("%s: %d", url, req.status_code)
|
|
except Exception as e:
|
|
LOGGER.exception("Error downloading %s", url)
|
|
return None
|
|
|
|
def extract_winners(url):
|
|
data = requests.get(url).json()
|
|
|
|
for key, fight in reversed(data.items()):
|
|
for entry in fight['entries']:
|
|
if entry.get('winner'):
|
|
url = entry.get('url')
|
|
path = download(url, f'winners/{key}/{os.path.basename(url)}')
|
|
if path:
|
|
print(path)
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(level=logging.INFO)
|
|
extract_winners('https://songfight.org/fights.json')
|