Skip to content

Instantly share code, notes, and snippets.

@louisroyer
Last active April 28, 2025 09:41
Show Gist options
  • Save louisroyer/1934d8afd817891b856d9771c8b3bba8 to your computer and use it in GitHub Desktop.
Save louisroyer/1934d8afd817891b856d9771c8b3bba8 to your computer and use it in GitHub Desktop.
Resolution of karaokes from kara.moe
#!/usr/bin/env python3
'''Get stats about resolution of .ass'''
import datetime
import os
import subprocess
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from xdg import xdg_data_dirs
import yaml
def base_dir(repo_name):
'''Get base directory for a repo'''
for path in xdg_data_dirs():
filepath = os.path.join(path, 'karaokemugen-app/app/config.yml')
try:
with open(filepath, 'r', encoding='utf-8') as file:
conf = yaml.safe_load(file)
for j in conf['System']['Repositories']:
if j['Name'] == repo_name:
return j['BaseDir']
except FileNotFoundError:
pass
raise FileNotFoundError
def count(base_directory, yearmonth):
'''Count number of karaokes in 0x0 for a yearmonth'''
directory = os.path.join(base_directory, 'lyrics')
res_0x0 = 0
res_other = 0
not_ass = 0
invalid = 0
for filename_e in os.listdir(os.fsencode(directory)):
filename = os.fsdecode(filename_e)
if not filename.endswith('.ass'):
not_ass += 1
continue
try:
with open(os.path.join(directory, filename), 'r', encoding='utf-8') as ass:
res_x = None
res_y = None
for line in ass:
if not None in (res_x , res_y):
break
if line.startswith('PlayResX:'):
fres_x = line.split('PlayResX: ')
if len(fres_x) != 2:
continue
res_x = int(fres_x[1])
if line.startswith('PlayResY:'):
fres_y = line.split('PlayResY: ')
if len(fres_y) != 2:
continue
res_y = int(fres_y[1])
if (not res_x) and (not res_y):
res_0x0 += 1
else:
res_other += 1
except UnicodeDecodeError:
invalid += 1
continue
print(''.join([
f'{yearmonth}: ',
f'res_0x0: {res_0x0}, res_other: {res_other}, ',
f'not_ass: {not_ass}, invalid: {invalid}']))
return yearmonth, res_0x0, res_other
def checkout(base_directory, yearmonth):
'''Run git checkout to yearthmonth'''
subprocess.run(['git', '-C', base_directory, 'checkout', f'{yearmonth[0]}{yearmonth[1]:02}'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
def checkout_master(base_directory):
'''Reset to origin/master'''
subprocess.run(['git', '-C', base_directory, 'checkout', 'master'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
def next_yearmonth(yearmonth):
'''Get next yearmonth'''
if yearmonth[1] == 12:
return (yearmonth[0] + 1, 1)
return (yearmonth[0], yearmonth[1] + 1)
if __name__ == '__main__':
basedir = base_dir('kara.moe')
res0 = []
res1 = []
res2 = []
start_yearmonth = (2017, 5)
last_yearmonth = (2025, 3)
ym = start_yearmonth
while ym != last_yearmonth:
checkout(basedir, ym)
r0, r1, r2 = count(basedir, ym)
res0.append(datetime.datetime(r0[0], r0[1], 1))
res1.append(r1)
res2.append(r2)
ym = next_yearmonth(ym)
checkout_master(basedir)
fig, ax = plt.subplots()
plt.title('Évolution de la résolution des karas')
ax.plot(res0, res1, color='green', label='0x0')
ax.plot(res0, res2, color='red', label='non 0x0')
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(bymonth=[1, 7]))
for label in ax.get_xticklabels(which='major'):
label.set(rotation=30, horizontalalignment='right')
plt.legend()
plt.savefig('output.svg')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment