aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/local_playlist.py
diff options
context:
space:
mode:
authorJames Taylor <user234683@users.noreply.github.com>2018-10-30 14:58:08 -0700
committerJames Taylor <user234683@users.noreply.github.com>2018-10-30 14:58:08 -0700
commitf982273f850080109ce4887e5998770c0b3e339b (patch)
treeed33c33a848e889819340997aa2627dfbdb95a08 /youtube/local_playlist.py
parent316dce9f033155f327cf6ca9c97ae168eb844345 (diff)
downloadyt-local-f982273f850080109ce4887e5998770c0b3e339b.tar.lz
yt-local-f982273f850080109ce4887e5998770c0b3e339b.tar.xz
yt-local-f982273f850080109ce4887e5998770c0b3e339b.zip
Don't add duplicates to playlists
Diffstat (limited to 'youtube/local_playlist.py')
-rw-r--r--youtube/local_playlist.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/youtube/local_playlist.py b/youtube/local_playlist.py
index 3c62134..18857e5 100644
--- a/youtube/local_playlist.py
+++ b/youtube/local_playlist.py
@@ -8,12 +8,22 @@ playlists_directory = os.path.normpath("data/playlists")
with open('yt_local_playlist_template.html', 'r', encoding='utf-8') as file:
local_playlist_template = Template(file.read())
+def video_ids_in_playlist(name):
+ try:
+ with open(os.path.join(playlists_directory, name + ".txt"), 'r', encoding='utf-8') as file:
+ videos = file.read()
+ return set(json.loads(video)['id'] for video in videos.splitlines())
+ except FileNotFoundError:
+ return set()
+
def add_to_playlist(name, video_info_list):
if not os.path.exists(playlists_directory):
os.makedirs(playlists_directory)
+ ids = video_ids_in_playlist(name)
with open(os.path.join(playlists_directory, name + ".txt"), "a", encoding='utf-8') as file:
for info in video_info_list:
- file.write(info + "\n")
+ if json.loads(info)['id'] not in ids:
+ file.write(info + "\n")
def get_local_playlist_page(name):