WankioM commited on
Commit
78a396d
1 Parent(s): 217e976

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -15,6 +15,56 @@ output_dir = Path("temp/").absolute()
15
  output_dir.mkdir(exist_ok=True, parents=True)
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  class AudioInput:
 
15
  output_dir.mkdir(exist_ok=True, parents=True)
16
 
17
 
18
+ class SpotifyApi:
19
+ spotify_directory = Path(output_dir / "spotify")
20
+
21
+ def __init__(self, url):
22
+ self.setup_spotify()
23
+ self.url = url
24
+ self.opengraph = opengraph.OpenGraph(url=url)
25
+ self.random_string = str(uuid.uuid4())[:8]
26
+ self.perma_output_path = Path(output_dir / self.random_string)
27
+ self.temp_output_path = Path(
28
+ self.spotify_directory / self.random_string
29
+ ).resolve()
30
+ self.folder_dir: Path = None
31
+
32
+ def setup_spotify(self) -> None:
33
+ # Check if the credentials file exists
34
+ if not os.path.exists("spotify.rc"):
35
+ with open("spotify.rc", "w") as f:
36
+ f.write(
37
+ f"{os.environ['SPOTIFY_USERNAME']} {os.environ['SPOTIFY_PASSWORD']}"
38
+ )
39
+ subprocess.call(["spodcast", "-l", "spotify.rc"])
40
+
41
+ def download_episode(self) -> str:
42
+ out_path = self.temp_output_path.resolve()
43
+ subprocess.call(["spodcast", "--root-path", out_path, self.url])
44
+ mp3_path = self.get_final_mp3()
45
+ assert mp3_path is not None
46
+ return mp3_path
47
+
48
+ def download_image(self):
49
+ image = self.opengraph["image"]
50
+ r = requests.get(image, allow_redirects=True)
51
+ path = self.perma_output_path.with_suffix(".jpg").absolute()
52
+ open(path, "wb").write(r.content)
53
+ return path
54
+
55
+ def get_title(self) -> str:
56
+ return self.opengraph["title"]
57
+
58
+ # Move output file in the temp mp3 folder to the final output folder that we'll store the video in
59
+ def get_final_mp3(self):
60
+ for root, dirs, files in os.walk(self.temp_output_path.resolve()):
61
+ for file in files:
62
+ if file.endswith(".mp3"):
63
+ final_mp3 = self.perma_output_path.with_suffix(".mp3").absolute()
64
+ shutil.copy(os.path.join(root, file), final_mp3)
65
+ shutil.rmtree(self.temp_output_path.absolute())
66
+ return final_mp3.as_posix()
67
+
68
 
69
 
70
  class AudioInput: