-
Notifications
You must be signed in to change notification settings - Fork 0
/
slanki.py
87 lines (75 loc) · 2.63 KB
/
slanki.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
import json
import os
import shutil
import sys
import tempfile
import urllib.request
import genanki
import requests
from dotenv import load_dotenv
load_dotenv()
SLACK_API_TOKEN = os.getenv('SLACK_API_TOKEN')
USERS_LIMIT = int(os.getenv('USERS_LIMIT'))
OUTPUT_FILE = os.getenv('OUTPUT_FILE')
DECK_ID = int(os.getenv('DECK_ID'))
MODEL_ID = int(os.getenv('MODEL_ID'))
def fetch_slack_users(token):
resp = requests.get(
'https://slack.com/api/users.list',
params={
'token': token,
'limit': 1000,
})
if resp.status_code != 200:
print("Got status code %s from Slack users.list API", resp.status_code)
sys.exit(1)
j = resp.json()
# we don't want to learn slackbot's name
ignore_users = set(['slackbot'])
j['members'] = [m for m in j['members'] if m['name'] not in ignore_users][:USERS_LIMIT]
return j['members']
def best_image_field(profile):
# some slack user profiles have a 'image_original' field, some
# don't. grab the image_N field with the highest N.
if 'image_original' in profile:
return 'image_original'
image_fields = [f for f in profile if f.startswith('image_')]
return max(image_fields, key=lambda f: int(f.split('_')[1]))
def generate_anki_deck(output_file, users):
deck = genanki.Deck(DECK_ID, 'Zapier Team Names')
model = genanki.Model(
MODEL_ID,
'Name model',
fields=[
{'name': 'photo_tags'},
{'name': 'name'},
],
templates=[
{
'name': 'card 1',
'qfmt': '{{photo_tags}}',
'afmt': '{{name}}',
}
])
old_working_dir = os.getcwd()
temp_dir = tempfile.mkdtemp(prefix='slanki')
os.chdir(temp_dir)
image_file_names = []
for i, user in enumerate(users):
print("User %s [%d/%d]" % (user['profile']['real_name'], i, len(users)))
image_url = user['profile'][best_image_field(user['profile'])]
image_file_name = image_url.split('/')[-1]
urllib.request.urlretrieve(image_url, image_file_name)
image_file_names.append(image_file_name)
note = genanki.Note(
model=model,
fields=['<img src="%s">' % image_file_name, user['profile']['real_name']]
)
deck.add_note(note)
package = genanki.Package(deck, media_files=image_file_names)
package.write_to_file(os.path.join(old_working_dir, output_file))
shutil.rmtree(temp_dir)
if __name__ == '__main__':
slack_users_json = fetch_slack_users(SLACK_API_TOKEN)
generate_anki_deck(OUTPUT_FILE, slack_users_json)
print("Created %s" % OUTPUT_FILE)