diff options
Diffstat (limited to 'radio')
-rwxr-xr-x | radio/radio.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/radio/radio.py b/radio/radio.py new file mode 100755 index 0000000..2c04056 --- /dev/null +++ b/radio/radio.py @@ -0,0 +1,95 @@ +#!/usr/bin/python3 +from sys import path +path.append("..") +from travnik import Torrent +import inotify.adapters +from threading import Thread +from random import randrange +from os import stat +from time import sleep + +import libtorrent + +def random_file(torrent, save_path, endung=".flac"): + def get_index_from_endung(chosen): + for i in range(torrent.torrent_file().files().num_files()): + if (torrent.torrent_file().files().file_path(i).endswith(endung)): + if chosen == 0: + return i + break + chosen -= 1 + prios = [] + for i in torrent.get_file_priorities(): + prios.append(0) + endungs = 0 + for i in range(torrent.torrent_file().files().num_files()): + if (torrent.torrent_file().files().file_path(i).endswith(endung)): + endungs += 1 + if endungs == 0: + return False + chosen = randrange(endungs) + tries_left = endungs + while True: + if tries_left == 0: + return False + tries_left -= 1 + try: + stat(save_path + "/" + torrent.torrent_file().files().file_path(get_index_from_endung(chosen))) + except FileNotFoundError: + chosen = (chosen + 1) % endungs; + break + else: + continue + prios[get_index_from_endung(chosen)] = 4 + torrent.prioritize_files(prios) + print(f"random file {torrent.torrent_file().files().file_path(get_index_from_endung(chosen))} chosen to download") + +def watcher(session, torrents, d="/tmp/", s=".", active_torrents=10): + i = inotify.adapters.Inotify() + i.add_watch(d) + for event in i.event_gen(yield_nones=False): + (_, type_names, path, filename) = event + if "IN_CLOSE_WRITE" in type_names and filename.endswith(".torrent"): + t = Torrent() + t.file(path + filename) + for pth, size in t.paths(): + if pth[-1].endswith(b'.flac'): + info = libtorrent.torrent_info(path + filename) + torr = session.add_torrent({"ti": info, "save_path": s}) + random_file(torr, save_path=s); + torrents.append(torr) + if len(torrents) > active_torrents: + session.remove_torrent(torrents[0]) + del torrents[0] + break + +def alerts(session): + while True: + session.wait_for_alert(86400*1000*21) + for a in session.pop_alerts(): + if a.category() & libtorrent.alert.category_t.error_notification: + print(f"error notification alert from libtorrent: {a}") + if type(a) == libtorrent.file_completed_alert: + print(f"file completed alert: {a}") + if type(a) in [libtorrent.external_ip_alert, libtorrent.listen_succeeded_alert, libtorrent.dht_get_peers_alert, libtorrent.dht_outgoing_get_peers_alert, libtorrent.dht_reply_alert, libtorrent.torrent_added_alert, libtorrent.state_changed_alert, libtorrent.torrent_resumed_alert, libtorrent.stats_alert, libtorrent.torrent_checked_alert, libtorrent.peer_connect_alert, libtorrent.alert, libtorrent.peer_disconnected_alert, libtorrent.incoming_connection_alert, libtorrent.block_downloading_alert, libtorrent.dht_bootstrap_alert, libtorrent.dht_announce_alert, libtorrent.block_finished_alert, libtorrent.piece_finished_alert, libtorrent.peer_unsnubbed_alert, libtorrent.peer_snubbed_alert, libtorrent.portmap_error_alert]: + continue + print(f"{type(a)} | {a}") + +def progress(session, torrents): + while True: + for torr in torrents: + s = torr.status() + print(torr.torrent_file().name() + '\t%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s' % (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, s.state)) + sleep(1) +if __name__ == "__main__": + session = libtorrent.session({"alert_mask": libtorrent.alert.category_t.all_categories}); + torrents = [] + w = Thread(target=watcher, args=(session, torrents)) + a = Thread(target=alerts, args=(session,)) + p = Thread(target=progress, args=(session, torrents)) + w.start() + a.start() + p.start() + w.join() + a.join() + p.join() |