3v324v23 commited on
Commit
8f3a4e5
1 Parent(s): 9555419

Refactor spotify api to include image downloads

Browse files
Files changed (4) hide show
  1. app.py +32 -22
  2. requirements.txt +2 -1
  3. test.ipynb +90 -0
  4. test.py +0 -17
app.py CHANGED
@@ -8,16 +8,28 @@ from pathlib import Path
8
  import gradio as gr
9
  from moviepy.editor import AudioFileClip
10
 
 
 
 
 
 
 
11
  output_dir = Path("temp/").absolute()
12
  output_dir.mkdir(exist_ok=True, parents=True)
13
 
14
-
15
  class SpotifyApi:
16
- spotify_directory = Path("spotify")
17
- final_directory = output_dir
18
 
19
- def __init__(self):
20
  self.setup_spotify()
 
 
 
 
 
 
 
 
21
 
22
  def setup_spotify(self) -> None:
23
  # Check if the credentials file exists
@@ -28,32 +40,30 @@ class SpotifyApi:
28
  )
29
  subprocess.call(["spodcast", "-l", "spotify.rc"])
30
 
31
- def download_episode(self, episode_url) -> str:
32
- # Generate a 8 character random string
33
- foldername = str(uuid.uuid4())[:8]
34
- out_path = (self.spotify_directory / foldername).resolve()
35
- subprocess.call(["spodcast", "--root-path", out_path, episode_url])
36
- self.foldername = foldername
37
  mp3_path = self.get_final_mp3()
38
  assert mp3_path is not None
39
  return mp3_path
40
 
 
 
 
 
 
 
 
 
 
 
41
  def get_final_mp3(self):
42
- # Look in all the subdirectories of spotify for the mp3 file
43
- for root, dirs, files in os.walk(
44
- Path(self.spotify_directory / self.foldername).resolve()
45
- ):
46
  for file in files:
47
  if file.endswith(".mp3"):
48
- final_mp3 = (
49
- Path(self.final_directory / self.foldername)
50
- .with_suffix(".mp3")
51
- .absolute()
52
- )
53
  shutil.copy(os.path.join(root, file), final_mp3)
54
- shutil.rmtree(
55
- Path(self.spotify_directory / self.foldername).absolute()
56
- )
57
  return final_mp3.as_posix()
58
 
59
 
 
8
  import gradio as gr
9
  from moviepy.editor import AudioFileClip
10
 
11
+
12
+ import opengraph
13
+ import pathlib
14
+
15
+ import requests
16
+
17
  output_dir = Path("temp/").absolute()
18
  output_dir.mkdir(exist_ok=True, parents=True)
19
 
 
20
  class SpotifyApi:
21
+ spotify_directory = Path(output_dir / "spotify")
 
22
 
23
+ def __init__(self, url):
24
  self.setup_spotify()
25
+ self.url = url
26
+ self.opengraph = opengraph.OpenGraph(url=url)
27
+ self.random_string = str(uuid.uuid4())[:8]
28
+ self.perma_output_path = Path(output_dir / self.random_string)
29
+ self.temp_output_path = Path(
30
+ self.spotify_directory / self.random_string
31
+ ).resolve()
32
+ self.folder_dir: Path = None
33
 
34
  def setup_spotify(self) -> None:
35
  # Check if the credentials file exists
 
40
  )
41
  subprocess.call(["spodcast", "-l", "spotify.rc"])
42
 
43
+ def download_episode(self) -> str:
44
+ out_path = self.temp_output_path.resolve()
45
+ subprocess.call(["spodcast", "--root-path", out_path, self.url])
 
 
 
46
  mp3_path = self.get_final_mp3()
47
  assert mp3_path is not None
48
  return mp3_path
49
 
50
+ def download_image(self):
51
+ image = self.opengraph["image"]
52
+ r = requests.get(image, allow_redirects=True)
53
+ path = self.perma_output_path.with_suffix(".jpg").absolute()
54
+ open(path, "wb").write(r.content)
55
+
56
+ def get_title(self) -> str:
57
+ return self.opengraph["title"]
58
+
59
+ # Move output file in the temp mp3 folder to the final output folder that we'll store the video in
60
  def get_final_mp3(self):
