implemented comic download cli

This commit is contained in:
Bryan Bailey
2022-03-09 23:23:33 -05:00
parent 8d44f40fb1
commit b0e2ad0a3b
2 changed files with 17 additions and 4 deletions

View File

@@ -1,6 +1,9 @@
from pydoc import cli
import click import click
from yoink.common import qb_client, app_root, library_path, config_path from yoink.common import qb_client, app_root, library_path, config_path
from yoink.comic import Comic, ComicArchiver
from yoink.torrent import Torrent, TorrentDownloader
@@ -17,7 +20,15 @@ def download(url):
click.echo('url cannot be blank') click.echo('url cannot be blank')
return 1 return 1
click.echo('Downloading') comic = Comic(url)
archiver = ComicArchiver(comic)
click.echo(f'Downloading {comic.title}')
archiver.download()
click.echo('Building comic archive')
archiver.generate_archive()
click.echo('Cleaning up')
archiver.cleanup_worktree()
click.echo('Success')
if __name__=='__main__': if __name__=='__main__':
yoink() yoink()

View File

@@ -62,10 +62,11 @@ class ComicArchiver:
if not os.path.exists(self.worktree): if not os.path.exists(self.worktree):
os.makedirs(self.worktree, mode=0o777) os.makedirs(self.worktree, mode=0o777)
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
for index,url in enumerate(self.comic.filelist): for index,url in enumerate(self.comic.filelist):
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
if not url.endswith('.jpg'): if not url.endswith('.jpg'):
formatted_file = os.path.join(self.worktree, f'{self.comic.title} ' + ''.join([str(index).zfill(3), '.jpg'])) formatted_file = os.path.join(self.worktree, f'{self.comic.title} ' + ''.join([str(index).zfill(3), '.jpg']))
@@ -75,6 +76,7 @@ class ComicArchiver:
page_number = url.split('/')[-1].split('.')[0].zfill(3) page_number = url.split('/')[-1].split('.')[0].zfill(3)
file_extension = url.split('/')[-1].split('.')[1] file_extension = url.split('/')[-1].split('.')[1]
urllib.request.urlretrieve(url, filename=os.path.join(self.worktree, f'{self.comic.title}{page_number}.{file_extension}')) urllib.request.urlretrieve(url, filename=os.path.join(self.worktree, f'{self.comic.title}{page_number}.{file_extension}'))
print()
def generate_archive(self, archive_format='.cbr'): def generate_archive(self, archive_format='.cbr'):
if os.path.exists(os.path.join(self.worktree, f'{self.comic.title}{archive_format}')): if os.path.exists(os.path.join(self.worktree, f'{self.comic.title}{archive_format}')):