Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
vidyasagar1432 committed Dec 11, 2021
2 parents 9e0b95b + e034b33 commit 1e0b007
Show file tree
Hide file tree
Showing 23 changed files with 2,933 additions and 1,013 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

__pycache__/
.venv/
dist
JioSaavn.egg-info
dist/
build/
jiosaavn.egg-info/
15 changes: 15 additions & 0 deletions JioSaavn/Async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .__asyncJioSaavn import JioSaavn
# from .__internal._async._fetch import *

from ._asyncFetch import searchSong,searchAlbum,song,album,playlist,lyrics


__all__ =[
'JioSaavn',
'searchSong',
'searchAlbum',
'song',
'album',
'playlist',
'lyrics'
]
14 changes: 14 additions & 0 deletions JioSaavn/Sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .__syncJioSaavn import JioSaavn

from ._syncFetch import searchSong,searchAlbum,song,album,playlist,lyrics


__all__ =[
'JioSaavn',
'searchSong',
'searchAlbum',
'song',
'album',
'playlist',
'lyrics'
]
137 changes: 137 additions & 0 deletions JioSaavn/__asyncJioSaavn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

from typing import Optional
from . import _asyncFetch

class JioSaavn:
'''
### Create an instance of JioSaavn with async methods.
'''
@staticmethod
async def searchSong(query:str,page:int=1,limit:int=10,response:str='json'):
'''Searches for song in JioSaavn.
Args:
query (str): Sets the search query.
page (int, optional): Sets page to the number of page. Defaults to 1.
limit (int, optional): Sets limit to the number of results. Defaults to 10. Max to 30.
response(str,optional): Sets the response of result. Defaults to `json`.
Note:
To get raw result Set `response` to `raw`
Example:
Calling `searchSong` method gives the search result.
>>> from jiosaavn.Async import JioSaavn
>>> search = await JioSaavn.searchSong('alone')
>>> print(search)
<https://github.com/vidyasagar1432/jiosaavn>
'''
return await _asyncFetch.searchSong(query=query,page=page,limit=limit,response=response)

@staticmethod
async def searchAlbum(query:str,response:str='json'):
'''Searches for album in JioSaavn.
Args:
query (str): Sets the search query.
response(str,optional): Sets the response of result. Defaults to `json`.
Note:
To get raw result Set `response` to `raw`
Examples:
Calling `searchAlbum` method gives the search result of albums.
>>> from jiosaavn.Async import JioSaavn
>>> search = await JioSaavn.searchAlbum('Alone')
>>> print(search)
<https://github.com/vidyasagar1432/jiosaavn>
'''
return await _asyncFetch.searchAlbum(query=query,response=response)

@staticmethod
async def song(url:Optional[str]=None,id:Optional[str]=None,lyrics:bool=False,response:str='json'):
'''Get song info from JioSaavn.
Args:
url (str): Sets the url of song.
id (str): Sets the id of song.
lyrics (bool): Sets the lyrics whether to get lyrics. Defaults to `False`.
response(str,optional): Sets the response of result. Defaults to `json`.
Note:
To get raw result Set `response` to `raw`
Examples:
Calling `song` method gives the song info.
>>> from jiosaavn.Async import JioSaavn
>>> result = await JioSaavn.song(id='veJXEDAz')
>>> print(result)
<https://github.com/vidyasagar1432/jiosaavn>
'''
return await _asyncFetch.song(url=url,id=id,lyrics=lyrics,response=response)

@staticmethod
async def album(url:Optional[str]=None,id:Optional[str]=None,lyrics:bool=False,response:str='json'):
'''Get album info from JioSaavn.
Args:
url (str): Sets the url of album.
id (str): Sets the id of album.
lyrics (bool): Sets the lyrics whether to get lyrics. Defaults to `False`.
response(str,optional): Sets the response of result. Defaults to `json`.
Note:
To get raw result Set `response` to `raw`
Examples:
Calling `album` method gives the album info.
>>> from jiosaavn.Async import JioSaavn
>>> result = await JioSaavn.album(id='10496527')
>>> print(result)
<https://github.com/vidyasagar1432/jiosaavn>
'''
return await _asyncFetch.album(url=url,id=id,lyrics=lyrics,response=response)

