/
post_image.py
95 lines (79 loc) · 2.75 KB
/
post_image.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
"""Posts an image to BlueSky.
Usage:
$ python post_image.py credfile.secret slides_input.db3
See README for description of the credentials file and the table expected
in the SQLite file.
"""
import base64
import pathlib
import sys
import sqlite3
import bsky_lib
_SELECT_SLIDE_QUERY = """
WITH collection_counts AS (
SELECT collection, COUNT(*) as num_img
FROM slides
GROUP BY collection
)
SELECT
sl.collection,
sl.filename,
sl.file_id_num,
cc.num_img,
sl.jpeg_base64
FROM
slides AS sl
LEFT JOIN collection_counts AS cc
ON sl.collection = cc.collection
ORDER BY RANDOM()
LIMIT 1
"""
_COLLECTION_URLS = {
"AFSC 35mm presentation slides": "https://www.sambiddle.com/afsc",
"Alaskan Air Command": "https://www.sambiddle.com/aac",
"MX Missile": "https://www.sambiddle.com/mx-missile",
"NORAD 35mm presentation slides": "https://www.sambiddle.com/norad",
"ORGANIZATION FOR NATIONAL SECURITY": "https://www.sambiddle.com/organization-for-national-security-vd037",
"SERIES 78, AERO SPACE DEFENSE COMMAND BOX 1 OF 2 V-0092": "https://www.sambiddle.com/series-78-aero-space-defense-command-box-1-of-2-v-0092",
"SOVIET MILITARY CAPABILITIES S-100-18-85 BOX 1 OF 2": "https://www.sambiddle.com/soviet-military-capabilities-s-100-18-85-box-1-of-2",
"US Navy 35mm presentation slides": "https://www.sambiddle.com/us-navy",
"V-0073 TACTICAL AIR COMMAND 1978 BX 1 of 2": "https://www.sambiddle.com/v-0073-tactical-air-command-1978-bx-1-of-2"
}
def post_image(
collection: str, file_rank: int, collection_size: int, jpeg_b64: str,
login: bsky_lib.BSkyLogin
):
collection_url = _COLLECTION_URLS.get(
collection, "https://www.sambiddle.com/35mm-scans")
builder = bsky_lib.BSkyMessageBuilder()
builder.add_segment(
bsky_lib.PlainTextSegment(
f'"{collection}," slide {file_rank} of {collection_size} ['
)
)
builder.add_segment(
bsky_lib.HyperlinkSegment(text="gallery", url=collection_url)
)
builder.add_segment(bsky_lib.PlainTextSegment("]"))
img_bytes = base64.b64decode(jpeg_b64)
builder.add_jpeg(img_bytes)
builder.post(login)
def main():
credfile = pathlib.Path(sys.argv[1])
login = bsky_lib.BSkyLogin.from_file(credfile)
db_filename = sys.argv[2]
conn = sqlite3.connect(db_filename)
cur = conn.cursor()
cur.execute(_SELECT_SLIDE_QUERY)
collection, _, rank, col_size, jpeg_b64 = cur.fetchone()
print(collection, rank, col_size)
post_image(
collection=collection,
file_rank=rank,
collection_size=col_size,
jpeg_b64=jpeg_b64,
login=login
)
conn.close()
if __name__ == "__main__":
main()