diff --git a/README.md b/README.md index 800af22..c43914e 100644 --- a/README.md +++ b/README.md @@ -88,15 +88,14 @@ To use _Spodcast_ you need a (free) _Spotify_ account, if you don't have one yet Here's `spodcast` displaying its help message: ``` $ spodcast -h -usage: spodcast [-h] [-c CONFIG_LOCATION] [-p] [-l LOGIN] [--root-path ROOT_PATH] - [--skip-existing SKIP_EXISTING] [--retry RETRY] - [--max-episodes MAX_EPISODES] [--chunk-size CHUNK_SIZE] - [--download-real-time DOWNLOAD_REAL_TIME] [--language LANGUAGE] - [--credentials-location CREDENTIALS_LOCATION] [--rss RSS] - [--log-level LOG_LEVEL] +usage: spodcast [-h] [-c CONFIG_LOCATION] [-p] [-v] [-l LOGIN] [--root-path ROOT_PATH] + [--skip-existing SKIP_EXISTING] [--retry RETRY] [--max-episodes MAX_EPISODES] + [--chunk-size CHUNK_SIZE] [--download-real-time DOWNLOAD_REAL_TIME] + [--language LANGUAGE] [--credentials-location CREDENTIALS_LOCATION] + [--rss-feed RSS_FEED] [--transcode TRANSCODE] [--log-level LOG_LEVEL] [urls ...] -A caching _Spotify_ podcast to RSS proxy. +A caching Spotify podcast to RSS proxy. positional arguments: urls Download podcast episode(s) from a url. Can take multiple urls. @@ -106,14 +105,15 @@ optional arguments: -c CONFIG_LOCATION, --config-location CONFIG_LOCATION Specify the spodcast.json location -p, --prepare-feed Installs RSS feed server code in ROOT_PATH. + -v, --version show program's version number and exit -l LOGIN, --login LOGIN Reads username and password from file passed as argument and stores credentials for later use. --root-path ROOT_PATH set root path for podcast cache --skip-existing SKIP_EXISTING - skip files with the same name and size - --retry RETRY retry count for _Spotify_ API access + [yes|no] skip files with the same name and size. Defaults to "yes". + --retry RETRY retry count for Spotify API access --max-episodes MAX_EPISODES number of episodes to download --chunk-size CHUNK_SIZE @@ -122,22 +122,30 @@ optional arguments: simulate streaming client --language LANGUAGE preferred content language --credentials-location CREDENTIALS_LOCATION - path to credentials file - --rss RSS add a (php) RSS feed server and related metadata for feed. To serve - the feed, point a web server at the spodcast root path as configured - using --root-path. + path to credentials file. If a relative path is used the file will be + stored in the same directory as the configuration file (-c + /path/to/config.json -> /path/to). + --rss-feed RSS_FEED [yes|no] add a (php) RSS feed server and related metadata for feed. To + manage feeds, point a web server at the spodcast root path as configured + using --root-path. Defaults to "yes". + --transcode TRANSCODE + [yes|no] transcode ogg/vorbis to mp3 (where applicable) - only needed + for devices which do not support open formats (e.g. iOS). Defaults to + "no". --log-level LOG_LEVEL log level (debug/info/warning/error/critical) ``` ### Using _Spodcast_ to proxy _Spotify_ podcasts to RSS -The following example shows how to use the `spodcast` command to prepare the feed root directory and add a _Spotify_ account to be used. It specifies the configuration file to create (`-c /mnt/audio/podcast/spodcast.json`) and the root path where podcasts will be downloaded to (`--root-path /mnt/audio/spodcast`). The `-p` option tells _spodcast_ to prepare the RSS feed server in the root directory which will also be used to store the credential file created by the `-l spotify.rc` command. That `spotify.rc` file is a plain text file containing the username and password (separated by a single space character) to use to login to _Spotify_. It is only needed to create the stored credentials file(s) so it can be deleted once _Spotcast_ is up and running. +The following example shows how to use the `spodcast` command to prepare the feed root directory and add a _Spotify_ account to be used. It specifies the configuration file to create (`-c /mnt/audio/podcast/spodcast.json`) and the root path where podcasts will be downloaded to (`--root-path /mnt/audio/spodcast`). The `-p` option tells _spodcast_ to prepare the RSS feed server in the root directory which will also be used to store the credential file created by the `-l spotify.rc` command. That `spotify.rc` file is a plain text file containing the username and password (separated by a single space character) to use to login to _Spotify_. It is only needed to create the stored credentials file(s) so it can be deleted once _Spotcast_ is up and running. If one or more of your preferred listening devices does not supports open audio codecs - e.g. because it runs iOS - you can use `--transcode yes` to enable transcoding of such streams to `mp3`. Transcoding is performed using _ffmpeg_ which needs to be available on your _Spodcast_ host. It can take a considerable amount of time depending on the hardware you're using to run _Spodcast_ so only enable this option when there are no other options. ``` spodcast -c /mnt/audio/podcast/spodcast.json --root-path /mnt/audio/spodcast -p -l /home/exampleuser/spotify.rc ``` Configure the [Web server](#web-server-configuration) using the path given as root path (in this example that would be `/mnt/audio/spodcast`) as web root, making sure to exclude files with `.json` and `.info` extenstions to avoid leaking your _Spotify_ credentials (even though these are stored in hashed form using hashed file names). Now point a browser at the site you configured for _Spodcast_ and you're ready to add the first show or episode. This is done easily by entering the _Spotify_ show/episode url (e.g. `https://open.spotify.com/show/4rOoJ6Egrf8K2IrywzwOMk` for _The Joe Rogan Experience_ for the whole show, `https://open.spotify.com/episode/2rYwwE7hcpgsDo9vRVHxAI?si=24fb00294b7f40db` for a specific episode, notice the `show` and `episode` parts of these links) and either hitting _Enter_ or clicking the _Add_ button. _Spodcast_ will now create a directory under the given root path, add the `.index.php` RSS feed generator script and the `index.info` show info URL used by that script and the RSS manager script and whatever episode(s) you decided to sync. -Once the initial feed has been created it can be kept up to date by enabling the feed update service found in the _Settings_ menu. Select the update frequency and the start time and click _Update_, this will create a _cron_ job for the web server which will run the _Spodcast_ manager script to update feeds. While the update frequency is configured for all shows simultaneously this is not the case for the number of episodes to _sync_ and the number to _keep_ in cache, these can be configured individually for each show. The idea here is that some shows may publish more than one episode between update intervals so by fetching the last X episodes on each update nothing will be missed. Episodes which have already been synced will not be synced again so no time or bandwidth is wasted. In the same vein the number of episodes to _keep_ can be configured to make sure your RSS clients have the opportunity to download these before they are rotated out of cache. Once more than X (being the value chosen for _keep_) episodes have been downloaded the oldest episodes will be deleted to keep the total no more than X. +Once the initial feed has been created it can be kept up to date by enabling the feed update service found in the _Settings_ menu. Select the update frequency and the start time and click _Update_, this will create a _cron_ job for the web server which will run the _Spodcast_ manager script to update feeds. If the system scheduler can not be used for some reason - e.g. because the web server user is not allowed to create cron jobs or because the web server is run from a Docker container which does not support cron jobs - you can use the _webcron_ endpoint to initiate update runs. Just point any web client at `SPODCAST_URL/?action=update_shows` to run an update and get a json-encoded report on what has been updated. By running a command line web client - _curl_ and _wget_ are good options - as a user cron job at regular intervals the feeds can be kept up to date. + +While the update frequency is configured for all shows simultaneously this is not the case for the number of episodes to _sync_ and the number to _keep_ in cache, these can be configured individually for each show. The idea here is that some shows may publish more than one episode between update intervals so by fetching the last X episodes on each update nothing will be missed. Episodes which have already been synced will not be synced again so no time or bandwidth is wasted. In the same vein the number of episodes to _keep_ can be configured to make sure your RSS clients have the opportunity to download these before they are rotated out of cache. Once more than X (being the value chosen for _keep_) episodes have been downloaded the oldest episodes will be deleted to keep the total no more than X. Point your RSS clients at the _Spodcast_ feed URL for this show and you should see new episodes appear after they were published on _Spotify_ and subsequently picked up on the next update. For the example given in the [Web server requirements](#web-server-configuration) example that URL would be `http://spodcast.example.org/The_Joe_Rogan_Experience`. @@ -151,21 +159,21 @@ Here's what the Spodcast feed manager looks like: The settings screen is simple and concise: -![Spodcast feed manager settings](/resources/screenshots/relevant_part_of_spodcast_settings_screen.png?raw=true "Spodcast feed manager: Settings") +![Spodcast feed manager settings](/resources/screenshots/spodcast_settings.png?raw=true "Spodcast feed manager: Settings") Each show has its own _sync_ and _keep_ settings. Use the _Delete_ button to, well, delete the show. Use _Refresh_ to retrieve the last [_sync_] episodes, skipping those which have already been synced. ![Show controls](/resources/screenshots/sync_keep_delete_refresh.png?raw=true "Configure _sync_ and _keep_ settings for each show") ### Using the _Spodcast_ CLI command to download a single episode -_Spodcast_ can also be used stand-alone (without the need for a web server) by either just ignoring the feed-related files (`.index.php`, `index.info` plus a `*.info` file for every episode) or by disabling the RSS feed using `--rss no` on the command line. Instead of using the `-l spotify.rc` command to add _Spotify_ credentials it is possible to point _Spotcast_ at a single `credentials.json` file (which will be created if it does not exist yet`), `spotcast` wil ask for the username and password when needed. To get single episode links use the _Spotify_ web app and select _Share->Copy Episode Link_ from the episode menu (three dots in the top-right corner of the episode block). The following example shows (an already configured instance of) `spodcast` ready to download a single episode: +_Spodcast_ can also be used stand-alone (without the need for a web server) by either just ignoring the feed-related files (`.index.php`, `index.info` plus a `*.info` file for every episode) or by disabling the RSS feed using `--rss no` on the command line. Instead of using the `-l spotify.rc` command to add _Spotify_ credentials it is possible to point _Spotcast_ at a single `credentials.json` file (which will be created if it does not exist yet`), `spodcast` wil ask for the username and password when needed. To get single episode links use the _Spotify_ web app and select _Share->Copy Episode Link_ from the episode menu (three dots in the top-right corner of the episode block). The following example shows (an already configured instance of) `spodcast` ready to download a single episode: ``` -spodcast -c ~/.config/spodcast/spodcast.json --credentials-location ~/.config/spodcast/credentials.json --rss no https://open.spotify.com/episode/2rYwwE7hcpgsDo9vRVHxAI?si=24fb00294b7f40db +spodcast -c ~/.config/spodcast/spodcast.json --credentials-location ~/.config/spodcast/credentials.json --rss-feed no https://open.spotify.com/episode/2rYwwE7hcpgsDo9vRVHxAI?si=24fb00294b7f40db ``` Like in the previous example _Spodcast_ will create a directory under the root path with the same name as the show from which the episode is downloaded. The episode will be downloaded into this directory under a `SHOW_NAME_-__EPISODE_NAME.[ogg|mp3]` name. Point a mediaplayer of choice at this file to play the episode. In "manual" mode _Spodcast_ does not do anything by itself, feeds can be kept up to date by running _Spotcast_ with the required settings for `--max-episodes` (which is the value used for _sync_ in the web UI) and the show URL. Here's how to update the _The Joe Rogan Experience_ show using the `spodcast` CLI command, syncing the last 3 episodes: ``` -spodcast -c ~/.config/spodcast/spodcast.json --rss no --max-episodes 3 https://open.spotify.com/show/4rOoJ6Egrf8K2IrywzwOMk` +spodcast -c ~/.config/spodcast/spodcast.json --rss-feed no --max-episodes 3 https://open.spotify.com/show/4rOoJ6Egrf8K2IrywzwOMk` ``` ## Web server configuration _Spodcast_ places a hidden `.index.php` file in the root path and each show directory. The one in the root directory is used to manage feeds while those in the show directories produce RSS feeds based on the information found in all `*.info` files in that directory. Configure the server to serve those `.index.php` files as index to make things work as intended. Don't forget to block all web access to files ending in `.json` and `.info` to make sure you _Spotify_ credentials (which are stored in hashed form in files named `spodcast-cred-MD5_HASH_OF_SPOTIFY_USER_NAME.json` in the root path) can not be accessed. For _nginx_ the following should suffice to produce an unencrypted (HTTP) feed under the domain name `spodcast.example.org` given a feed root directory (as configured using `--root-path`) of `/mnt/audio/spodcast` with _php-fpm 7.4_ listening on `unix:/run/php/php7.4-fpm.sock`: diff --git a/dist/spodcast-0.3.5.tar.gz b/dist/spodcast-0.3.5.tar.gz deleted file mode 100644 index d3ca9c7..0000000 Binary files a/dist/spodcast-0.3.5.tar.gz and /dev/null differ diff --git a/dist/spodcast-0.3.6.tar.gz b/dist/spodcast-0.3.6.tar.gz deleted file mode 100644 index 72d98eb..0000000 Binary files a/dist/spodcast-0.3.6.tar.gz and /dev/null differ diff --git a/dist/spodcast-0.4.1.tar.gz b/dist/spodcast-0.4.1.tar.gz new file mode 100644 index 0000000..70de775 Binary files /dev/null and b/dist/spodcast-0.4.1.tar.gz differ diff --git a/resources/screenshots/spodcast_settings.png b/resources/screenshots/spodcast_settings.png new file mode 100644 index 0000000..05f4e15 Binary files /dev/null and b/resources/screenshots/spodcast_settings.png differ diff --git a/setup.cfg b/setup.cfg index 0e24e93..c69c280 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = spodcast -version = 0.3.7 +version = 0.4.1 description = A caching Spotify podcast to RSS proxy. long_description = file:README.md long_description_content_type = text/markdown @@ -21,6 +21,8 @@ packages = spodcast install_requires = librespot >= 0.0.1 + ffmpeg-python + setuptools include_package_data = True diff --git a/spodcast/__main__.py b/spodcast/__main__.py index 3a09209..99d0622 100644 --- a/spodcast/__main__.py +++ b/spodcast/__main__.py @@ -1,11 +1,11 @@ import argparse +import pkg_resources from spodcast.app import client from spodcast.config import CONFIG_VALUES def main(): - parser = argparse.ArgumentParser(prog='spodcast', - description='A caching Spotify podcast to RSS proxy.') + parser = argparse.ArgumentParser(prog='spodcast', description='A caching Spotify podcast to RSS proxy.') parser.add_argument('-c', '--config-location', type=str, help='Specify the spodcast.json location') @@ -14,6 +14,10 @@ def main(): action='store_true', help='Installs RSS feed server code in ROOT_PATH.') + parser.add_argument('-v', '--version', + action='version', + version = '%(prog)s ' + pkg_resources.require("Spodcast")[0].version) + group = parser.add_mutually_exclusive_group(required=True) group.add_argument('urls', type=str, @@ -26,7 +30,6 @@ def main(): type=str, help='Reads username and password from file passed as argument and stores credentials for later use.') - for configkey in CONFIG_VALUES: parser.add_argument(CONFIG_VALUES[configkey]['arg'], type=str, diff --git a/spodcast/config.py b/spodcast/config.py index c8d39b6..73026ff 100644 --- a/spodcast/config.py +++ b/spodcast/config.py @@ -1,5 +1,6 @@ import json import os +import pkg_resources import sys from typing import Any @@ -7,16 +8,20 @@ CONFIG_FILE_PATH = '../spodcast.json' CONFIG_DIR = 'CONFIG_DIR' CONFIG_PATH = 'CONFIG_PATH' +VERSION = 'VERSION' +VERSION_STR = pkg_resources.require("Spodcast")[0].version + ROOT_PATH = 'ROOT_PATH' SKIP_EXISTING_FILES = 'SKIP_EXISTING_FILES' CHUNK_SIZE = 'CHUNK_SIZE' DOWNLOAD_REAL_TIME = 'DOWNLOAD_REAL_TIME' LANGUAGE = 'LANGUAGE' CREDENTIALS_LOCATION = 'CREDENTIALS_LOCATION' -RETRY_ATTEMPTS = 'RETRY_ATTEMPTS' +RETRY = 'RETRY' MAX_EPISODES = 'MAX_EPISODES' LOG_LEVEL = 'LOG_LEVEL' -ENABLE_RSS_FEED = 'ENABLE_RSS_FEED' +RSS_FEED = 'RSS_FEED' +TRANSCODE = 'TRANSCODE' CONFIG_VALUES = { ROOT_PATH: { 'default': '../Spodcast/', @@ -26,16 +31,16 @@ CONFIG_VALUES = { SKIP_EXISTING_FILES: { 'default': 'True', 'type': bool, 'arg': '--skip-existing', - 'help': 'skip files with the same name and size' }, - RETRY_ATTEMPTS: { 'default': '5', + 'help': '[yes|no] skip files with the same name and size. Defaults to "yes".' }, + RETRY: { 'default': 5, 'type': int, 'arg': '--retry', 'help': 'retry count for Spotify API access' }, - MAX_EPISODES: { 'default': '1000', + MAX_EPISODES: { 'default': 1000, 'type': int, 'arg': '--max-episodes', 'help': 'number of episodes to download' }, - CHUNK_SIZE: { 'default': '50000', + CHUNK_SIZE: { 'default': 50000, 'type': int, 'arg': '--chunk-size', 'help': 'download chunk size' }, @@ -50,11 +55,15 @@ CONFIG_VALUES = { CREDENTIALS_LOCATION: { 'default': 'credentials.json', 'type': str, 'arg': '--credentials-location', - 'help': 'path to credentials file' }, - ENABLE_RSS_FEED: { 'default': 'True', + 'help': 'path to credentials file. If a relative path is used the file will be stored in the same directory as the configuration file (-c /path/to/config.json -> /path/to).' }, + RSS_FEED: { 'default': 'True', 'type': bool, - 'arg': '--rss', - 'help': 'add a (php) RSS feed server and related metadata for feed. To serve the feed, point a web server at the spodcast root path as configured using --root-path.' }, + 'arg': '--rss-feed', + 'help': '[yes|no] add a (php) RSS feed server and related metadata for feed. To manage feeds, point a web server at the spodcast root path as configured using --root-path. Defaults to "yes".' }, + TRANSCODE: { 'default': 'False', + 'type': bool, + 'arg': '--transcode', + 'help': '[yes|no] transcode ogg/vorbis to mp3 (where applicable) - only needed for devices which do not support open formats (e.g. iOS). Defaults to "no".' }, LOG_LEVEL: { 'default': 'warning', 'type': str, 'arg': '--log-level', @@ -111,6 +120,7 @@ class Config: cls.Values[CONFIG_DIR] = os.path.dirname(true_config_file_path) cls.Values[CONFIG_PATH] = str(true_config_file_path) + cls.Values[VERSION] = str(VERSION_STR) @classmethod def get_default_json(cls) -> Any: @@ -172,19 +182,24 @@ class Config: @classmethod def get_credentials_location(cls) -> str: - return os.path.join(os.getcwd(), cls.get(CREDENTIALS_LOCATION)) + credentials_location = cls.get(CREDENTIALS_LOCATION) + return credentials_location if os.path.isabs(credentials_location) else os.path.join(cls.get(CONFIG_DIR), cls.get(CREDENTIALS_LOCATION)) @classmethod - def get_retry_attempts(cls) -> int: - return cls.get(RETRY_ATTEMPTS) + def get_retry(cls) -> int: + return cls.get(RETRY) @classmethod def get_max_episodes(cls) -> int: return cls.get(MAX_EPISODES) @classmethod - def get_enable_rss_feed(cls) -> bool: - return cls.get(ENABLE_RSS_FEED) + def get_rss_feed(cls) -> bool: + return cls.get(RSS_FEED) + + @classmethod + def get_transcode(cls) -> bool: + return cls.get(TRANSCODE) @classmethod def get_log_level(cls) -> str: @@ -197,3 +212,11 @@ class Config: @classmethod def get_config_path(cls) -> str: return cls.get(CONFIG_PATH) + + @classmethod + def get_version_str(cls) -> str: + return cls.get(VERSION) + + @classmethod + def get_version_int(cls) -> int: + return int(cls.get(VERSION).replace('.','')) diff --git a/spodcast/feedgenerator.py b/spodcast/feedgenerator.py index 2a534d1..dd99628 100644 --- a/spodcast/feedgenerator.py +++ b/spodcast/feedgenerator.py @@ -1,17 +1,33 @@ +import re + RSS_FEED_FILE_NAME = '.index.php' RSS_FEED_INFO_EXTENSION = 'info' RSS_FEED_SHOW_INDEX = 'index' +RSS_FEED_SHOW_IMAGE = 'image.jpg' +RSS_FEED_VERSION = '$SPODCAST_VERSION$ ' +VERSION_NOT_FOUND = 0 -def RSS_FEED_CODE(): +def get_index_version(filename) -> str: + with open(filename, 'rb') as f: + for line in f.readlines(): + m = re.search(RSS_FEED_VERSION + ' (\d+.\d+.\d+)', str(line)) + if m: + return int(m[1].replace('.','')) + + return VERSION_NOT_FOUND + + +def RSS_FEED_CODE(version): return r'''title; $feed_description=$info->description; - $feed_logo=$info->image; $feed_link=$info->link; } @@ -71,12 +86,14 @@ foreach ($raw_files as &$raw_file) { ''' -def RSS_INDEX_CODE(bin_path, config_name): +def RSS_INDEX_CODE(bin_path, config_name, version): return r''' [options...]".PHP_EOL; echo " commands: ".implode("|",CLI_COMMANDS).PHP_EOL; @@ -98,35 +118,30 @@ if (PHP_SAPI == "cli") { $settings=read(dirname(__FILE__)."/".SETTINGS_INFO); $feeds=read(dirname(__FILE__)."/".FEEDS_INFO); - $SPODCAST_CONFIG=dirname(__FILE__)."/".SPODCAST_CONFIG; $command=$argv[1]; if($command == "refresh") { - foreach ($feeds as $url => ["title"=>$title,"directory"=>$directory,"max"=>$max,"keep"=>$keep]) { - if ($max > 0) { - $output = add_feed($url, $max, true); - echo(implode(PHP_EOL, $output)); - } - $episodes=get_episodes($directory); - if(count($episodes) > $keep) { - $to_delete=array_splice($episodes, $keep); - foreach ($to_delete as $episode) { - system("rm -f ".escapeshellarg($directory."/".$episode['filename'])." 2>&1"); - system("rm -f ".escapeshellarg($directory."/".$episode['filename']).".".INFO." 2>&1"); - } - } + list($retval, $result) = refresh_shows($feeds); + if ($retval > 0) { + echo "An error occurred during refresh, return value was $retval" . PHP_EOL; } + echo implode(PHP_EOL, $result); } die(); } +# CGI/API +$PROTOCOL = (empty($_SERVER['HTTPS'])) ? "http://" : "https://"; +$SPODCAST_URL = $PROTOCOL . $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $feeds=get_feeds(dirname(__FILE__)); $settings=get_settings(); +$config=get_spodcast_config(); $ERROR_MESSAGE=null; $ERROR_DETAILS=null; +$SPODCAST_COMMAND=SPODCAST." -c ".$SPODCAST_CONFIG." --log-level ".$settings['log_level']; function get_feeds($dir) { - $spodcast_url=(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; + global $SPODCAST_URL; foreach(glob($dir."/*/".SHOW_INFO) as $show_info) { $episodes=get_episodes(dirname($show_info)); $json=file_get_contents($show_info); @@ -138,7 +153,7 @@ function get_feeds($dir) { $feeds[$info->link]['directory']=dirname($show_info); $feeds[$info->link]['max']=$info->max ?? 2; $feeds[$info->link]['keep']=$info->keep ?? 5; - $feeds[$info->link]['feed']=$spodcast_url.basename(dirname($show_info)); + $feeds[$info->link]['feed']=$SPODCAST_URL.basename(dirname($show_info)); } uasort($feeds, fn ($a, $b) => strnatcmp($a['title'], $b['title'])); store($feeds, FEEDS_INFO); @@ -146,6 +161,7 @@ function get_feeds($dir) { } function get_episodes($dir) { + $episodes=[]; foreach(glob($dir."/*.".INFO) as $episode_info) { if(basename($episode_info) == SHOW_INFO) { continue; @@ -159,22 +175,29 @@ function get_episodes($dir) { } function get_settings() { + global $SPODCAST_URL; $settings=read(SETTINGS_INFO); - $spodcast_url=(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; - $settings['spodcast_url']=$settings['spodcast_url'] ?? $spodcast_url; + $settings['spodcast_url']=$settings['spodcast_url'] ?? $SPODCAST_URL; $settings['update_start']=$settings['update_start'] ?? 0; $settings['update_rate']=$settings['update_rate'] ?? 1; $settings['update_enabled']=$settings['update_enabled'] ?? false; + $settings['log_level']=$settings['log_level'] ?? LOG_LEVEL; store($settings, SETTINGS_INFO); return $settings; } +function get_spodcast_config() { + global $SPODCAST_CONFIG; + $config=read($SPODCAST_CONFIG); + return $config; +} + function get(&$var, $default=null) { return isset($var) ? $var : $default; } function read($file) { - if(file_exists($file)) { + if(is_readable($file)) { $json=file_get_contents($file); $info=json_decode($json, true); } else { @@ -185,8 +208,9 @@ function read($file) { function store($info, $file) { $f = fopen($file,'w'); - fwrite($f, json_encode($info)); + $result = fwrite($f, json_encode($info)); fclose($f); + return ($result === false) ? ERROR : SUCCESS; } function cron_signature($crontab, $CRON_SIGNATURE) { @@ -207,11 +231,15 @@ function debug($var) { } function submit_crontab($crontab) { - $tempfile=system("mktemp"); + $retval = null; + $output = null; + $tempfile=tempnam(sys_get_temp_dir(), 'spodcast'); file_put_contents($tempfile, implode(PHP_EOL,$crontab)); $command="crontab ".$tempfile; - exec($command); + exec($command, $output, $retval); unlink($tempfile); + + return [$retval, $output]; } function get_range($start, $rate) { @@ -221,6 +249,31 @@ function get_range($start, $rate) { return implode(",", $arr); } +function background_check_id($id) { + $runfile = md5($id).".json"; + $log = md5($id).".log"; + if (is_readable($runfile)) { + $info = read($runfile); + if (background_check($info['pid'])) { + return true; + } + unlink($runfile); + unlink($log); + } + return false; +} + +function background_check($pid) { + try { + $result = shell_exec(sprintf("ps %d", $pid)); + if (count(preg_split("/\n/", $result)) > 2) { + return true; + } + } catch(Exception $e) {} + return false; +} + +# [ status, output ] return functions function update_scheduler($enable, $start, $rate) { $CRON_SIGNATURE="SPODCAST:".dirname(__FILE__); $crontab=null; @@ -233,42 +286,102 @@ function update_scheduler($enable, $start, $rate) { array_splice($crontab, $index, 1); } $crontab[]=sprintf("%d %s * * * php %s refresh # %s".PHP_EOL, rand(5,25), get_range($start, $rate), __FILE__, $CRON_SIGNATURE); - submit_crontab($crontab); + return submit_crontab($crontab); } else { if ($index !== NOT_FOUND) { array_splice($crontab, $index, 1); $crontab[count($crontab)-1]=rtrim($crontab[count($crontab)-1]).PHP_EOL; - submit_crontab($crontab); + return submit_crontab($crontab); } } + } else { + return [$retval, "failed to update scheduler"]; } } function login($username, $password, $return_output=false) { + global $SPODCAST_CONFIG; + global $SPODCAST_COMMAND; $output = null; $retval = null; - $tempfile=system("mktemp"); - $SPODCAST_CONFIG=dirname(__FILE__)."/".SPODCAST_CONFIG; + $tempfile=tempnam(sys_get_temp_dir(), 'spodcast'); file_put_contents($tempfile, "$username $password"); - $command=SPODCAST." -c ".$SPODCAST_CONFIG." --log-level ".LOG_LEVEL." -l ".$tempfile." 2>&1"; + $command=$SPODCAST_COMMAND . " -l ".$tempfile." 2>&1"; exec($command, $output, $retval); unlink($tempfile); - if ($retval > 0 || $return_output) { - return $output; - } - return SUCCESS; + + return [$retval, $output]; } -function add_feed($url, $max, $return_output=false) { +function background_run($command, $id) { + $retval=null; + $md5 = md5($id); + $runfile = $md5.".json"; + $output = $md5.".log"; + $cmd = sprintf("nohup %s > %s 2>&1 & echo $!", $command, $output); + exec($cmd, $pid, $retval); + if ($retval == 0 && count($pid) > 0 && $pid[0] > 0) { + $info['command']=$command; + $info['log']=$output; + $info['pid']=(int) $pid[0]; + store($info, $runfile); + return [$retval, $pid[0]]; + } else { + return [$retval, 0]; + } +} + +function background_add_feed($url, $max) { + global $SPODCAST_CONFIG; + global $SPODCAST_COMMAND; $output = null; $retval = null; - $SPODCAST_CONFIG=dirname(__FILE__)."/".SPODCAST_CONFIG; - $command=SPODCAST." -c ".$SPODCAST_CONFIG." --max-episodes ".$max." ".escapeshellarg($url)." 2>&1"; - exec($command, $output, $retval); - if ($retval > 0 || $return_output) { - return $output; + $command=$SPODCAST_COMMAND . " --max-episodes ".(int)$max." ".escapeshellarg($url); + list($retval, $pid) = background_run($command, $url); + if ($retval == 0 && (int) $pid > 0) { + return [$retval, $pid]; } - return SUCCESS; + return [$retval, 0]; +} + + +function add_feed($url, $max) { + global $SPODCAST_CONFIG; + global $SPODCAST_COMMAND; + $output = null; + $retval = null; + $command=$SPODCAST_COMMAND . " --max-episodes ".(int)$max." ".escapeshellarg($url)." 2>&1"; + exec($command, $output, $retval); + return [$retval, $output]; +} + +function update_feed($url, $max, $keep, $feeds) { + $output = null; + $retval = null; + if ($max > 0) { + list($retval,$output) = add_feed($url, $max); + if ($retval > 0) { + return [$retval, $output]; + } + } + $directory=$feeds[$url]['directory']; + $episodes=get_episodes($directory); + if(count($episodes) > $keep) { + $to_delete=array_splice($episodes, $keep); + foreach ($to_delete as $episode) { + $command="rm -f ".escapeshellarg($directory."/".$episode['filename'])." 2>&1"; + exec($command, $output, $retval); + if ($retval > 0) { + return [$retval, $output]; + } + $command="rm -f ".escapeshellarg($directory."/".$episode['filename']).".".INFO." 2>&1"; + exec($command, $output, $retval); + if ($retval > 0) { + return [$retval, $output]; + } + } + } + return [$retval, $output]; } function delete_feed($url, $return_output=false) { @@ -278,85 +391,214 @@ function delete_feed($url, $return_output=false) { $feed_dir=$feeds[$url]['directory']; $command="rm -rf ".$feed_dir." 2>&1"; exec($command, $output, $retval); - if ($retval > 0 || $return_output) { - return $output; - } - return SUCCESS; + return [$retval, $output]; } function update_show($url, $field, $value) { if (array_search($field, UPDATEABLE) === false) { - return ["$field is not an updateable field"]; + return [1, "$field is not an updateable field"]; } else { $feeds=read(FEEDS_INFO); $show_dir=$feeds[$url]['directory']; $show=read($show_dir."/".SHOW_INFO); - $show[$field]=(int) $value; + $show[$field]=$value; store($show, $show_dir."/".SHOW_INFO); - return SUCCESS; + return [0, "$field set to $value"]; } } +function refresh_shows($feeds) { + $result = []; + $status = 0; + foreach ($feeds as $url => ["title"=>$title, "directory"=>$directory, "max"=>$max, "keep"=>$keep]) { + $output = null; + $retval = 0; + list($retval, $output) = update_feed($url, $max, $keep, $feeds, true); + if ($retval > 0) { + $status = $retval; + } + $result = array_merge($result, $output); + } + return [$return, $result]; +} + +# terminating functions + function show_error($message, $details) { header("Location: ./?action=error&message=".urlencode($message)."&details=".urlencode(implode(PHP_EOL,$details))); die(); } +function json_response($info) { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode($info, true); + exit(); +} + +# CGI commands + switch(get($_GET['action'])) { + case 'refresh': + $url = get($_POST['url']); + $max = (int) get($_POST['max']) ?? MAX_EPISODES; + $keep = (int) get($_POST['keep']) ?? KEEP_EPISODES; + $info['url']=$url; + $info['show']=$feeds[$url]; + $info['id']=basename($url); + if ($max > $keep) { + $info['result'] = "It does not make sense for the number of episodes to refresh to be larger than the number of episodes to keep."; + $info['status'] = 'ERROR'; + } else { + list($result,$output) = update_feed($url, $max, $keep, $feeds, false); + $info['status'] = ($result == 0) ? 'SUCCESS': 'ERROR'; + if ($result != 0) { + $info['result'] = "Refresh failed"; + } + } + json_response($info); + case 'new': - $url = get($_GET['url']); - $result = add_feed($url, MAX_EPISODES); - if ($result !== SUCCESS) { - show_error("Add/refresh feed failed", $result); - } - header("Location: ."); - die(); -case 'delete': - $url = get($_GET['url']); - $result = delete_feed($url); - if ($result !== SUCCESS) { - show_error("Delete feed failed", $result); - } - header("Location: ."); - die(); + $url = get($_POST['url']); + list($result, $output) = background_add_feed($url, MAX_EPISODES, false); + $info['url']=$url; + $info['show']=$feeds[$url] ?? null; + $info['id']=basename($url); + $info['status']= ($result == 0) ? 'SUCCESS': 'ERROR'; + json_response($info); + case 'update': - $url = get($_GET['url']); - $field = get($_GET['field']); - $value = get($_GET['value']); - $result=update_show($url, $field, $value); - if ($result !== SUCCESS) { - show_error("Update failed", $result); + $url = get($_POST['url']); + $field = get($_POST['field']); + $value = get($_POST['value']); + if (in_array(strtolower($field), UPDATEABLE)) { + list($result, $output) = update_show($url, $field, $value); + if ($result == 0) { + $info['result'] = $output; + $info['status'] = 'SUCCESS'; + } else { + $info['result'] = "Update failed"; + $info['status'] = 'ERROR'; + } + } else { + $info['result'] = "$field can not be updated"; + $info['status'] = 'ERROR'; } - header("Location: ."); - die(); -case 'login': - $username = get($_GET['username']); - $password = get($_GET['password']); - $result = login($username, $password); - if ($result !== SUCCESS) { - show_error("Login failed",$result); - } - header("Location: ."); - die(); + + json_response($info); + case 'schedule': - $enable = (get($_GET['enable']) == "true" ? true : false); - $start = (int) get($_GET['start']); - $rate = (int) get($_GET['rate']); + $enable = (get($_POST['enable']) == "true" ? true : false); + $start = (int) get($_POST['start']); + $rate = (int) get($_POST['rate']); $settings['update_enabled'] = $enable; $settings['update_start'] = $start; $settings['update_rate'] = $rate; - store($settings, SETTINGS_INFO); - update_scheduler($enable, $start, $rate); - header("Location: ."); - die(); + $result = store($settings, SETTINGS_INFO); + if ($result === ERROR) { + $info['status'] = 'ERROR'; + $info['result'] = 'Could not store scheduler preferences, giving up'; + } else { + list($result, $output) = update_scheduler($enable, $start, $rate); + if ($result == 0) { + $info['status'] = 'SUCCESS'; + $info['result'] = ($enable === true) ? "Scheduled updates enabled, $rate times per day starting at $start:00" : 'Scheduled updates disabled'; + } else { + $info['status'] = 'ERROR'; + $info['result'] = implode(PHP_EOL, $result); + } + } + json_response($info); + +case 'transcode': + $enable = (get($_POST['enable']) == "true" ? true : false); + $config['TRANSCODE']=($enable) ? 'True' : 'False'; + $result = store($config, $SPODCAST_CONFIG); + if ($result === SUCCESS) { + $info['status'] = 'SUCCESS'; + $info['result'] = ($enable) ? 'Transcoding enabled' : 'Transcoding disabled'; + } else { + $info['status'] = 'ERROR'; + $info['result'] = 'Could not enable transcoding: can not write to config file'; + } + json_response($info); + +case 'logging': + $level = get($_POST['level']) ?? LOG_LEVEL; + $config['LOG_LEVEL']=$level; + if (in_array(strtolower($level), ['critical','error','warning','info','debug'])) { + $result = store($config, $SPODCAST_CONFIG); + if ($result === SUCCESS) { + $info['status'] = 'SUCCESS'; + $info['result'] = 'Log level set to '. $level; + } else { + $info['status'] = 'ERROR'; + $info['result'] = 'Could not change log level: can not write to Spodcast config file'; + } + } else { + $info['status'] = 'ERROR'; + $info['result'] = 'Invalid log level ' . $level; + } + json_response($info); + +case 'status': + $url = get($_POST['url']); + $info['url'] = $url; + $info['show']=$feeds[$url] ?? null; + $info['id'] = basename($url); + $info['status'] = background_check_id($url) ? 'ACTIVE' : 'READY'; + json_response($info); + +case 'delete': + $url = get($_POST['url']); + $info['url'] = $url; + $info['id'] = basename($url); + list($result, $output) = delete_feed($url); + if ($result !== 0) { + $info['status'] = 'ERROR'; + $info['result'] = "Delete feed failed: " . implode(PHP_EOL, $output); + } else { + $info['status'] = 'SUCCESS'; + $info['result'] = "Deleted " . $feeds[$url]['title'] . "" ; + } + json_response($info); + +case 'login': + $username = get($_POST['username']); + $password = get($_POST['password']); + list($result, $output) = login($username, $password); + if ($result === 0) { + $info['status'] = 'SUCCESS'; + $info['result'] = 'Login succeeded'; + } else { + $info['status'] = 'ERROR'; + $info['result'] = 'Login failed for user '. $username; + } + json_response($info); + +case 'update_shows': + list($result, $output) = refresh_shows($feeds); + $info['status'] = ($result == 0) ? 'SUCCESS' : 'ERROR'; + $info['result'] = $output; + json_response($info); + case 'error': - $ERROR_MESSAGE = get($_GET['message']); - $ERROR_DETAILS = get($_GET['details']); + $ERROR_MESSAGE = get($_POST['message']); + $ERROR_DETAILS = get($_POST['details']); default: break; } +$ACTIVE=[]; + +foreach (array_keys($feeds) as $url) { + if (background_check_id($url)) { + $ACTIVE[$url]=true; + } +} + +$TRANSCODE_ENABLED=($config['TRANSCODE'] == "True") ? true : false ; +$LOG_LEVEL=$config['LOG_LEVEL']; $UPDATE_ENABLED=$settings['update_enabled']; $UPDATE_START=$settings['update_start']; $UPDATE_RATE=$settings['update_rate']; @@ -373,11 +615,13 @@ $UPDATE_RATE=$settings['update_rate']; div#loading { position: fixed; display: flex; width: 100%; height: 100%; top: 0; left: 0; opacity: 0.8; background-color: #fff; z-index: 99; justify-content: center; align-items: center; font-size: 200%; } div#settings, div#error { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 99; opacity: 0.95; margin: 0.5em auto; } div.hidden { visibility: hidden; } - .episode, .setgroup, .error { display: flex; flex-wrap: wrap; margin: 0 0 2em 0; padding: 0; font-family: sans-serif; border: 1px solid #F0F0F0; background: #F0F0F0; border-radius: 6px; } + div.overlay { position: absolute; width: 100%; height: 100%; left: 0; right: 0; background: white; opacity: 0.5; z-index: 100; display: flex; justify-content: center; align-items: center; } + .show, .setgroup, .error { position: relative; display: flex; flex-wrap: wrap; margin: 0 0 2em 0; padding: 0; font-family: sans-serif; border: 1px solid #F0F0F0; background: #F0F0F0; border-radius: 6px; } + .show .placeholder { height: 4em; } .setgroup { border: 1px solid black; margin: 1em; } .error { border: 2px solid red; margin: 1em; } .entry, .setitem, .errorline { display: flex; box-sizing: border-box; flex-grow: 1; width: 100%; padding: 0.4em 0.6em; overflow: hidden; align-items: center; text-align: left; background: #F0F0F0; } - .comment { font-size: 80%; font-style: italic; } + .comment { font-size: 80%; font-style: italic;} .entry > a { text-decoration: none; color: black; } .entry > a:hover { color: red; } .title { background: linear-gradient(90deg, #E0E0E0, #F0F0F0); border-radius: 6px; } @@ -396,11 +640,19 @@ $UPDATE_RATE=$settings['update_rate']; #delete:hover { color: white; background: red; } #update-settings { background: inherit; } - legend { margin-left: 1.5em; font-size: 120%; border: 1px solid black; border-radius: 6px; background: white; } + .loader { display: inline-block; width: 4em; height: 4em; } + .loader:after { content: " "; display: block; width: 2em; height: 2em; margin: 8px; border-radius: 50%; border: 6px solid black; border-color: black transparent black transparent; animation: loader 1.2s linear infinite; } + @keyframes loader { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } + + #toast { position: absolute; width: 33%; left: 33%; bottom: 2em; padding: 2em; border-radius: 6px; background: #F0F0F0; font-size: 1.2em; font-family: sans-serif; z-index: 200; text-align: center;} + #toast.success { border: 1px solid black; } + #toast.error { border: 6px solid red; } + + legend { margin-left: 1.5em; font-size: 100%; border: 1px solid black; border-radius: 6px; background: white; padding: 0.1em 0.5em; } .error legend { border: 2px solid red; } @media all and (max-width: 500px) { - .episode { display: block; } + .show { display: block; } .entry { width: 95% !important; } .last { margin-bottom: 1em; } .actions { flex-direction: column; align-items: flex-end; } @@ -416,23 +668,17 @@ $UPDATE_RATE=$settings['update_rate'];
["title"=>$title,"image"=>$image,"episodes"=>$episodes,"last"=>$last,"max"=>$max,"keep"=>$keep,"feed"=>$feed]): ?> -
+
+
">
- +
-
 ()
