-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
124 lines (98 loc) · 3.87 KB
/
app.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.responses import RedirectResponse
from file_downloader import TaskManager
from saavn_nn import AlbumDetails
from saavn_nn import PlaylistDetails
from saavn_nn import SongDetails
from saavn_nn import check_media_url
from saavn_nn import get_album_details
from saavn_nn import get_album_details_from_url
from saavn_nn import get_lyrics
from saavn_nn import get_playlist_details
from saavn_nn import get_playlist_details_from_url
from saavn_nn import get_song_details
from saavn_nn import get_song_details_from_url
from saavn_nn import search_query
app = FastAPI()
task_manager = TaskManager()
@app.get('/')
def home():
return RedirectResponse(url = '/docs')
@app.get('/search')
def search_jiosaavn(query: str, lyrics: Optional[bool] = False) -> Union[
Dict[str, Union[str, Union[SongDetails, AlbumDetails, PlaylistDetails, List[
SongDetails]]]], JSONResponse]:
"""
Submit your query here.
Valid query options are - song/album/playlist link of from https://www.jiosaavn.com/
or text query
"""
if 'saavn' in query:
token = query.split('/')[-1]
if '/song' in query:
return {"type": "song", "result": get_song_details_from_url(token)}
elif '/album' in query:
return {
"type": "album",
"result": get_album_details_from_url(token)
}
elif '/playlist' in query or '/featured' in query:
return {
"type": "playlist",
"result": get_playlist_details_from_url(token, 1)
}
else:
return JSONResponse(status_code = 404, content = {
"error": "Unknown URL",
"error code": 1001,
"url": query,
"message": "Please send links from https://www.jiosaavn.com only"
})
return {"type": "query", "result": search_query(query)}
@app.get('/song/{song_id}')
def song_details(song_id: str) -> SongDetails:
"""Get song details using song id,
which may be obtained from previous query results"""
return get_song_details(song_id)
@app.get('/lyrics/{song_id}')
def get_song_lyrics_(song_id: str):
"""Get song lyrics"""
return get_lyrics(song_id)
@app.get('/album/{album_id}')
def album_details(album_id: str) -> AlbumDetails:
"""Get album details including details of all songs in the album by using
album id, which may be obtained from previous query results"""
return get_album_details(album_id)
@app.get('/playlist/{playlist_id}')
def playlist_details(playlist_id: str,
page_no: Optional[int] = 1) -> PlaylistDetails:
"""Get playlist details with top 5 songs in the playlist by using
album id, which may be obtained from previous query results"""
return get_playlist_details(playlist_id, page_no)
@app.get('/media_url/{song_id}')
def download_link(song_id: str) -> str:
"""Get verified download link of the song using song_id"""
return check_media_url(get_song_details(song_id).media_url)
@app.get('/download_song/{song_id}')
def download_song(song_id: str):
song_details = get_song_details(song_id)
media_url = song_details.media_url
return {"task_id": task_manager.add_task(media_url)}
@app.get('/get_task_status/{task_id}')
def get_task_status(task_id: int):
print("getting task status", task_id)
return {"task_finished": task_manager.get_task_status(task_id)}
@app.get('/del_task/{task_id}')
def del_task(task_id: int):
print("deleting task status", task_id)
task_manager.del_task(task_id)
return {"task_finished": True}
if __name__ == '__main__':
from os import environ
from uvicorn import run
run("app:app", host = "0.0.0.0", port = int(environ.get("PORT", 5000)))