-
-
Save unconditional/e8ad9d00805a5c7c63c1c42267e05c51 to your computer and use it in GitHub Desktop.
Convert imgur album to a pdf file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from imgurpython import ImgurClient | |
from creds import * | |
import PIL | |
from reportlab.pdfgen import canvas | |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, \ | |
Spacer, PageBreak | |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
from reportlab.lib.pagesizes import A4 | |
from reportlab.lib.units import mm | |
import requests | |
import re | |
import shutil | |
import os | |
import sys | |
import urllib3 | |
urllib3.disable_warnings() | |
CLIENT_ID = 'xxxx' | |
CLIENT_SECRET = 'yyyyyyy' | |
def get_scaled_reportlab_image(name, doc): | |
sc = PIL.Image.open(name) | |
(width, height) = sc.size | |
page_width = 210*mm-doc.rightMargin-doc.leftMargin | |
page_height = 293*mm-doc.topMargin-doc.bottomMargin | |
if width > page_width or height > page_height: | |
scale = page_width / width | |
width *= scale | |
height *= scale | |
return Image(name, width, height) | |
def main(argv): | |
album_id = input('Album ID: ') | |
client = ImgurClient(CLIENT_ID, CLIENT_SECRET) | |
album_data = client.get_album(album_id) | |
album_title_raw = album_data.title | |
album_title = re.sub('[^\w\s-]', '', album_title_raw) | |
album_file = album_title + '.pdf' | |
album_dir = os.path.join(os.path.dirname(__file__), album_title) | |
doc = SimpleDocTemplate( | |
album_file, | |
pagesize=A4, | |
rightMargin=25, | |
leftMargin=25, | |
topMargin=25, | |
bottomMargin=25 | |
) | |
styles = getSampleStyleSheet() | |
styles['Normal'].fontSize=12 | |
styles['Normal'].leading=20 | |
styles['Normal'].spaceBefore=10 | |
styles['Heading1'].spaceAfter=20 | |
style_normal = styles['Normal'] | |
style_heading = styles['Heading1'] | |
items = client.get_album_images(str(album_id)) | |
if not items: | |
print("Failed to get items") | |
sys.exit() | |
if os.path.isfile(album_file): | |
os.remove(album_file) | |
if not os.path.isdir(album_dir): | |
os.mkdir(album_dir) | |
print(" Downloading album_id to dir: " + album_dir) | |
story = [] | |
story.append(Paragraph(album_title_raw, style_heading)) | |
for item in items: | |
print(" >> Item: " + str(item.id)) | |
name = str(item.id) + '.jpg' | |
name = os.path.join(album_dir, name) | |
if os.path.isfile(name): | |
print(name + ' already downloaded') | |
else: | |
response = requests.get(item.link, stream=True) | |
with open(name, 'wb') as out_file: | |
shutil.copyfileobj(response.raw, out_file) | |
del response | |
im = get_scaled_reportlab_image(name, doc) | |
title = str(item.title) | |
if item.title: | |
story.append(Paragraph(item.title, style_normal)) | |
story.append(im) | |
if item.description: | |
story.append(Paragraph(item.description, style_normal)) | |
story.append(PageBreak()) | |
doc.build(story) | |
print(f' Album {album_id} file created: {album_file}') | |
#shutil.rmtree(album_dir, ignore_errors=True) | |
if __name__ == '__main__': | |
main(sys.argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment