summary refs log tree commit diff stats
path: root/podweb.py
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2024-01-21 16:40:38 -0800
committerComradeCrow <comradecrow@vivaldi.net>2024-01-21 16:40:38 -0800
commit6c9d35c7910254d0c9d0536a8661d1d22ccdbb2c (patch)
tree89d6dc89575a3af1ffbbf3f8e3074359075a769c /podweb.py
downloadpodweb-6c9d35c7910254d0c9d0536a8661d1d22ccdbb2c.tar.gz
initial commit
Diffstat (limited to 'podweb.py')
-rwxr-xr-xpodweb.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/podweb.py b/podweb.py
new file mode 100755
index 0000000..4ac83ea
--- /dev/null
+++ b/podweb.py
@@ -0,0 +1,102 @@
+#!/usr/bin/env python3
+import os
+import copy
+import click
+import yaml
+import xml
+
+import podcastparser
+import urllib
+
+global options
+options = {"DEBUG": False, "serverlist": os.path.normpath(os.path.join(os.path.expanduser("~/.config/podweb"), "serverlist")), "podcastpath": ""}
+
+class PodWeb():
+    
+    def __init__(self, DEBUG : bool = False) -> None:
+        self.options = options
+        self.options.update({"DEBUG": DEBUG})
+        self.servers = []
+        self.DEFAULT_SERVERLIST_HEADING = '''## You can add podcast xml feeds here.  
+## You can also optionally add categories and names for the podcasts.
+## The order of category, name, and url does not matter.
+## Here are some example entries:
+##  - category: example category
+##    name: example podcast 1
+##    url: https://example.com/feed.xml
+##  - name: example podcast 2
+##    url: example.com/feed2.xml
+'''
+
+        if self.options["DEBUG"]:
+            self.config_path = "."
+            self.config_filepath = "debug_config.yaml"
+            self.options["serverlist"] = "debug_serverlist"
+        else:
+            self.config_path = os.path.normpath(os.path.expanduser("~/.config/podweb"))
+            self.config_filepath = os.path.join(self.config_path, "config.yaml")
+
+        self._load_config()
+        self._load_serverlist()
+
+    def _load_config(self) -> None:
+
+        if not os.path.exists(self.config_path): os.makedirs(self.config_path)
+
+        if not os.path.isfile(self.config_filepath):
+            with open(self.config_filepath, "w+") as f:
+                yaml.dump(self.options, f)
+        
+        else:
+            with open(self.config_filepath, "r+") as f:
+                self.options.update(yaml.full_load(f))
+
+    def _update_config(self, changed_option : dict) -> None:
+        '''Makes a change to the config file'''
+        with open(self.options["serverlist"], "w+") as f:
+            config_options = yaml.full_load(f)
+            config_options.update(changed_option)
+            f.write(config_options)
+
+    def _load_serverlist(self) -> list:
+        '''Loads the contents of the serverlist'''
+        self._create_serverlist()
+        with open(self.options["serverlist"], "r+") as f:
+            content = yaml.full_load(f)
+        if content:
+            self.servers = content
+
+    def _create_serverlist(self) -> None:
+        '''Checks if the serverlist does not exist and creates it if not'''
+        if not os.path.isfile(self.options["serverlist"]):
+            with open(self.options["serverlist"], "w+") as f:
+                f.write(self.DEFAULT_SERVERLIST_HEADING)
+
+    def _update_serverlist(self) -> None:
+        '''This is destructive and overwrites the current serverlist with the stored serverlist'''
+        with open(self.options["serverlist"], "w+") as f:
+            f.write(self.DEFAULT_SERVERLIST_HEADING)
+        if len(self.servers):
+            with open(self.options["serverlist"], "a") as f:
+                yaml.dump(self.servers, f)
+    
+    def add_podcast(self, feedurl : str, name = None, category = None) -> None:
+        for i in self.servers:
+            iparse = urllib.parse.urlparse(i["url"])
+            feedparse = urllib.parse.urlparse(feedurl)
+            if iparse.hostname == feedparse.hostname and iparse.path == feedparse.path:
+                return None
+        parsed = podcastparser.parse(feedurl, urllib.request.urlopen(feedurl))
+        if name:
+            new_feed = {"url": feedurl, "name": name}
+        else:
+            new_feed = {"url": feedurl, "name": parsed["title"]}
+        if category: new_feed.update({"category": category})
+        self.servers.append(new_feed)
+        self._update_serverlist()
+
+    def import_opml(self, opml_path : str):
+        pass
+
+
+testweb = PodWeb(DEBUG = True)
\ No newline at end of file