@staticmethod
async def playlist(url:Optional[str]=None,id:Optional[str]=None,lyrics:bool=False,response:str='json'):
'''Get playlist info from JioSaavn.
Args:
url (str): Sets the url of playlist.
id (str): Sets the id of playlist.
lyrics (bool): Sets the lyrics whether to get lyrics. Defaults to `False`.
response(str,optional): Sets the response of result. Defaults to `json`.
Note:
To get raw result Set `response` to `raw`
Examples:
Calling `playlist` method gives the playlist info.
>>> from jiosaavn.Async import JioSaavn
>>> result = await JioSaavn.playlist(url='https://www.jiosaavn.com/s/playlist/88063878238ad9a391a33c0e628d2b01/90s_Love/OykxHSA0YytFo9wdEAzFBA__')
>>> print(result)
<https://github.com/vidyasagar1432/jiosaavn>
'''
return await _asyncFetch.playlist(url=url,id=id,lyrics=lyrics,response=response)

@staticmethod
async def lyrics(url:Optional[str]=None,id:Optional[str]=None,response:str='json'):
'''Get lyrics of a song (If Available)
Args:
url (str): Sets the url of playlist.
id (str): Sets the id of song.
response(str,optional): Sets the response of result. Defaults to `json`.
Note:
To get raw result Set `response` to `raw`
Examples:
Calling `lyrics` method gives the lyrics of song.
>>> from jiosaavn.Async import JioSaavn
>>> result = await JioSaavn.lyrics(id='blMuXL1P')
>>> print(result)
<https://github.com/vidyasagar1432/jiosaavn>
'''
return await _asyncFetch.lyrics(url=url,id=id,response=response)

90 changes: 90 additions & 0 deletions JioSaavn/__asyncparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

from . import __helper
from . import _asyncFetch

async def makeSongResponse(song,lyrics:bool=False):
return {
'id': song['id'],
'songName':song['song'],
'albumName':song['album'],
'albumId': song['albumid'],
'playCount': song['play_count'],
'duration': song['duration'],
'label': song['label'],
'primaryArtists': song['primary_artists'],
'primaryArtistsId':song['primary_artists_id'],
'featuredArtists': song['featured_artists'],
'featuredArtistsId':song['featured_artists_id'],
# 'primaryArtists': [{'artist':artist,'id':id} for artist,id in zip((song['primary_artists']).split(', '),(song['primary_artists_id']).split(', '))],
# 'featuredArtists': [{'artist':artist,'id':id} for artist,id in zip((song['featured_artists']).split(', '),(song['featured_artists_id']).split(', '))],
'singers': song['singers'],
'starring':song['starring'],
'language': __helper.ucfirst(song['language']),
'copyright': song['copyright_text'],
'hasLyrics' : song['has_lyrics'],
'lyrics': await _asyncFetch.lyrics(song['id']) if song['has_lyrics'] == "true" and lyrics else None,
'releaseDate': song['release_date'],
'songUrl':song['perma_url'],
'albumUrl': song['album_url'],
'imagesUrls': __helper.makeDifferentQualityImages(song['image']),
'audioUrls':__helper.makeDifferentQualityMediaUrls(song['media_preview_url']),
'labelUrl':f"https://www.jiosaavn.com{song['label_url']}",
}


async def makeSearchResponse(data):
return [{
'id':i['id'],
'songName':i['title'],
'albumId':i['more_info']['album_id'],
'albumName':i['more_info']['album'],
'playCount': i['play_count'],
'imagesUrls':__helper.makeDifferentQualityImages(i['image']),
'primaryArtists':i['more_info']['artistMap']['primary_artists'],
'featuredArtists':i['more_info']['artistMap']['featured_artists'],
'artists':i['more_info']['artistMap']['artists'],
'description':i['header_desc'],
'hasLyrics':i['more_info']['has_lyrics'],
'language':__helper.ucfirst(i['language']),
'songUrl':i['perma_url'],
'albumUrl':i['more_info']['album_url'],
} for i in data['results']]



async def makeAlbumSearchResponse(data):
return [{
'id': i['id'],
'albumName': i['title'],
'music': i['music'],
'description': i['description'],
'year': i['more_info']['year'],
'language': __helper.ucfirst(i['more_info']['language']),
'songIds':i['more_info']['song_pids'],
'albumUrl': i['url'],
'imagesUrls':__helper.makeDifferentQualityImages(i['image']),
}for i in data['albums']['data']]

