From c6de0539468e3f7dbf052dfaac4142703abe157f Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Sun, 18 Jun 2017 18:14:20 +0200
Subject: [PATCH] Should now download org vorbis files

---
 funkwhale_api/downloader/downloader.py            | 12 ++++++------
 funkwhale_api/downloader/tests/test_downloader.py |  7 +++++--
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/funkwhale_api/downloader/downloader.py b/funkwhale_api/downloader/downloader.py
index 840185c..854d0d0 100644
--- a/funkwhale_api/downloader/downloader.py
+++ b/funkwhale_api/downloader/downloader.py
@@ -3,26 +3,26 @@ import requests
 import json
 from urllib.parse import quote_plus
 import youtube_dl
+from django.conf import settings
+import glob
 
 
 def download(
         url,
-        target_directory,
-        name="%(id)s",
+        target_directory=settings.MEDIA_ROOT,
+        name="%(id)s.%(ext)s",
         extension='vorbis',
         bitrate=192):
-    target_path = os.path.join(target_directory, '{}.ogg'.format(name))
+    target_path = os.path.join(target_directory, name)
     ydl_opts = {
-        'format': 'bestaudio/best',
         'quiet': True,
         'outtmpl': target_path,
         'postprocessors': [{
             'key': 'FFmpegExtractAudio',
             'preferredcodec': extension,
-            'preferredquality': str(bitrate),
         }],
     }
     _downloader = youtube_dl.YoutubeDL(ydl_opts)
     info = _downloader.extract_info(url)
-    info['audio_file_path'] = target_path % {'id': info['id']}
+    info['audio_file_path'] = target_path.format(**info)
     return info
diff --git a/funkwhale_api/downloader/tests/test_downloader.py b/funkwhale_api/downloader/tests/test_downloader.py
index fd508f6..77164d5 100644
--- a/funkwhale_api/downloader/tests/test_downloader.py
+++ b/funkwhale_api/downloader/tests/test_downloader.py
@@ -3,8 +3,11 @@ from test_plus.test import TestCase
 from .. import downloader
 from funkwhale_api.utils.tests import TMPDirTestCaseMixin
 
+
 class TestDownloader(TMPDirTestCaseMixin, TestCase):
 
-    def test_can_download_audio_from_youtube_url(self):
+    def test_can_download_audio_from_youtube_url_to_vorbis(self):
         data = downloader.download('https://www.youtube.com/watch?v=tPEE9ZwTmy0', target_directory=self.download_dir)
-        self.assertTrue(os.path.exists(data['audio_file_path']))
+        self.assertTrue(
+            os.path.exists(os.path.join(self.download_dir, 'tPEE9ZwTmy0.ogg'))
+        )
-- 
GitLab