summary refs log blame commit diff stats
path: root/podweb.py
blob: 4ac83eab560ff4127984e7f2d776371c6544cde6 (plain) (tree)





































































































                                                                                                                                                 
#!/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)