-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
135 lines (114 loc) · 3.77 KB
/
main.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
import itertools
import os
import math
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
from collections import namedtuple
import requests
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(loader=FileSystemLoader("html"), autoescape=select_autoescape())
Options = namedtuple(
"Options",
("base_folder", "all_posts", "root_path", "switch_message", "switch_path"),
)
def take(n, it):
"""
Return first n items of the iterable as a list
"""
it = iter(it)
while r := list(itertools.islice(it, n)):
yield r
def get_paintings(artist_id):
"""
Fetch artist last painting from their gallery
"""
url = "https://www.dailypaintworks.com/Home/SearchPostsResults"
payload = {
"inSearchId": "1680581114640",
"inSitePage": "GALLERY",
"inByDate": "false",
"inCurrentDate": datetime.now().strftime("%M/%D/%Y"),
"inPageNumber": "0",
"inDisplayedPostCount": "0",
"isInArrangeMode": "false",
"inArtistId": str(artist_id),
}
headers = {
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
}
res = requests.request("POST", url, headers=headers, data=payload)
assert res.status_code == 200, res.text
return res.json()
def sync():
aids = os.getenv("ARTIST_IDS")
if not aids:
print("No artist ids found. Exiting")
return
aids = aids.split(",")
print("[%d] artist ids found" % len(aids))
pool = ThreadPool(4)
data = pool.map(get_paintings, aids)
pool.close()
pool.join()
data = sorted(
sum([d["Posts"] for d in data], []),
key=lambda p: (
datetime.strptime(p["DateString"], "%b %d, %Y"),
p["GalleryName"],
),
reverse=True,
)
return data
def update_template(all_posts, page_size=120):
if not all_posts:
return
options = [
Options(
"docs",
all_posts,
"/dailypaintworks-updates",
"Show only available",
"/dailypaintworks-updates/available",
),
Options(
os.path.join("docs", "available"),
[p for p in all_posts if not p["Sold"]],
"/dailypaintworks-updates/available",
"Show all",
"/dailypaintworks-updates",
),
]
template = env.get_template("index.html")
for opt in options:
pages_total = math.ceil(len(opt.all_posts) / page_size)
print(
"[%d] posts found. Creating [%d] pages" % (len(opt.all_posts), pages_total)
)
if not os.path.exists(opt.base_folder):
os.mkdir(opt.base_folder)
page_folder = os.path.join(opt.base_folder, "pages")
if not os.path.exists(page_folder):
os.mkdir(page_folder)
for page, posts in enumerate(take(page_size, opt.all_posts), start=1):
if not posts:
break
filename = os.path.join(page_folder, f"{page}.html")
if page == 1:
filename = os.path.join(opt.base_folder, "index.html")
kwargs = {
"posts": posts,
"page_current": page,
"root_path": opt.root_path,
"pages": [f"{opt.root_path}/pages/{i + 1}" for i in range(pages_total)],
"switch_message": opt.switch_message,
"switch_path": opt.switch_path,
}
with open(filename, "w") as f:
f.write(template.render(**kwargs))
if __name__ == "__main__":
update_template(sync())