1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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)
|