Python Temp

From bernie's
Jump to navigation Jump to search
import json
#from urllib import urlopen
from urllib.request import urlopen
import os
import datetime
from pathlib import Path

url = ""
localfile = "playlist.txt"
fileId = Path(localfile)

def lastline(file):
	''' '''
	with open(file, 'rb') as f:
	    try:  # catch OSError in case of a one line file , os.SEEK_END)
	        while != b'\n':
	  , os.SEEK_CUR)
	    except OSError:
	    return f.readline().decode()

sep = " | "
lastrecord = lastline(localfile)
date ="%y%m%d")

with urlopen(url) as content:
	data = json.loads(
	for el in reversed(data):
		record = (f"{date}{sep}{el['time']}{sep}{el['artist']}{sep}{el['titre']}\n")
		if record is not lastrecord:
			with open(localfile, 'a') as recordlist: