Commit e499b9c9 authored by Eliot Berriot's avatar Eliot Berriot

Now embed audio tags in files

parent 059d4491
......@@ -15,7 +15,7 @@ Created libraries are private by default, but you can change their visibility as
### Preparation
```sh
pip3 install --user click aiohttp python-dotenv feedparser
pip3 install --user click aiohttp python-dotenv feedparser mutagen
echo "FUNKWHALE_SERVER_URL=https://demo.funkwhale.audio" >> .env
echo "FUNKWHALE_USERNAME=demo" >> .env
......
#!/usr/bin/env python3
import aiohttp
import asyncio
import io
import os
import re
import argparse
import mutagen.mp3
import mutagen.id3
import feedparser
import tempfile
......@@ -87,6 +91,7 @@ async def _main(urls, server, username, password):
download_session=download_session,
upload_session=funkwhale_session,
url=entry_url,
parsed_feed=feeds_by_url[url]["parsed_feed"],
parsed_entry=parsed_entry,
library=feeds_by_url[url]["library"],
download_semaphore=download_semaphore,
......@@ -155,35 +160,71 @@ async def login(session, server, username, password):
return json_body["token"]
def get_track_position(entry):
try:
return mutagen.id3.TRCK(text=[entry['itunes_episode']])
except KeyError:
pass
regex = r'.*#(\d+).*$'
match = re.match(regex, entry['title'])
if match:
return mutagen.id3.TRCK(text=[str(match.groups()[0])])
return mutagen.id3.TRCK(text=["1"])
async def mirror_entry(
server,
download_session,
upload_session,
url,
parsed_feed,
parsed_entry,
library,
download_semaphore,
upload_semaphore,
):
async with download_semaphore:
download_link = [l['href'] for l in parsed_entry['links'] if l['type'].startswith('audio')][0]
download_link = [l['href'] for l in parsed_entry['links'] if l['type'].startswith('audio/mpeg')][0]
print(' Downloading {}…'.format(download_link))
async with download_session.get(download_link) as download_r:
download_r.raise_for_status()
data = {
"library": library['uuid'],
"import_reference": 'sync-podcast',
"source": url,
"audio_file": await download_r.read(),
}
audio_data = io.BytesIO(await download_r.read())
audio_file = mutagen.mp3.MP3(audio_data)
if isinstance(audio_file, mutagen.mp3.MP3):
print(' Fixing metadata on {}…'.format(download_link))
if audio_file.tags is None:
audio_file.add_tags()
mapping = {
'TIT2': lambda e, f: mutagen.id3.TIT2(text=[e['title']]),
'TPE1': lambda e, f: mutagen.id3.TPE1(text=[f['feed']['author']]),
'TALB': lambda e, f: mutagen.id3.TALB(text=[f['feed']['title']]),
'TRCK': lambda e, f: get_track_position(e),
'TPOS': lambda e, f: mutagen.id3.TPOS(text=[e.get('itunes_season', 1)]),
}
for tag, handler in mapping.items():
existing = audio_file.tags.getall(tag)
if existing:
continue
value = handler(parsed_entry, parsed_feed)
audio_file.tags.setall(tag, [value])
audio_data.seek(0)
audio_file.save(audio_data)
audio_data.seek(0)
data = {
"library": library['uuid'],
"import_reference": 'sync-podcast',
"source": url,
"audio_file": audio_data,
}
print(' Uploading {}…'.format(download_link))
async with upload_semaphore:
async with upload_session.post(server + '/api/v1/uploads/', data=data) as upload_r:
try:
upload_r.raise_for_status()
except:
if upload_r.status != 201:
result = await upload_r.json()
print(' Failed to upload {}: {}'.format(url, result))
print(' Failed to upload {}: {}'.format(download_link, result))
return
result = await upload_r.json()
print(' Uploaded {}: ID is {}!'.format(download_link, result['uuid']))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment