-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
215d5f5
commit 1ddbc8e
Showing
15 changed files
with
499 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
import pickle | ||
from jiosaavn.debugger import ic | ||
|
||
class Cache: | ||
def __init__(self, filepath: str) -> None: | ||
self.filepath = Path(filepath) | ||
self.cache_data = None | ||
|
||
def __str__(self) -> str: | ||
return f"Cache file for Jiosaavn downloaded from {self.filepath.name}\n\nData:\n{self.cache_data}" | ||
|
||
def __repr__(self) -> str: | ||
return f"Cache({self.filepath})" | ||
|
||
@property | ||
def data(self) -> list: | ||
if self.cache_data is None: | ||
if not self.filepath.is_file(): | ||
self.cache_data = self._write_to_pickle([]) | ||
else: | ||
with open(self.filepath, 'rb') as f: | ||
self.cache_data = pickle.load(f) | ||
return self.cache_data | ||
|
||
def write(self, data: list) -> list: | ||
"""Write the `data` to cache file | ||
Returns: | ||
list: `data` | ||
""" | ||
self.cache_data = self._write_to_pickle(data) | ||
return self.data | ||
|
||
def _write_to_pickle(self, data: list) -> list: | ||
with open(self.filepath, 'wb') as f: | ||
pickle.dump(data, f) | ||
ic('Cache Updated') | ||
return data | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"Placeholder module info" | ||
__version__ = "0.0.1" | ||
|
||
from template_python.debugger import * | ||
from jiosaavn.debugger import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from dataclasses import dataclass | ||
from jiosaavn.file_parser import Song | ||
from jiosaavn.debugger import ic | ||
from jiosaavn.request_package import Req | ||
|
||
class SaavnAPI: | ||
session = Req() | ||
def __init__(self, baseurl: str, port: int = 80): | ||
self.baseurl = baseurl | ||
self.port = port | ||
self.url = f"http://{baseurl}:{port}" | ||
|
||
def __str__(self) -> str: | ||
return f"SaavnAPI class for {self.url}" | ||
|
||
def __repr__(self) -> str: | ||
return f"SaavnAPI({self.baseurl}, {self.port})" | ||
|
||
def song(self, url: str) -> Song: | ||
data = self.session.get(f"{self.url}/song/?query={url}").json() | ||
return _song_from_json(data) | ||
|
||
def playlist(self, url: str) -> tuple[Song]: | ||
data = self.session.get(f"{self.url}/result/?query={url}").json() | ||
return (_song_from_json(song) for song in data.get('songs')) | ||
|
||
|
||
def _song_from_json(data: dict) -> Song: | ||
if type(data.get('artistMap')) == dict: | ||
artists = list(data.get('artistMap').keys()) | ||
else: | ||
artists = [] | ||
|
||
return Song( | ||
song_id = data['id'], | ||
name = data['song'], | ||
album = data['album'], | ||
media_url= data['media_url'], | ||
primary_artists= data['primary_artists'].split(', '), | ||
artists= artists, | ||
year = int(data.get('year', 0)), | ||
image_url= data.get('image') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.