aboutsummaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorgabrielgio <gabriel.giovanini@pm.me>2021-07-27 23:15:53 +0200
committergabrielgio <gabriel.giovanini@pm.me>2021-07-27 23:28:51 +0200
commitb8d69f9bf5a03fd6d8b6a477f3b7ca8f10c27bda (patch)
tree6652fd8b3e89b652791167e73b293b57d0b6496b /main.py
parent3d54b3d91d0c175feae82c413fd0139545d46e2a (diff)
downloadreddit-nextcloud-importer-b8d69f9bf5a03fd6d8b6a477f3b7ca8f10c27bda.tar.gz
reddit-nextcloud-importer-b8d69f9bf5a03fd6d8b6a477f3b7ca8f10c27bda.tar.bz2
reddit-nextcloud-importer-b8d69f9bf5a03fd6d8b6a477f3b7ca8f10c27bda.zip
feat: Move to gallery-dl
As it turns out there is already a project that does what I want but better.
Diffstat (limited to 'main.py')
-rw-r--r--main.py44
1 files changed, 34 insertions, 10 deletions
diff --git a/main.py b/main.py
index 036aab3..2f34b3a 100644
--- a/main.py
+++ b/main.py
@@ -7,8 +7,8 @@ import praw
from nextcloud import NextCloud
from praw.models.util import stream_generator
+from gallery_dl.job import DownloadJob
-from importer.downloader import Downloader
from importer.uploader import upload_file, create_folders
levels = {
@@ -55,6 +55,33 @@ parser.add_argument('-l', '--log-level',
choices=levels.keys(),
help=f'it will set log level.')
+
+def get_list_of_files(dirName):
+ # create a list of file and sub directories
+ # names in the given directory
+ listOfFile = os.listdir(dirName)
+ allFiles = list()
+ # Iterate over all the entries
+ for entry in listOfFile:
+ # Create full path
+ fullPath = os.path.join(dirName, entry)
+ # If entry is a directory then get the list of files in this directory
+ if os.path.isdir(fullPath):
+ allFiles = allFiles + get_list_of_files(fullPath)
+ else:
+ allFiles.append(fullPath)
+
+ return allFiles
+
+
+def download(url) -> [str]:
+ d = DownloadJob(url=url)
+ d.run()
+ basedir = d.pathfmt.basedirectory
+ files = get_list_of_files(basedir)
+ return files
+
+
if __name__ == "__main__":
args = parser.parse_args()
@@ -81,15 +108,12 @@ if __name__ == "__main__":
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")
+ logging.info(f"{post.id} from {post.subreddit} downloaded")
+ for path in download(url):
+ filename = os.path.basename(path)
+ upload_file(path, f"{args.nextcloud_path}/{post.subreddit}/{filename}", nxc)
+ os.unlink(path)
+ logging.info(f"{path} uploaded")
except Exception as e:
logging.error(e)