|
| 1 | +import json |
| 2 | +import os |
| 3 | +import uuid |
| 4 | +import requests |
| 5 | +import base64 |
| 6 | +import logging |
| 7 | +import urllib3 |
| 8 | +from logging import Logger |
| 9 | + |
| 10 | +urllib3.disable_warnings() |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | +logger: Logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +def compare_fingerprints(current, new): |
| 15 | + current_data = {(data['ident'], data['size'], data['date']) for data in current} |
| 16 | + new_data = {(data['ident'], data['size'], data['date']) for data in new} |
| 17 | + return new_data - current_data |
| 18 | + |
| 19 | +def copy_file(source, destination): |
| 20 | + with open(source, 'r', encoding='utf-8') as src_file: |
| 21 | + data = src_file.read() |
| 22 | + with open(destination, 'w', encoding='utf-8') as dest_file: |
| 23 | + dest_file.write(data) |
| 24 | + |
| 25 | +async def export_pictures(url, col, headers): |
| 26 | + payload = { |
| 27 | + "jsonrpc": "2.0", |
| 28 | + "method": col.get('method'), |
| 29 | + "params": col.get("params"), |
| 30 | + "id": str(uuid.uuid4()), |
| 31 | + } |
| 32 | + try: |
| 33 | + response = requests.post(url, json=payload, headers=headers, verify=False, timeout=10000) |
| 34 | + response.raise_for_status() |
| 35 | + if response.text == 'ko!': |
| 36 | + raise Exception("ko!") |
| 37 | + elif not response.json()['result']['output']: |
| 38 | + raise Exception("Empty response from ExportPhotos", response.json()) |
| 39 | + data = response.json()['result']['output'] |
| 40 | + os.makedirs(f'./cache', exist_ok=True) |
| 41 | + os.makedirs(f'./cache/pictures', exist_ok=True) |
| 42 | + |
| 43 | + if os.path.exists(f'./cache/pictures/taiga_{col.get("params")["type"]}.json'): |
| 44 | + logger.info(f'<./cache/pictures/taiga_{col.get("params")["type"]}.json> Already exists checking datas...') |
| 45 | + current = json.load(open(f'./cache/pictures/taiga_{col.get("params")["type"]}.json', 'r', encoding='utf-8')) |
| 46 | + compared = compare_fingerprints(current['data'], data[0][1]) |
| 47 | + |
| 48 | + if compared.__len__() > 0: |
| 49 | + logger.info(f'<./cache/pictures/taiga_{col.get("params")["type"]}.json> Already exists moving to .old file !') |
| 50 | + os.rename(f'./cache/pictures/taiga_{col.get("params")["type"]}.json', f'./cache/pictures/taiga_{col.get("params")["type"]}.json.old') |
| 51 | + else: |
| 52 | + logger.info(f'<./cache/pictures/taiga_{col.get("params")["type"]}.json> All datas are the same !') |
| 53 | + |
| 54 | + with open(f'./cache/pictures/taiga_{col.get("params")["type"]}.json', 'w', encoding='utf-8') as fichier: |
| 55 | + json.dump( |
| 56 | + { |
| 57 | + "type": data[0][0][0], |
| 58 | + "data": data[0][1], |
| 59 | + "total": data[0][0][1], |
| 60 | + }, |
| 61 | + fichier, |
| 62 | + ensure_ascii=False, |
| 63 | + indent=4, |
| 64 | + ) |
| 65 | + logger.info(f"{col.get('method')}") |
| 66 | + |
| 67 | + if os.path.exists(f'./cache/pictures/taiga_{col.get("params")["type"]}.json.old'): |
| 68 | + compare_now = compare_fingerprints( |
| 69 | + json.load(open(f'./cache/pictures/taiga_{col.get("params")["type"]}.json.old', 'r', encoding='utf-8'))['data'], |
| 70 | + json.load(open(f'./cache/pictures/taiga_{col.get("params")["type"]}.json', 'r', encoding='utf-8'))['data'], |
| 71 | + ) |
| 72 | + |
| 73 | + if compare_now.__len__() > 0: |
| 74 | + logger.info(f'<./cache/pictures/taiga_{col.get("params")["type"]}.json> Datas are different, starting exportation...') |
| 75 | + for picture in compare_now: |
| 76 | + await export_picture(url, col, headers, picture[0]) |
| 77 | + else: |
| 78 | + logger.info(f'<./cache/pictures/taiga_{col.get("params")["type"]}.json> Datas are the same, nothing to do !') |
| 79 | + else: |
| 80 | + logger.info(f'<./cache/pictures/taiga_{col.get("params")["type"]}.json> No old file found, starting exportation...') |
| 81 | + for picture in data[0][1]: |
| 82 | + await export_picture(url, col, headers, picture.get('ident')) |
| 83 | + |
| 84 | + copy_file(f'./cache/pictures/taiga_{col.get("params")["type"]}.json', f'./cache/pictures/taiga_{col.get("params")["type"]}.json.old') |
| 85 | + except requests.exceptions.HTTPError as e: |
| 86 | + logger.warning(f"Failed to insert {col.get('method')}: {e} \n {e.response.text}") |
| 87 | + |
| 88 | + |
| 89 | +async def export_picture(url, col, headers, id): |
| 90 | + payload = { |
| 91 | + "jsonrpc": "2.0", |
| 92 | + "method": col.get('methodBase64'), |
| 93 | + "params": { |
| 94 | + **col.get("paramsBase64"), |
| 95 | + "id": id.replace(col.get("paramsBase64").get('type') + '-', ''), |
| 96 | + }, |
| 97 | + "id": str(uuid.uuid4()), |
| 98 | + } |
| 99 | + |
| 100 | + try: |
| 101 | + response = requests.post(url, json=payload, headers=headers, verify=False, timeout=10000) |
| 102 | + response.raise_for_status() |
| 103 | + if response.text == 'ko!': |
| 104 | + raise Exception("ko!") |
| 105 | + elif not response.json()['result']['output']: |
| 106 | + raise Exception("Empty response from ExportPhoto", response.json()) |
| 107 | + data = response.json()['result']['output'] |
| 108 | + os.makedirs(f'./cache/pictures/files', exist_ok=True) |
| 109 | + with open(f'./cache/pictures/files/{id}.jpg', 'wb') as fichier: |
| 110 | + fichier.write(base64.b64decode(data[0][0].get('fichier'))) |
| 111 | + logger.info(f"{col.get('methodBase64')} -> {id}") |
| 112 | + except requests.exceptions.HTTPError as e: |
| 113 | + logger.warning(f"Failed to insert {col.get('methodBase64')}: {e} \n {e.response.text}") |
0 commit comments