diff options
Diffstat (limited to 'util.py')
-rw-r--r-- | util.py | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -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) |