61
+ for root, dirs, files in os.walk(self.temp_output_path.resolve()):
 
 
 
62
  for file in files:
63
  if file.endswith(".mp3"):
64
+ final_mp3 = self.perma_output_path.with_suffix(".mp3").absolute()
 
 
 
 
65
  shutil.copy(os.path.join(root, file), final_mp3)
66
+ shutil.rmtree(self.temp_output_path.absolute())
 
 
67
  return final_mp3.as_posix()
68
 
69
 
requirements.txt CHANGED
@@ -18,4 +18,5 @@ spodcast
18
  moviepy
19
  huggingface-hub
20
  opencv-python
21
- image_tools
 
 
18
  moviepy
19
  huggingface-hub
20
  opencv-python
21
+ image_tools
22
+ podiant-opengraph
test.ipynb ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "9f0e1e4b-e36a-4dfe-adfd-6f99fb1d39f6",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import os\n",
11
+ "from app import animate_images, AudioInput, SpotifyApi"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 2,
17
+ "id": "778fa4aa-cc12-4c33-a866-47ddcedcaf5f",
18
+ "metadata": {},
19
+ "outputs": [
20
+ {
21
+ "name": "stderr",
22
+ "output_type": "stream",
23
+ "text": [
24
+ "WARNING:spodcast.podcast:Downloaded Hello Internet: Nine Ladies Dancing\n"
25
+ ]
26
+ }
27
+ ],
28
+ "source": [
29
+ "def test_spotify_downloads():\n",
30
+ " url = \"https://open.spotify.com/episode/3Ev4jlIjT8IVih1Oxidhqf?si=54a95e8fb07640c5\"\n",
31
+ " api = SpotifyApi(url=url)\n",
32
+ " api.download_episode()\n",
33
+ " api.download_image()\n",
34
+ " api.get_title()\n",
35
+ "\n",
36
+ "test_spotify_downloads()"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": 1,
42
+ "id": "de461d9b-bfbb-4d2d-b35e-fc3889494991",
43
+ "metadata": {},
44
+ "outputs": [],
45
+ "source": [
46
+ "def get_test_image_paths():\n",
47
+ " gallery_dir = \"temp/images\"\n",
48
+ " return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)][:2]\n",
49
+ "\n",
50
+ "get_test_image_paths()"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 2,
56
+ "id": "99df13fd-bd5d-4964-9300-016fcf7625e6",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "def get_test_audio_paths():\n",
61
+ " audio_path = \"temp/audio.mp3\"\n",
62
+ " audio_input = AudioInput(audio_path, 10, 10)\n",
63
+ " return audio_input\n",
64
+ "\n",
65
+ "get_test_audio_paths()"
66
+ ]
67
+ }
68
+ ],
69
+ "metadata": {
70
+ "kernelspec": {
71
+ "display_name": "Python 3 (ipykernel)",
72
+ "language": "python",
73
+ "name": "python3"
74
+ },
75
+ "language_info": {
76
+ "codemirror_mode": {
77
+ "name": "ipython",
78
+ "version": 3
79
+ },
80
+ "file_extension": ".py",
81
+ "mimetype": "text/x-python",
82
+ "name": "python",
83
+ "nbconvert_exporter": "python",
84
+ "pygments_lexer": "ipython3",
85
+ "version": "3.9.13"
86
+ }
87
+ },
88
+ "nbformat": 4,
89
+ "nbformat_minor": 5
90
+ }
test.py DELETED
@@ -1,17 +0,0 @@
1
- import os
2
- from app import animate_images, AudioInput
3
-
4
- def get_test_image_paths():
5
- gallery_dir = "temp/images"
6
- return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)][:2]
7
-
8
- def get_test_audio_paths():
9
- audio_path = "temp/audio.mp3"
10
- audio_input = AudioInput(audio_path, 10, 10)
11
- return audio_input
12
-
13
-
14
- image_paths = get_test_image_paths()
15
- audio_input = get_test_audio_paths()
16
-
17
- animate_images(image_paths, audio_input)