async def makeAlbumResponse(data,lyrics:bool=False):
return {
'albumId': data['albumid'],
'title': data['title'],
'name': data['name'],
'year': data['year'],
'primaryArtists': (data['primary_artists']).split(', '),
'image': data['image'],
'songs':[await makeSongResponse(song=song,lyrics=lyrics) for song in data['songs']],
'songUrl': data['perma_url'],
'releaseDate': data['release_date'],
}

async def makePlaylistResponse(data,lyrics:bool=False):
return {
'id': data['listid'],
'listname': data['listname'],
'username': data['username'],
'listCount': data['list_count'],
'playlistUrl': data['perma_url'],
'image': data['image'],
'songs':[await makeSongResponse(song=song,lyrics=lyrics) for song in data['songs']],
}
15 changes: 8 additions & 7 deletions JioSaavn/_requests.py → JioSaavn/__asyncrequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
import aiohttp
import json

from ._useragent import getHeaders
from .__useragent import getHeaders


async def getjSON(url:str):
async with aiohttp.ClientSession(headers=getHeaders()) as session:
async with session.get(url) as response:
if not response.status == 404:
return json.loads(await response.text())
return None
assert response.status == 200,'ClientResponse 404'
return json.loads(await response.text())


async def getText(url:str,data=None):
async with aiohttp.ClientSession(headers=getHeaders()) as session:
async with session.get(url,data=data) as response:
if not response.status == 404:
return await response.text()
return None
assert response.status == 200,'ClientResponse 404'
return await response.text()


File renamed without changes.
8 changes: 8 additions & 0 deletions JioSaavn/__constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# from enum import Enum

# class Response(Enum):
# json = 'json'
# raw = 'raw'

Response = ['json','raw']
4 changes: 3 additions & 1 deletion JioSaavn/_exceptions.py → JioSaavn/__exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

class Error(Exception):
"""Base class for exceptions in this module."""
pass
Expand All @@ -6,7 +7,8 @@ class Error(Exception):
class ValidationError(Error):
def __init__(self, message: str) -> None:
super().__init__(message)



class InvalidURL(Exception):
"""
URL is improperly formed or cannot be parsed.
Expand Down
15 changes: 7 additions & 8 deletions JioSaavn/_helper.py → JioSaavn/__helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

from ._requests import getText

def ucfirst(string:str):
return string.capitalize()

Expand All @@ -16,23 +14,24 @@ def makeDifferentQualityImages(image:str):
"150x150": f'{image}-150x150.jpg',
"500x500": f'{image}-500x500.jpg',}

async def getSongId(url:str):
response = await getText(url,data=[('bitrate', '320')])

def getSongId(response:str):
try:
return response.split('"song":{"type":"')[1].split('","image":')[0].split('"id":"')[-1]
except IndexError:
return(response.split('"pid":"'))[1].split('","')[0]

async def getAlbumId(url:str):
response = await getText(url)

def getAlbumId(response:str):
try:
return response.split('"album_id":"')[1].split('"')[0]
except IndexError:
return response.split('"page_id","')[1].split('","')[0]

async def getPlaylistId(url:str):
response = await getText(url)

def getPlaylistId(response:str):
try:
return response.split('"type":"playlist","id":"')[1].split('"')[0]
except IndexError:
return response.split('"page_id","')[1].split('","')[0]

11 changes: 8 additions & 3 deletions JioSaavn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from JioSaavn._fetch import searchSong,searchAlbum,song,lyrics,album,playlist
from . import Async, Sync
from .Sync import JioSaavn,searchSong,searchAlbum,song,album,playlist,lyrics

__title__ = "JioSaavn"
__description__ = "JioSaavn Api for Python 3."
__version__ = 'v0.0.5'
__title__ = "jiosaavn"
__description__ = "Search for JioSaavn songs & album. Get song ,album, lyric & playlist information from url or id."


__all__ =[
'JioSaavn',
'Async',
'Sync',
'searchSong',
'searchAlbum',
'song',
Expand Down
Loading

0 comments on commit 1e0b007

Please sign in to comment.