summaryrefslogtreecommitdiffstats
path: root/skripti/live_hls_archiver.py
blob: f3d14989382cc4df110a4897c6723ee20d8ade2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python3
# skript je napisan za rtv stream in pričakuje na argv[1] tak hls, kot ga dobimo na http://z.4a.si/rtv.php?kanal=tv.slo1
# shranjuje v trenutni direktorij
from sys import argv
from time import sleep
from os import path, rename
import requests
import m3u8
def main():
	while True:
		playlist = m3u8.loads(requests.get(argv[1]).text, uri=argv[1])
		for segment in playlist.segments:
			filename = segment.program_date_time.isoformat() + "." + next(reversed(segment.uri.split("?")[0].split(".")));
			if not path.exists(filename):
				print("prenašam " + filename)
				temp_filename = "temp_"+argv[0].replace("/", "")
				open(temp_filename, "wb").write(requests.get(segment.uri).content)
				rename(temp_filename, filename)
		try:
			sleep(playlist.target_duration)
		except TypeError:
			sleep(1)
if __name__ == "__main__":
	main()