+
 ()
  
-
sync 
keep  
delete
refresh
+
sync 
keep  
delete
refresh
- + ''' + diff --git a/spodcast/podcast.py b/spodcast/podcast.py index 57e1cf3..2a09db1 100644 --- a/spodcast/podcast.py +++ b/spodcast/podcast.py @@ -7,8 +7,10 @@ import urllib.parse from librespot.metadata import EpisodeId +import ffmpeg + from spodcast.const import ERROR, ID, ITEMS, NAME, SHOW, DURATION_MS, DESCRIPTION, RELEASE_DATE, URI, URL, EXTERNAL_URLS, IMAGES, SPOTIFY, FILE_EXISTS -from spodcast.feedgenerator import RSS_FEED_CODE, RSS_FEED_FILE_NAME, RSS_FEED_SHOW_INDEX, RSS_FEED_INFO_EXTENSION +from spodcast.feedgenerator import RSS_FEED_CODE, RSS_FEED_FILE_NAME, RSS_FEED_SHOW_INDEX, RSS_FEED_INFO_EXTENSION, RSS_FEED_SHOW_IMAGE, RSS_FEED_VERSION, get_index_version from spodcast.spotapi import EPISODE_INFO_URL, SHOWS_URL, EPISODE_DOWNLOAD_URL, ANON_PODCAST_DOMAIN from spodcast.utils import clean_filename from spodcast.spodcast import Spodcast @@ -70,6 +72,8 @@ def download_file(url, filepath): import shutil import requests + mimetype = "audio/mpeg" + r = requests.get(url, stream=True, allow_redirects=True) if r.status_code != 200: r.raise_for_status() # Will only raise for 4xx codes, so... @@ -83,24 +87,29 @@ def download_file(url, filepath): and abs(file_size - os.path.getsize(filepath)) < 1000 and Spodcast.CONFIG.get_skip_existing_files() ): - return filepath, FILE_EXISTS + return filepath, FILE_EXISTS, mimetype log.info("Downloading file") r.raw.read = functools.partial(r.raw.read, decode_content=True) with open(filepath, "wb") as file: shutil.copyfileobj(r.raw, file) - return filepath, os.path.getsize(filepath) + + return filepath, os.path.getsize(filepath), mimetype def download_stream(stream, filepath): size = stream.input_stream.size + mp3_filepath = os.path.splitext(filepath)[0] + ".mp3" + mimetype = "audio/ogg" if ( - os.path.isfile(filepath) - and abs(size - os.path.getsize(filepath)) < 1000 + ((os.path.isfile(filepath) + and abs(size - os.path.getsize(filepath)) < 1000) + or (Spodcast.CONFIG.get_transcode() + and os.path.isfile(mp3_filepath))) and Spodcast.CONFIG.get_skip_existing_files() ): - return filepath, FILE_EXISTS + return filepath, FILE_EXISTS, mimetype log.info("Downloading stream") time_start = time.time() @@ -117,7 +126,18 @@ def download_stream(stream, filepath): if delta_want > delta_real: time.sleep(delta_want - delta_real) - return filepath, downloaded + if Spodcast.CONFIG.get_transcode(): + log.info("transcoding ogg->mp3") + transcoder = ffmpeg.input(filepath) + transcoder = ffmpeg.output(transcoder, mp3_filepath) + ffmpeg.run(transcoder, quiet=True) + file.close() + os.unlink(filepath) + filepath = mp3_filepath + downloaded = os.path.getsize(filepath) + mimetype = "audio/mpeg" + + return filepath, downloaded, mimetype def download_episode(episode_id) -> None: @@ -140,48 +160,52 @@ def download_episode(episode_id) -> None: stream = Spodcast.get_content_stream(episode_stream_id, Spodcast.DOWNLOAD_QUALITY) basename = f"{filename}.ogg" filepath = os.path.join(show_directory, basename) - path, size = download_stream(stream, filepath) - mimetype="audio/ogg" + path, size, mimetype = download_stream(stream, filepath) + basename = os.path.basename(path) # may have changed due to transcoding else: basename=f"{filename}.mp3" filepath = os.path.join(show_directory, basename) - path, size = download_file(download_url, filepath) - mimetype="audio/mpeg" + path, size, mimetype = download_file(download_url, filepath) if size == FILE_EXISTS: log.info(f"Skipped {podcast_name}: {episode_name}") - return else: log.warning(f"Downloaded {podcast_name}: {episode_name}") - if Spodcast.CONFIG.get_enable_rss_feed(): - episode_info = { - "mimetype": mimetype, - "medium": "audio", - "duration": int(duration_ms/1000), - "date": time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.strptime(release_date, "%Y-%m-%d")), - "title": escape(episode_name), "guid": uri, "description": escape(description), - "filename": urllib.parse.quote(basename), - "size": int(size) } - info_file = open(os.path.join(show_directory, f"{basename}.{RSS_FEED_INFO_EXTENSION}"), "w") - info_file.write(json.dumps(episode_info)) - info_file.close() + if Spodcast.CONFIG.get_rss_feed(): + episode_info = { + "mimetype": mimetype, + "medium": "audio", + "duration": int(duration_ms/1000), + "date": time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.strptime(release_date, "%Y-%m-%d")), + "title": escape(episode_name), "guid": uri, "description": escape(description), + "filename": urllib.parse.quote(basename), + "size": int(size) } + info_file = open(os.path.join(show_directory, f"{basename}.{RSS_FEED_INFO_EXTENSION}"), "w") + info_file.write(json.dumps(episode_info)) + info_file.close() + if Spodcast.CONFIG.get_rss_feed(): show_index_file_name = os.path.join(show_directory, f"{RSS_FEED_SHOW_INDEX}.{RSS_FEED_INFO_EXTENSION}") - if not os.path.isfile(show_index_file_name): + if not os.path.isfile(show_index_file_name) or int(get_index_version(show_index_file_name)) < Spodcast.CONFIG.get_version_int(): podcast_name, link, description, image = get_info(episode_id, "show") show_info = { + "version": str(RSS_FEED_VERSION + Spodcast.CONFIG.get_version_str()), "title": escape(podcast_name), "link": link, "description": escape(description), - "image": image } + "image": RSS_FEED_SHOW_IMAGE } show_index_file = open(show_index_file_name, "w") show_index_file.write(json.dumps(show_info)) show_index_file.close() + show_image_name = os.path.join(show_directory, f"{RSS_FEED_SHOW_IMAGE}") + if not os.path.isfile(show_image_name): + download_file(image, show_image_name) + rss_file_name = os.path.join(show_directory, RSS_FEED_FILE_NAME) - if not os.path.isfile(rss_file_name): + if not os.path.isfile(rss_file_name) or int(get_index_version(rss_file_name)) < Spodcast.CONFIG.get_version_int(): rss_file = open(rss_file_name, "w") - rss_file.write(RSS_FEED_CODE()) + rss_file.write(RSS_FEED_CODE(Spodcast.CONFIG.get_version_str())) rss_file.close() diff --git a/spodcast/spodcast.py b/spodcast/spodcast.py index 46742e9..5ab2564 100755 --- a/spodcast/spodcast.py +++ b/spodcast/spodcast.py @@ -15,7 +15,7 @@ from librespot.core import Session from spodcast.config import Config from spodcast.const import CREDENTIALS_PREFIX, TYPE, USER_READ_EMAIL, OFFSET, LIMIT -from spodcast.feedgenerator import RSS_FEED_FILE_NAME, RSS_INDEX_CODE +from spodcast.feedgenerator import RSS_FEED_FILE_NAME, RSS_INDEX_CODE, get_index_version class Spodcast: SESSION: Session = None @@ -34,9 +34,9 @@ class Spodcast: os.makedirs(root_path, exist_ok=True) if os.path.exists(root_path): index_file_name = os.path.join(root_path, RSS_FEED_FILE_NAME) - if not os.path.isfile(index_file_name): + if not os.path.isfile(index_file_name) or int(get_index_version(index_file_name)) < Spodcast.CONFIG.get_version_int(): rss_file = open(index_file_name, "w") - rss_file.write(RSS_INDEX_CODE(Spodcast.CONFIG.get_bin_path(), os.path.basename(Spodcast.CONFIG.get_config_path()))) + rss_file.write(RSS_INDEX_CODE(Spodcast.CONFIG.get_bin_path(), os.path.basename(Spodcast.CONFIG.get_config_path()), Spodcast.CONFIG.get_version_str())) rss_file.close() else: sys.exit(f"Can not create root path {root_path}") @@ -142,7 +142,7 @@ class Spodcast: responsejson = response.json() if 'error' in responsejson: - if tryCount < (cls.CONFIG.get_retry_attempts() - 1): + if tryCount < (cls.CONFIG.get_retry() - 1): Spodcast.LOG.warning(f"Spotify API Error (try {tryCount + 1}) ({responsejson['error']['status']}): {responsejson['error']['message']}") time.sleep(5) return cls.invoke_url(url, tryCount + 1) diff --git a/spodcast/utils.py b/spodcast/utils.py index 29d916b..ecba4f0 100644 --- a/spodcast/utils.py +++ b/spodcast/utils.py @@ -4,7 +4,7 @@ import unicodedata from enum import Enum from typing import List, Tuple -from .spodcast import Spodcast +from spodcast.spodcast import Spodcast valid_filename_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)