Skip to content

Instantly share code, notes, and snippets.

@SREENATHPGS
Created July 25, 2022 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SREENATHPGS/72578cead95290838900fb10dfda560d to your computer and use it in GitHub Desktop.
Save SREENATHPGS/72578cead95290838900fb10dfda560d to your computer and use it in GitHub Desktop.
A random partially tested python class file.
import string, random, subprocess, traceback
class UnSuccesfulCommandExecution(Exception):
pass
class Playlist:
playlist = list()
urls = list()
def __init__(self, name):
self.name = name
def id_generator(self, size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def add(self, video_url, local_path):
video_id = self.id_generator(8)
if not video_url in self.urls:
item = {"uid":video_id, "url": video_url, "duration": self.duration(local_path)}
self.playlist.append(item)
return item
def duration(self, path):
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", path],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if result.returncode == 0:
return float(result.stdout)
else:
traceback.print_exc()
raise UnSuccesfulCommandExecution("Error in getting duration. Aborting.")
def getNext(self, video_id):
nextItem = None
for index, item in enumerate(self.playlist):
if video_id == item["uid"]:
next_index = index + 1
if next_index <= len(self.playlist):
nextItem = self.playlist[next_index]
else:
nextItem = self.playlist[0]
return nextItem
def show(self):
return self.playlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment