aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgabrielgio <gabriel.giovanini@pm.me>2021-07-27 21:39:27 +0200
committergabrielgio <gabriel.giovanini@pm.me>2021-07-27 21:39:27 +0200
commit3d54b3d91d0c175feae82c413fd0139545d46e2a (patch)
tree8d57175d1c364c2be3800818cd682e01b748089e
parentc8bd60648d193f85479d5f380b05bb6d4394ffda (diff)
downloadreddit-nextcloud-importer-3d54b3d91d0c175feae82c413fd0139545d46e2a.tar.gz
reddit-nextcloud-importer-3d54b3d91d0c175feae82c413fd0139545d46e2a.tar.bz2
reddit-nextcloud-importer-3d54b3d91d0c175feae82c413fd0139545d46e2a.zip
feat: Add two more options
* from beginning: it will download all available saved posts. * log level: it will set a log level to logging lib.
-rw-r--r--README.md11
-rw-r--r--main.py31
2 files changed, 39 insertions, 3 deletions
diff --git a/README.md b/README.md
index 42f5f13..5fd1438 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,7 @@
- [![pipeline status](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/badges/main/pipeline.svg)](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/-/commits/main) [![coverage report](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/badges/main/coverage.svg)](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/-/commits/main)
+ [![pipeline
+ status](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/badges/main/pipeline.svg)](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/-/commits/main)
+ [![coverage
+ report](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/badges/main/coverage.svg)](https://gitlab.com/gabrielgio/reddit-nextcloud-importer/-/commits/main)
# Reddit Nextcloud importer
@@ -30,6 +33,12 @@ Nextcloud.
* `NEXTCLOUD_PATH`: root folder for where all the images will be saved.
+* `FROM_BEGINNING`: it will download all the files since the first availabile
+ saved post.
+
+* `LOG_LEVEL`: it will set the log level. It can chosen between:
+ * info
+ * error
# Running as docker container
diff --git a/main.py b/main.py
index 72c10fd..036aab3 100644
--- a/main.py
+++ b/main.py
@@ -1,6 +1,7 @@
import argparse
import logging
import os
+from time import sleep
import praw
@@ -10,6 +11,15 @@ from praw.models.util import stream_generator
from importer.downloader import Downloader
from importer.uploader import upload_file, create_folders
+levels = {
+ 'critical': logging.CRITICAL,
+ 'error': logging.ERROR,
+ 'warn': logging.WARNING,
+ 'warning': logging.WARNING,
+ 'info': logging.INFO,
+ 'debug': logging.DEBUG
+}
+
parser = argparse.ArgumentParser(description="Monitor saved")
parser.add_argument('-c', '--client-id',
help="Reddit client id",
@@ -35,11 +45,22 @@ parser.add_argument('-o', '--nextcloud-host',
parser.add_argument('-d', '--nextcloud-path',
help="Nextcloud root folder",
default=os.environ.get('NEXTCLOUD_PATH', 'im'))
-
-logging.basicConfig(level=logging.INFO)
+parser.add_argument('-f', '--from-beginning',
+ dest='from_beginning',
+ help="it will attempt to download all saved posts from the beginning.",
+ action='store_true',
+ default=os.environ.get("FROM_BEGINNING") is not None)
+parser.add_argument('-l', '--log-level',
+ default=os.environ.get('LOG_LEVEL', 'info'),
+ choices=levels.keys(),
+ help=f'it will set log level.')
if __name__ == "__main__":
args = parser.parse_args()
+
+ level = levels.get(args.log_level.lower())
+ logging.basicConfig(level=level)
+
reddit = praw.Reddit(client_id=args.client_id,
client_secret=args.client_secret,
password=args.reddit_password,
@@ -73,6 +94,12 @@ if __name__ == "__main__":
logging.error(e)
+ if args.from_beginning:
+ logging.info(f"Downloading from the beginning")
+ for post in redditor.saved(limit=None):
+ upload(post)
+ sleep(60)
+
generator = stream_generator(redditor.saved, attribute_name="name")
for post in generator:
upload(post)