aboutsummaryrefslogtreecommitdiff
path: root/util.py
blob: cd80506a59fc2ef5510cd33e9118fa121f090651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)