"""Shazam Playlist to Youtube Playlist""" | |
import pandas as pd | |
from pytube import Search, YouTube | |
from flask import Flask, request, send_from_directory | |
app = Flask(__name__) | |
def index(): | |
try: | |
return send_from_directory('.', 'index.html') | |
except Exception as e: | |
return str(e) | |
def video_id() -> str: | |
try: | |
title: str = request.json.get('title') | |
artist: str = request.json.get('artist') | |
youtube: YouTube = _get_youtube_song(title, artist) | |
return youtube.video_id | |
except Exception as e: | |
return str(e) | |
def parse_csv(): | |
try: | |
file = request.files['file'] | |
# Process the uploaded file | |
shazamlibrary_df = pd.read_csv(file, header=1) | |
shazamlibrary_df = shazamlibrary_df.drop_duplicates(subset=['TrackKey'])[['Title', 'Artist']] | |
shazamlibrary_df.insert(0, '#', shazamlibrary_df.index + 1) | |
return shazamlibrary_df.to_html(index=False, justify="left") | |
except Exception as e: | |
return str(e) | |
# Adding underscore to ignore, otherwise, docker fails with error: | |
# TypeError: unsupported operand type(s) for |: 'type' and 'NoneType' | |
def _get_youtube_song(title: str, artist: str) -> YouTube | None: | |
search_result = Search(f'{title} by {artist}') | |
return search_result.results[0] if search_result.results else None |