File size: 1,443 Bytes
3636d4e
 
 
 
5b99258
a5c2bee
 
 
 
3636d4e
c8ea80c
3636d4e
c8ea80c
 
3636d4e
c8ea80c
3636d4e
 
 
 
e2e5e1d
3636d4e
 
 
 
 
 
c8ea80c
3636d4e
 
 
 
 
 
c8ea80c
5b99258
e2e5e1d
 
 
5b99258
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""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__)

@app.route('/')
def index():
    try:
        return send_from_directory('.', 'index.html')
    except Exception as e:
        return str(e)
    
@app.route('/video_id', methods=['POST'])
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)

@app.route('/parse_csv', methods=['POST'])
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