From 494822935bcf9eda231e78e2feaa1033640f13ba Mon Sep 17 00:00:00 2001 From: zuckerberg <5-zuckerberg@users.noreply.git.neet.dev> Date: Tue, 25 May 2021 06:53:54 -0400 Subject: [PATCH] initial commit --- default.nix | 8 ++++++++ downloader.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ flake.lock | 41 +++++++++++++++++++++++++++++++++++++ flake.nix | 18 +++++++++++++++++ radio.py | 8 ++++++++ setup.py | 10 +++++++++ 6 files changed, 141 insertions(+) create mode 100644 default.nix create mode 100644 downloader.py create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 radio.py create mode 100644 setup.py diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..51dac78 --- /dev/null +++ b/default.nix @@ -0,0 +1,8 @@ +{ pkgs ? import { }, self ? ./. }: + +pkgs.python3Packages.buildPythonApplication { + pname = "radio"; + src = self; + version = "0.1"; + propagatedBuildInputs = with pkgs.python3Packages; [ pip ffmpeg-python youtube-dl ]; +} \ No newline at end of file diff --git a/downloader.py b/downloader.py new file mode 100644 index 0000000..beaf4fe --- /dev/null +++ b/downloader.py @@ -0,0 +1,56 @@ +# +# Downloads the video/audio as a stream from a provided link using youtube-dl +# does not save the file, only the most recent fragment is held. Thus, this is +# ideal for devices with little memory +# TODO gather video metadata before download +# + +# dirpath = tempfile.mkdtemp() +# sys.path.append(dirpath) +# def import_from_pip(package): +# pip.main(['install', '--target=' + dirpath, '--upgrade', package]) +# return __import__(package) +# youtube_dl = import_from_pip('youtube_dl') + +import tempfile +import pip +import sys +import youtube_dl +import subprocess + +dirpath = tempfile.mkdtemp() +sys.path.append(dirpath) + +BUFFER_SIZE = 1024 + +def execute(cmd): + popen = subprocess.Popen(cmd, stdout=subprocess.PIPE) + # monitor the stdout + for chunk in iter(lambda: popen.stdout.read(BUFFER_SIZE), b''): + yield chunk + popen.stdout.close() + popen.wait() + +def updateYoutubeDL(): + pip.main(['install', '--target=' + dirpath, '--upgrade', package]) + +def download(url): + # update youtube-dl + # TODO: do this only every once in a while + #updateYoutubeDL() + + # start downloader so that it's stdout (with fragments) may be captured + for s in execute(["python3","downloader.py",url]): + print(s) + +def runYoutubeDL(url): + ydl_opts = { + 'logtostderr': True, + 'outtmpl': '-', + } + + with youtube_dl.YoutubeDL(ydl_opts) as ydl: + ydl.download([url]) + +if __name__ == "__main__": + runYoutubeDL(sys.argv[1]) \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5a6f079 --- /dev/null +++ b/flake.lock @@ -0,0 +1,41 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1620759905, + "narHash": "sha256-WiyWawrgmyN0EdmiHyG2V+fqReiVi8bM9cRdMaKQOFg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b543720b25df6ffdfcf9227afafc5b8c1fabfae8", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1621784194, + "narHash": "sha256-CQWN/QvVHG8qCn7UhGGwoT3jAPvnJHQUvzBlIt48FGs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c5265c01a944b1cecfcfab392d5204d73d65d4ec", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..7e1f5c0 --- /dev/null +++ b/flake.nix @@ -0,0 +1,18 @@ +{ + description = "An internet radio service"; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let pkgs = nixpkgs.legacyPackages.${system}; in + rec { + packages = flake-utils.lib.flattenTree { + radio = import ./default.nix { inherit pkgs; inherit self; }; + }; + defaultPackage = packages.radio; + apps.radio = flake-utils.lib.mkApp { drv = packages.radio; }; + defaultApp = apps.radio; + } + ); +} diff --git a/radio.py b/radio.py new file mode 100644 index 0000000..aeea366 --- /dev/null +++ b/radio.py @@ -0,0 +1,8 @@ +import downloader + +def run(): + downloader.download('https://www.youtube.com/watch?v=BaW_jenozKc') + downloader.download('https://www.youtube.com/watch?v=kgBcg4uBd9Q') + +if __name__ == "__main__": + run() \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..69316ab --- /dev/null +++ b/setup.py @@ -0,0 +1,10 @@ +from setuptools import setup + +setup( + name='radio', + version='0.1', + py_modules=['radio'], + entry_points={ + 'console_scripts': ['radio = radio:run'] + }, +) \ No newline at end of file