-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.py
32 lines (25 loc) · 915 Bytes
/
format.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from urllib.request import urlopen
import json
import os
# Output files
_OUTPUT_FOLDER = "output/"
_JSON_FILE = "quotes.json"
_FORTUNE_FILE = "vndb"
# Get query results
response = urlopen("https://query.vndb.org/5c9a6037d875c238.json")
fortunes = json.loads(response.read())
os.makedirs(_OUTPUT_FOLDER, exist_ok=True)
with open(_OUTPUT_FOLDER + _JSON_FILE, "w", encoding="utf-8") as f:
json.dump(fortunes, f)
# Convert to fortune file format
def fortune_generator(fortunes):
for fortune in fortunes:
yield f"{fortune['quote']}\n\t-- {fortune['source']}"
with open(_OUTPUT_FOLDER + _FORTUNE_FILE, "w") as file:
# `%` is necessary for fortune file
for quote in fortune_generator(fortunes):
file.write(quote + "\n%\n")
# Generate .dat file
os.system(f"strfile -c % {_OUTPUT_FOLDER + _FORTUNE_FILE}")