dotfiles

*nix config files
git clone git://git.pyratebeard.net/dotfiles.git
Log | Files | Refs | README

pywmt (2543B)


      1 #!/usr/bin/env python3
      2 #                                            ██
      3 #  ██████   ██   ██                         ░██
      4 # ░██░░░██ ░░██ ██  ███     ██ ██████████  ██████
      5 # ░██  ░██  ░░███  ░░██  █ ░██░░██░░██░░██░░░██░
      6 # ░██████    ░██    ░██ ███░██ ░██ ░██ ░██  ░██
      7 # ░██░░░     ██     ░████░████ ░██ ░██ ░██  ░██
      8 # ░██       ██      ███░ ░░░██ ███ ░██ ░██  ░░██
      9 # ░░       ░░      ░░░    ░░░ ░░░  ░░  ░░    ░░
     10 # p y t h o n   w e e k l y   m u s i c   t o o t
     11 #
     12 # TODO
     13 # - mastodon post
     14 
     15 import requests
     16 import datetime as dt
     17 from subprocess import check_output
     18 
     19 # number of recently played
     20 # albums to output from api
     21 ALBUM_LIMIT = 20
     22 
     23 # gather creds from password-store
     24 def get_cred(cred):
     25     return (check_output("pass navidrome/" + cred, shell=True)
     26             .splitlines()[0]
     27             .decode('utf-8'))
     28 
     29 BASE_URL = get_cred('baseurl')
     30 USERNAME = get_cred('username')
     31 PASSWORD = get_cred('password')
     32 
     33 login_url = f'{BASE_URL}/auth/login'
     34 login_data = {
     35     'username': USERNAME,
     36     'password': PASSWORD
     37 }
     38 
     39 # acquire token
     40 try:
     41     login_res = requests.post(login_url, json=login_data)
     42     login_res.raise_for_status()
     43     token = login_res.json().get('token')
     44     if not token:
     45         print("login succeeded but no token received.")
     46         exit()
     47 except Exception as e:
     48     print(f"login failed: {e}")
     49     exit()
     50 
     51 headers = {
     52     'X-ND-Authorization': f'Bearer {token}'
     53 }
     54 
     55 params = {
     56     '_start': 0,
     57     '_end': ALBUM_LIMIT,
     58     '_sort': 'play_date',
     59     '_order': 'DESC',
     60     'recently_played': 'true'
     61 }
     62 
     63 # pull last {ALBUM_LIMIT} recently played albums
     64 try:
     65     res = requests.get(f'{BASE_URL}/api/album',
     66                             headers=headers,
     67                             params=params)
     68     res.raise_for_status()
     69     albums = res.json()
     70 except Exception as e:
     71     print(f"failed to fetch albums: {e}")
     72     exit()
     73 
     74 # get last monday date to start new week
     75 today = dt.date.today()
     76 monday = today - dt.timedelta(days=today.weekday())
     77 
     78 # find all albums played since monday
     79 for a in albums:
     80     play_date = dt.datetime.fromisoformat(a.get('playDate'))
     81     if play_date.date() >= monday:
     82         print(f"{a['name']} by {a['artist']}")