-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_poster.py
166 lines (158 loc) · 5.89 KB
/
create_poster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from image_sources.tmdb_cover_search import TMDBCoverGenerator
from image_sources.google_cover_search import GoogleCoverGenerator
from image_sources.custom_cover_generator import CustomCoverGenerator
from image_sources.itunes_cover_generator import ItunesCoverGenerator
from data_sources.movie_source import retrieve_movie_details, modifier
from data_sources.color_source import (
combo_color_palette,
retrieve_user_colors,
ext_color_palette,
)
from poster_sources.movie_poster import TheFilmPlanet, Classicult, PeliculaPrint
from poster_sources.music_poster import AlbumArtEngineer
from data_sources.music_source import retrieve_spotify_data
from image_sources.windows_image import image_open, hide_file
from uploader import create_listing, etsy_instance
import json
from PIL import Image
import random
from pathlib import Path
import argparse
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
import unicodedata
import re
value = unicodedata.normalize("NFKD", value)
value = str(re.sub("[^\w\s-]", "", value).strip().lower())
value = str(re.sub("[-\s]+", "-", value))
# ...
return value
if __name__ == "__main__":
# Parse arguments
def split_on_comma(s): return [int(item)
for item in s.split(",") if len(item) > 0]
choices = ["movie", "music"]
parser = argparse.ArgumentParser()
parser.add_argument("search", type=str, nargs="+",
help="Movie / TV Show / Album")
parser.add_argument(
"-t", "--type", choices=choices, required=True, help="Music/Movie Poster"
)
parser.add_argument("-u", "--url", type=str, help="Custom Image URL")
parser.add_argument(
"-g",
"--google",
nargs="?",
const=[],
type=split_on_comma,
help="Google Image Result Numbers",
)
parser.add_argument(
"-m",
"--movie",
nargs="?",
const=[],
type=split_on_comma,
help="Imdb Image Result Numbers",
)
parser.add_argument(
"-i",
"--itunes",
nargs="?",
const=[],
type=split_on_comma,
help="Itunes Image Result Numbers",
)
parser.add_argument("-a", "--album", type=int, help="Album number")
parser.add_argument("-p", "--plot", type=int, help="Plot number")
parser.add_argument("-f", "--fast", help="Fast colors",
action="store_true")
args = parser.parse_args()
print(args)
query = " ".join(args.search)
hide_file()
# Get Data & Poster Type & Title Slugs
data = None
Poster = None
title_slug = None
search = None
if args.type == "movie":
Poster = PeliculaPrint # TheFilmPlanet
data = modifier(retrieve_movie_details(query), args.plot) # movie
title_slug = slugify(data["original_title"]) + f"-{data['id']}"
search = data["original_title"]
elif args.type == "music":
Poster = AlbumArtEngineer
data = retrieve_spotify_data(query, args.album)
title_slug = slugify(data["title"]) + f"-{data['release_date']}"
search = data["title"] + " " + data["artist"]
# Get Covers
covers = []
if args.url:
custom_img_maker = CustomCoverGenerator()
covers += [custom_img_maker.get_cover(args.url, data=False)]
if isinstance(args.movie, list):
tmdb_img_maker = TMDBCoverGenerator(
"all", "4237f50f40774d6ed361922c222568a0")
covers += tmdb_img_maker.get_covers(
search, data=False, nums=args.movie, pause=True
)
if isinstance(args.itunes, list):
img_maker = ItunesCoverGenerator(
"backdrops", "4237f50f40774d6ed361922c222568a0" # posters
)
covers += img_maker.get_covers(search,
pause=False, nums=args.album, data=False)
if isinstance(args.google, list):
google_img_maker = GoogleCoverGenerator(
"AIzaSyDC2lWkgbX9OdzNY4OCX8oVoozVTUAkkDc", "f8b95d8f937c12c6a"
)
covers += google_img_maker.get_covers(
search, data=False, nums=args.google, pause=True
)
if len(covers) == 0:
raise ValueError("Please specify 1 or more cover engines")
nums = None
e = etsy_instance()
to_upload = []
for cover in covers:
cover_id = f"{random.randint(1,999999999):09d}"
if args.fast:
colors = ext_color_palette(cover)[:5]
else:
colors = retrieve_user_colors(cover, combo_color_palette(cover))
full_data = {
**data,
"colors": colors,
"cover_id": cover_id,
"title_slug": title_slug,
}
full_data["relative_path"] = f"posters/{full_data['title_slug']}"
full_data["img_path"] = f"posters/{full_data['title_slug']}/img/{cover_id}.png"
p = Poster(full_data, cover).get_poster()
print(f"{full_data['title_slug']}/img/{cover_id} Created.")
full_data["img_size"] = p.size
poster_dir = Path(f"{full_data['relative_path']}/img")
data_dir = Path(f"{full_data['relative_path']}/data")
covers_dir = Path(f"{full_data['relative_path']}/covers")
poster_dir.mkdir(parents=True, exist_ok=True)
data_dir.mkdir(parents=True, exist_ok=True)
covers_dir.mkdir(parents=True, exist_ok=True)
p.show()
upload = "y" in input("upload > ").strip().lower()
p.hide()
print("Saving...")
p.save(poster_dir / f"{cover_id}.png")
cover.save(covers_dir / f"{cover_id}.png")
p.close()
cover.close()
with open(data_dir / f"{cover_id}.json", "w") as f:
json.dump(full_data, f, indent=2)
if upload:
print("Poster queued for upload.")
to_upload.append(full_data)
for poster_data in to_upload:
create_listing(e, poster_data)