-
Notifications
You must be signed in to change notification settings - Fork 0
/
varisigne-bot.py
executable file
·79 lines (57 loc) · 2.04 KB
/
varisigne-bot.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
#!/usr/bin/env python
import json
import os.path
import random
import re
from mastodon import Mastodon
IMAGE_DATA = 'finna-data/finna-records-signe.jsonl'
ORIG_IMAGE_DIR = 'finna-data/original/signe'
COLOR_IMAGE_DIR = 'finna-data/colorized/signe'
FINNA_BASE_URL = 'https://finna.fi'
TOKEN_FILE = 'signe.secret'
def orig_image_filename(record_id):
return os.path.join(ORIG_IMAGE_DIR, f"{record_id}.jpg")
def color_image_filename(record_id):
return os.path.join(COLOR_IMAGE_DIR, f"{record_id}.jpg")
# Read the image data
images = []
with open(IMAGE_DATA) as datafile:
for line in datafile:
record = json.loads(line)
# check that we have a corresponding image, otherwise skip record
if not os.path.exists(orig_image_filename(record['id'])) or \
not os.path.exists(color_image_filename(record['id'])):
continue
images.append(record)
# Pick a random image
record = random.choice(images)
# Prepare a post
title = record['title']
try:
year = record['year']
except KeyError:
year = 'tuntematon vuosi'
collection = ', '.join([brec['translated'] for brec in record['buildings']])
link = f"{FINNA_BASE_URL}/Record/{record['id']}"
status_text = f"""
{title}. ({year}, {collection})
Alkuperäisen kuvan lähde: {link}
Valokuvaaja Signe Brander, värit #DeOldify
""".strip()
color_alt_text = f"""
{title}. ({year}, {collection})
Alkuperäisen valokuvan on ottanut Signe Brander. Tämä kuva on keinotekoisesti väritetty DeOldify-algoritmilla.
""".strip()
orig_alt_text = f"""
{title}. ({year}, {collection})
Alkuperäinen mustavalkoinen valokuva, jonka on ottanut Signe Brander.
""".strip()
# Initialize Mastodon library
mastodon = Mastodon(
access_token = TOKEN_FILE,
api_base_url = 'https://botsin.space/'
)
# Post it!
media_color = mastodon.media_post(color_image_filename(record['id']), description=color_alt_text)
media_orig = mastodon.media_post(orig_image_filename(record['id']), description=orig_alt_text)
mastodon.status_post(status_text, media_ids=[media_color, media_orig], language='fi')