Created
July 29, 2024 08:56
-
-
Save intellectronica/f5d093ccb1618aa3f9faf6d5c42304f3 to your computer and use it in GitHub Desktop.
Script for creating Mochi flashcards based on the Ultimate Geography card collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import tempfile | |
import subprocess | |
import pandas as pd | |
import base64 | |
from textwrap import dedent | |
from datetime import datetime | |
import random | |
import string | |
MOCHI_BASE_URL = 'https://app.mochi.cards/api/' | |
MOCHI_API_KEY = '...' # <- Your MochiChards API Key goes here | |
def mochi(http_method, endpoint, data=None, files=None): | |
headers = { 'Content-Type': 'application/json' } | |
url = MOCHI_BASE_URL + endpoint | |
kwargs = {} | |
if data: | |
kwargs['json'] = data | |
if files: | |
kwargs['files'] = files | |
response = requests.request(http_method, url, auth=(MOCHI_API_KEY, None), headers=headers, **kwargs) | |
return response.json() | |
temp_dir = tempfile.TemporaryDirectory() | |
subprocess.run(['git', 'clone', 'https://github.com/anki-geo/ultimate-geography.git', temp_dir.name]) | |
for data in ['main', 'capital', 'capital_hint', 'capital_info', 'country', 'country_info', 'flag_similarity']: | |
globals()['df_' + data] = pd.read_csv(f'{temp_dir.name}/src/data/{data}.csv') | |
countries = [] | |
for main in df_main['country'].tolist(): | |
country = { | |
'country': main, | |
} | |
flag = df_main[df_main['country'] == main]['flag'].values[0] | |
if pd.notnull(flag): | |
country['flag'] = flag.split('"')[1].split('"')[0] | |
with open(f'{temp_dir.name}/src/media/flags/{country["flag"]}', 'rb') as file: | |
country['flag_data'] = base64.b64encode(file.read()).decode('utf-8') | |
map = df_main[df_main['country'] == main]['map'].values[0] | |
if pd.notnull(map): | |
country['map'] = map.split('"')[1].split('"')[0] | |
with open(f'{temp_dir.name}/src/media/maps/{country["map"]}', 'rb') as file: | |
country['map_data'] = base64.b64encode(file.read()).decode('utf-8') | |
if len(df_capital[df_capital['country'] == main]['capital']) > 0: | |
country['capital'] = df_capital[df_capital['country'] == main]['capital'].values[0] | |
if len(df_capital_hint[df_capital_hint['country'] == main]['capital hint']) > 0: | |
country['capital_hint'] = df_capital_hint[df_capital_hint['country'] == main]['capital hint'].values[0] | |
if len(df_capital_info[df_capital_info['country'] == main]['capital info']) > 0: | |
country['capital_info'] = df_capital_info[df_capital_info['country'] == main]['capital info'].values[0] | |
if str(country['capital_info']) == 'nan': | |
del country['capital_info'] | |
if len(df_country_info[df_country_info['country'] == main]['country info']) > 0: | |
country['country_info'] = df_country_info[df_country_info['country'] == main]['country info'].values[0] | |
if str(country['country_info']) == 'nan': | |
del country['country_info'] | |
if len(df_flag_similarity[df_flag_similarity['country'] == main]['flag similarity']) > 0: | |
country['flag_similarity'] = df_flag_similarity[df_flag_similarity['country'] == main]['flag similarity'].values[0] | |
countries.append(country) | |
def generate_random_string(length): | |
characters = string.ascii_letters + string.digits | |
return ''.join(random.choice(characters) for _ in range(length)) | |
deck_id = mochi('POST', 'decks', { 'name': datetime.now().isoformat() + ' Ultimate Geography' })['id'] | |
for country in countries: | |
if 'flag' in country: | |
flag_filename = generate_random_string(12) + '.svg' | |
flag_card = { | |
'deck-id': deck_id, | |
'content': dedent(f""" | |
<div style="text-align: center"><img style="text-align: center" src="{flag_filename}" /></div> | |
--- | |
<span style="text-align: center"> | |
## {country['country']} | |
</span> | |
""").strip(), | |
'review-reverse?': False, | |
'deprecated/attachments': [{ | |
'file-name': flag_filename, | |
'content-type': 'image/svg+xml', | |
'data': country['flag_data'], | |
}], | |
} | |
if 'country_info' in country: | |
flag_card['content'] += f"""\n\n<div style="text-align: center">{country['country_info']}</div>""" | |
if 'flag_similarity' in country: | |
flag_card['content'] += f"""\n\n<div style="text-align: center"><small><i>Similar to: {country['flag_similarity']}</i></small></div>""" | |
flag_card['content'] += "\n\n#flag" | |
mochi('POST', 'cards', flag_card) | |
if 'map' in country: | |
map_filename = generate_random_string(12) + '.png' | |
map_card = { | |
'deck-id': deck_id, | |
'content': dedent(f""" | |
<div style="text-align: center"><img style="text-align: center" src="{map_filename}" /></div> | |
--- | |
<span style="text-align: center"> | |
## {country['country']} | |
</span> | |
""").strip(), | |
'review-reverse?': False, | |
'deprecated/attachments': [{ | |
'file-name': map_filename, | |
'content-type': 'image/png', | |
'data': country['map_data'], | |
}], | |
} | |
if 'country_info' in country: | |
map_card['content'] += f"""\n\n<div style="text-align: center">{country['country_info']}</div>""" | |
map_card['content'] += "\n\n#map" | |
mochi('POST', 'cards', map_card) | |
if 'capital' in country: | |
capital_content = dedent(f""" | |
<span style="text-align: center"> | |
## {country['capital']} | |
</span> | |
""") | |
if 'capital_info' in country: | |
capital_content += f"""\n\n<div style="text-align: center">{country['capital_info']}</div>""" | |
capital_content += dedent(f""" | |
--- | |
<span style="text-align: center"> | |
## {country['country']} | |
</span> | |
""") | |
if 'country_info' in country: | |
capital_content += f"""\n\n<div style="text-align: center">{country['country_info']}</div>""" | |
capital_content += "\n\n#capital" | |
capital_card = { | |
'deck-id': deck_id, | |
'content': capital_content.strip(), | |
'review-reverse?': True, | |
} | |
mochi('POST', 'cards', capital_card) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment