From e04d89a1278f66c15be9cadb6ab524dbb878efd8 Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Sat, 3 Jul 2021 21:24:15 +0200 Subject: Initial commit --- util.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 util.py (limited to 'util.py') diff --git a/util.py b/util.py new file mode 100644 index 0000000..cd80506 --- /dev/null +++ b/util.py @@ -0,0 +1,40 @@ +import argparse +import json +import os +from time import sleep +from typing import Dict + +import jsonpickle +import requests + +headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} + +parser = argparse.ArgumentParser(description="Monitor saved") +parser.add_argument('-c', '--client-id', help="Reddit client id", default=os.environ.get('CLIENT_ID', '')) +parser.add_argument('-s', '--client-secret', help="Reddit client secret", default=os.environ.get('CLIENT_SECRET', '')) +parser.add_argument('-u', '--reddit-username', help="Reddit username", default=os.environ.get('REDDIT_USERNAME', '')) +parser.add_argument('-p', '--reddit-password', help="Reddit user password", default=os.environ.get('REDDIT_PASSWORD', '')) +parser.add_argument('-P', '--nextcloud-password', help="Nextcloud Password", default=os.environ.get('NEXTCLOUD_PASSWORD', '')) +parser.add_argument('-U', '--nextcloud-username', help="Nextcloud Username", default=os.environ.get('NEXTCLOUD_USERNAME', '')) +parser.add_argument('-o', '--nextcloud-host', help="Nextcloud Host", default=os.environ.get('NEXTCLOUD_HOST', 'localhost')) + + +def try_post(url, json_string, count=0): + try: + if count > 10: + return + r = requests.post(url, data=json_string, headers=headers) + if r.status_code != 200: + sleep(60 * count) + try_post(url, json_string, count + 1) + except: + sleep(60 * count) + try_post(url, json_string, count + 1) + + +def jsonfy(post): + json_string = jsonpickle.encode(post) + json_dict: Dict = json.loads(json_string) + json_dict.pop('_reddit') + json_dict.pop('py/object') + return json.dumps(json_dict) -- cgit v1.2.3