From c8bd60648d193f85479d5f380b05bb6d4394ffda Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Mon, 26 Jul 2021 22:28:05 +0200 Subject: fix: Various fixes --- Dockerfile | 4 ++-- importer/downloader.py | 7 ++----- importer/providers/i_reddit.py | 16 +++++++++++----- importer/providers/providerbase.py | 2 +- importer/providers/raw_image_base.py | 2 +- importer/providers/youtube_dl_base.py | 5 +++-- main.py | 25 +++++++++++++++++-------- 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index a2421ec..22e32bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,8 +7,8 @@ WORKDIR /opt COPY requirements.txt . RUN pip install -r requirements.txt -COPY importer/* importer/ -COPY main.py . +ADD importer importer +ADD main.py . ENTRYPOINT python main.py diff --git a/importer/downloader.py b/importer/downloader.py index 1dd1684..290c9e2 100644 --- a/importer/downloader.py +++ b/importer/downloader.py @@ -15,8 +15,5 @@ class Downloader: self.url = url self.reddit = reddit - def download(self): - with self.Provider(url=self.url, reddit=self.reddit) as provider: - provider.download() - self.paths = provider.paths - self.downloaded = provider.downloaded + def provider(self): + return self.Provider(url=self.url, reddit=self.reddit) diff --git a/importer/providers/i_reddit.py b/importer/providers/i_reddit.py index 797ce43..13fc70e 100644 --- a/importer/providers/i_reddit.py +++ b/importer/providers/i_reddit.py @@ -2,8 +2,14 @@ from importer.providers.raw_image_base import RawImageProviderBase class IReddit(RawImageProviderBase): - regex = "^.*i\\.redd\\.it.*\\.(jpg|jpeg)$" - _TEST = [{ - "url": "https://i.redd.it/pjj1ll1b2rr41.jpg", - "paths": ["source_pjj1ll1b2rr41.jpg"] - }] + regex = "^.*i\\.redd\\.it.*\\.(jpg|jpeg|gif)$" + _TEST = [ + { + "url": "https://i.redd.it/pjj1ll1b2rr41.jpg", + "paths": ["source_pjj1ll1b2rr41.jpg"] + }, + { + "url": "https://i.redd.it/55vpi6ol5jc71.gif", + "paths": ['source_55vpi6ol5jc71.gif'] + } + ] diff --git a/importer/providers/providerbase.py b/importer/providers/providerbase.py index 374b9af..51b525e 100644 --- a/importer/providers/providerbase.py +++ b/importer/providers/providerbase.py @@ -12,7 +12,7 @@ class ProviderBase: "paths": ["source_fXLMjfp.jpg"], }] - def __init__(self, url: str): + def __init__(self, url: str, **kwargs): self.url = url self.paths = [] self.downloaded = False diff --git a/importer/providers/raw_image_base.py b/importer/providers/raw_image_base.py index 267dcad..89ecfca 100644 --- a/importer/providers/raw_image_base.py +++ b/importer/providers/raw_image_base.py @@ -8,7 +8,7 @@ from importer.providers.providerbase import ProviderBase class RawImageProviderBase(ProviderBase): - regex = "^.*i.(jpg|jpeg|mp4)$" + regex = "^.*.(jpg|jpeg|mp4|gif)$" def download(self): path = self._download_raw_file(self.url) diff --git a/importer/providers/youtube_dl_base.py b/importer/providers/youtube_dl_base.py index 3bb2fb8..3d67738 100644 --- a/importer/providers/youtube_dl_base.py +++ b/importer/providers/youtube_dl_base.py @@ -18,6 +18,7 @@ class YoutubeDlProviderBase(ProviderBase): def download(self): ydl_opts = { + 'quiet': True, 'format': self.format, 'merge_output_format': self.merge_format_output, 'outtmpl': self.output_template @@ -28,9 +29,9 @@ class YoutubeDlProviderBase(ProviderBase): if info.get('_type', None) == 'playlist': for entry in info['entries']: r = ydl.prepare_filename(entry) - self.paths.append(f'{os.path.splitext(r)[0]}.mp4') + self.paths.append(f'{os.path.splitext(r)[0]}.{entry["ext"]}') else: r = ydl.prepare_filename(info) - self.paths.append(f'{os.path.splitext(r)[0]}.mp4') + self.paths.append(f'{os.path.splitext(r)[0]}.{info["ext"]}') self.downloaded = True diff --git a/main.py b/main.py index 575a7ef..72c10fd 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import argparse +import logging import os import praw @@ -35,6 +36,8 @@ parser.add_argument('-d', '--nextcloud-path', help="Nextcloud root folder", default=os.environ.get('NEXTCLOUD_PATH', 'im')) +logging.basicConfig(level=logging.INFO) + if __name__ == "__main__": args = parser.parse_args() reddit = praw.Reddit(client_id=args.client_id, @@ -54,14 +57,20 @@ if __name__ == "__main__": def upload(post): - url = post.url - create_folders(f"{args.nextcloud_path}/{post.subreddit}/", nxc) - with Downloader(url=url, reddit=reddit) as d: - d.download() - for path in d.paths: - if "-mobile" in path: # Remove mobile version - continue - upload_file(path, f"im/{post.subreddit}/{path}", nxc) + try: + url = post.url + create_folders(f"{args.nextcloud_path}/{post.subreddit}/", nxc) + d = Downloader(url=url, reddit=reddit) + with d.provider() as provider: + provider.download() + logging.info(f"{post.id} from {post.subreddit} downloaded") + for path in provider.paths: + if "-mobile" in path: # Remove mobile version + continue + upload_file(path, f"{args.nextcloud_path}/{post.subreddit}/{path}", nxc) + logging.info(f"{path} uploaded") + except Exception as e: + logging.error(e) generator = stream_generator(redditor.saved, attribute_name="name") -- cgit v1.2.3