-
Notifications
You must be signed in to change notification settings - Fork 67
/
screen-literature-clock-get.py
128 lines (101 loc) · 4.31 KB
/
screen-literature-clock-get.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
import random
import codecs
import textwrap
from utility import is_stale
import requests
import csv
import datetime
import re
import math
if is_stale('litclock_annotated.csv', 86400):
url = "https://raw.githubusercontent.com/JohannesNE/literature-clock/master/litclock_annotated.csv"
response = requests.get(url)
response.raise_for_status()
with open('litclock_annotated.csv', 'w') as text_file:
text_file.write(response.text)
time_rows = []
current_time = datetime.datetime.now().strftime("%H:%M")
# current_time = "07:32"
with open('litclock_annotated.csv', 'r') as file:
reader = csv.DictReader(file,
fieldnames=[
"time", "time_human", "full_quote", "book_title", "author_name", "sfw"],
delimiter='|',
lineterminator='\n',
quotechar=None, quoting=csv.QUOTE_NONE)
for row in reader:
if row["time"] == current_time and row["sfw"] != "nsfw":
time_rows.append(row)
if len(time_rows) == 0:
print("No quotes found for this time.")
exit()
else:
# chosen_item = random.choice(time_rows)
chosen_item = min(time_rows, key=lambda x: len(x["full_quote"]))
print(chosen_item)
quote = chosen_item["full_quote"]
book = chosen_item["book_title"]
author = chosen_item["author_name"]
human_time = chosen_item["time_human"]
# replace newlines with spaces
quote = quote.replace("<br/>", " ")
quote = quote.replace("<br />", " ")
quote = quote.replace("<br>", " ")
quote = quote.replace(u"\u00A0", " ") # non breaking space
# replace punctuation with simpler counterparts
transl_table = dict([(ord(x), ord(y)) for x, y in zip(u"‘’´“”—–-", u"'''\"\"---")])
quote = quote.translate(transl_table)
human_time = human_time.translate(transl_table)
quote = quote.encode('ascii', 'ignore').decode('utf-8')
human_time = human_time.encode('ascii', 'ignore').decode('utf-8')
quote_length = len(quote)
# Try to calculate font size and max chars based on quote length
goes_into = (quote_length / 100) if quote_length > 80 else 0
font_size = 60 - (goes_into * 8)
max_chars_per_line = 23 + (goes_into * 6)
# Some upper and lower limit adjustments
font_size = 25 if font_size < 25 else font_size
max_chars_per_line = 55 if max_chars_per_line > 55 else max_chars_per_line
font_size = math.ceil(font_size)
max_chars_per_line = math.floor(max_chars_per_line)
attribution = f"- {book}, {author}"
if len(attribution) > 55:
attribution = attribution[:55] + "…"
print(f"Quote length: {quote_length}, Font size: {font_size}, Max chars per line: {max_chars_per_line}")
quote_pattern = re.compile(re.escape(human_time), re.IGNORECASE)
# Replace human time by itself but surrounded by pipes for later processing.
quote = quote_pattern.sub(lambda x: f"|{x.group()}|", quote, count=1)
lines = textwrap.wrap(quote, width=max_chars_per_line, break_long_words=True)
generated_quote = ""
time_ends_on_next_line = False
for line in lines:
start_span = ""
end_span = ""
if line.count("|") == 2:
line = line.replace("|", "<tspan style='font-weight:bold;'>", 1)
line = line.replace("|", "</tspan>", 1)
if line.count("|") == 1 and not time_ends_on_next_line:
line = line.replace("|", "<tspan style='font-weight:bold;'>", 1)
time_ends_on_next_line = True
end_span = "</tspan>"
if line.count("|") == 1 and time_ends_on_next_line:
line = line.replace("|", "</tspan>", 1)
time_ends_on_next_line = False
start_span = "<tspan style='font-weight:bold;'>"
generated_quote += f"""
<tspan x="33" dy="1.2em">{start_span}{line}{end_span}</tspan>
"""
generated_quote += f"""
<tspan x="150" dy="1.5em" style="font-size:18px;">{attribution}</tspan>
"""
svg_template = f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg height="480" width="800" version="1.1">
<rect width="800" height="480" id="rect3855" fill="white" />
<text id="quote" x="33" y="15" style="font-size:{font_size}px;line-height:0%;font-family:'Noto Serif',serif;text-anchor:beginning">
{generated_quote}
</text>
</svg>
"""
output_svg_filename = 'screen-literature-clock.svg'
svg_output = svg_template
codecs.open(output_svg_filename, 'w', encoding='utf-8').write(svg_output)