PreviouslyOnIFM/splimport/decode.py

102 lines
2.7 KiB
Python

import os
import sys
import struct
import requests
from apev2 import Apev2
# apev2 spec: https://wiki.hydrogenaud.io/index.php?title=APEv2_specification
# parse dates from XLastPlayed and XLastScheduled from SPL. by brainsmoke:
# d=swap32(0x14a63351)
# Y,M,D,h,m,s = 1980+(d>>25), (d>>21)&0xf, (d>>16)&0x1f, (d>>11)&0x1f, (d>>5)&0x3f, (d<<1)&0x3f
# print(Y,M,D,h,m,s)
# for swapping endianness of a binary package
def swap32(i):
return struct.unpack("<I", struct.pack(">I", i))[0]
ignore_tags = [
"Energy", "Gender", "HookLen", "HookStart", "Rating", "Tempo", "Intro", "Outro", "CueDB", "CueOverlap",
"SegueDB", "Cue", "FadeSpeed", "RecordDate", "Duration", "Segue", "REPLAYGAIN_TRACK_GAIN", "Genre", "Category",
"XLastScheduled", "EAN/UPC", "Comment", "Record Date", "XEndDate", "XRestrictions"
]
host = 'http://localhost:8082'
# host = 'https://tracks.intergalactic.fm'
query_params = {
# "secret": "secret"
}
if len(sys.argv) <= 1:
sys.exit("no argument passed")
else:
tagpath = sys.argv[1]
# ape = Apev2.from_file('OMYG.apetag')
# ape = Apev2.from_file('MULE DRIVER - Snorer.apetag')
ape = Apev2.from_file(tagpath)
time_list = []
date_list = []
for item in ape.tag.frames:
key = item.item_key
value = item.item_value
# print(key, value)
if key in ignore_tags:
pass
elif key == 'XLastPlayed':
hex = value.hex()
for i in range(0, len(hex), 8):
hex_chunk = "0x{}".format(hex[i: i+8])
d=swap32(int(hex_chunk, 16))
Y,M,D,h,m,s = 1980+(d>>25), (d>>21)&0xf, (d>>16)&0x1f, (d>>11)&0x1f, (d>>5)&0x3f, (d<<1)&0x3f
if Y == 1980:
continue
# print(Y,M,D,h,m,s)
time_list.append("{}:{}:{}".format(h, m, s))
date_list.append("{}-{}-{}".format(Y, M, D))
elif key == "Other":
query_params["country"] = value.decode()
elif key == "Year":
query_params["year"] = value.decode()
elif key == "Title":
query_params["track_name"] = value.decode()
elif key == "Artist":
query_params["artist_name"] = value.decode()
elif key == "Publisher":
query_params["label"] = value.decode()
elif key == "Album":
query_params["release_name"] = value.decode()
elif key == "URL":
query_params["info_url"] = value.decode()
elif key == "URL2":
query_params["img_url"] = value.decode()
else:
print(key, value)
i=0
while i < len(time_list):
# print(time_list[i], date_list[i])
query_params["time"] = time_list[i]
query_params["date"] = date_list[i]
# print(query_params)
response = requests.get(
'{}/spl/1'.format(host),
params=query_params
)
# print(response)
i+=1