def search(self, query): # type: (str) -> SearchResult try: res = requests.post(self._search_url, data={"text": query}, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Nex1 Music server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Nex1 Music server. (HTTPError)") data = res.json() musics = [] albums = [] artists = [] for i in data: if 'type' in i and i['type'] == 'تک آهنگ': musics.append( Music(id=int(i["id"]), title=self._reformat(i['tracken']), artist=i['artisten'], url=None, image=i['image'], source=Nex1)) elif 'type' in i and i['type'] == 'آلبوم': albums.append( Album(id=i["id"], title=self._reformat(i['tracken']), artist=self._reformat(i['artisten']), url=None, image=i['image'], source=Nex1)) return SearchResult(musics, albums, artists)
def get_artist(self, artist_id): url = self._api_url + "GetArtistById?id={id}".format(id=artist_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Navahang server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Navahang server. (HTTPError)") musics = [] albums = [] data = res.json() for i in data['works']: if i['category'] == 'MP3': musics.append( Music(id=i["post_id"], title=self._reformat(i['song_name']), artist=i['artist_name'], image=i['image'], source=Navahang, download_url=i['high_quality'])) elif i['category'] == 'Album': albums.append( Music(id=i["albumId"], title=self._reformat(i['song']), artist=i['artist'], image=i['playerimage'], source=Navahang)) return SearchResult(musics=musics, albums=albums, artists=[ Artist(full_name=data['name'], id=artist_id, source=Navahang) ])
def get_download_url(self, music_id): url = self._download_url + '/music.xml?id={}'.format(music_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to bia2music server.") except HTTPError: raise SourceNetworkError( "Cannot connect to bia2music server. (HTTPError)") data = ElementTree.fromstring(res.content)[0] return data.get('Title'), data.get('Artist'), data.get('mp3')
def get_download_url(self, music_id): url = self._download_url + "mp3?id={id}".format(id=music_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to RadioJavan server.") except HTTPError: raise SourceNetworkError( "Cannot connect to RadioJavan server. (HTTPError)") data = res.json() return data.get('song'), data.get('artist'), data.get('link')
def get_download_url(self, music_id): url = self._download_url + "gettrackdetail?id={id}&lang=en".format( id=music_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to WikiSeda server.") except HTTPError: raise SourceNetworkError( "Cannot connect to WikiSeda server. (HTTPError)") data = res.json()['song'][0] return data.get('songname'), data.get('artist'), data.get('mp3')
def get_download_url(self, music_id): url = self._download_url try: res = requests.post(url, data={"post_id": music_id}, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Nex1 Music server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Nex1 Music server. (HTTPError)") data = res.json() return data.get('TrackEn'), data.get('ArtistEn'), data.get('Music320')
def get_download_url(self, music_id): url = self._api_url + "GetSingleMediaInfo?media_id={id}".format( id=music_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Navahang server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Navahang server. (HTTPError)") data = res.json()[0] return data.get('song_name'), data.get('artist_name'), data.get( 'download')
def search(self, query): # type: (str) -> SearchResult try: res = requests.get(self._search_url.format(query=quote(query)), timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Navahang server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Navahang server. (HTTPError)") data = res.json() musics = [] albums = [] artists = [] if 'MP3' in data: for i in data['MP3']: title, artist = i['title'].split(' – ') musics.append( Music(id=int(i["id"]), title=self._reformat(title), artist=artist, url=self._site_url + i['url'], image=i['image'], source=Navahang)) if 'Artist' in data: for i in data['Artist']: artists.append( Artist(id=int(i["id"]), full_name=i['title'], url=self._site_url + i['url'], image=i['image'], source=Navahang)) if 'Album' in data: for i in data['Album']: try: album, artist = i['title'].split(' – ') except: album, artist = i['title'], None albums.append( Album(id=int(i["id"]), title=self._reformat(album), artist=self._reformat(artist), url=self._site_url + i['url'], image=i['image'], source=Navahang)) return SearchResult(musics, albums, artists)
def get_download_url(self, music_id): url = self._download_url.format(id=music_id) try: res = requests.post(url, headers=self._headers, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to RapFarsi server.") except HTTPError: raise SourceNetworkError( "Cannot connect to RapFarsi server. (HTTPError)") data = res.json()['Post'] return data.get('title'), data.get( 'artistTitle'), data['track320']['link']
def search(self, query): # type: (str) -> SearchResult try: res = requests.post(self._search_url, data={ "engine_key": self._engine_key, "q": query, "per_page": 50 }, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to bia2music server.") except HTTPError: raise SourceNetworkError( "Cannot connect to bia2music server. (HTTPError)") if res.status_code == 404: raise SearchResult(None, None, None) data = res.json()['records']['page'] musics = [] albums = [] artists = [] for i in data: if i['type'] == 'music': musics.append( Music(id=i["music_id"], title=self._reformat(i['title']), artist=i['artist'], url=i['url'], image=i['image'], source=Bia2)) elif i['type'] == 'artist': artists.append( Artist(id=i["artist_id"], full_name=i['title'], url=i['url'], image=i['image'], source=Bia2)) elif i['type'] == '': title = self._split_album_title(i['title']) albums.append( Album(id=i["url"].split('/')[-1], title=self._reformat(title[0]), artist=self._reformat(title[1]), url=i['url'], image=i['image'], source=Bia2)) return SearchResult(musics, albums, artists)
def get_related_tracks(self, track_id, album_id=0): try: res = requests.post(self._api_url + "related_tracks.php", data={ "track_id": track_id, "album_id": album_id, "is_iran": 1 }, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to MrTehran server.") except HTTPError: raise SourceNetworkError( "Cannot connect to MrTehran server. (HTTPError)") data = res.json() return data
def get_artist(self, artist_id): try: res = requests.post(self._api_url + "artist_data.php", data={ "user_id": 217604, "artist_id": artist_id, "is_iran": 1 }, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to MrTehran server.") except HTTPError: raise SourceNetworkError( "Cannot connect to MrTehran server. (HTTPError)") musics = [] albums = [] data = res.json() for i in data['all_tracks']: musics.append( Music(id=int(i["track_id"]), title=self._reformat(i["track_title"]), artist=i["track_artist"], url=self._download_url + i["track_audio"], image=self._download_url + i["track_thumbnail"], source=MrTehran)) for i in data['all_albums']: image = None if not i["track_artwork"] is None: image = self._download_url + i["track_artwork"] albums.append( Album(id=int(i["album_id"]), title=self._reformat(i["album_title"]), artist=i["album_artist"], url=(self._site_url + "mp3/" + i["track_id"] + "/" + i["track_artist"] + "/" + i["track_title"]).lower().replace(" ", "-"), image=image, source=MrTehran)) return SearchResult( musics=musics, albums=albums, artists=[ Artist(full_name=data.get('artist_info').get('artist_name'), id=data.get('artist_info').get('artist_id'), source=MrTehran) ])
def search(self, query): # type: (str) -> SearchResult try: res = requests.get(self._search_url.format(query=quote(query)), timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to RadioJavan server.") except HTTPError: raise SourceNetworkError( "Cannot connect to RadioJavan server. (HTTPError)") data = res.json() musics = [] albums = [] artists = [] if 'mp3s' in data: for i in data['mp3s']: musics.append( Music(id=int(i["id"]), title=i['song'], artist=i['artist'], url=i['share_link'], image=i['photo'], source=RadioJavan, download_url=i['link'])) if 'artists' in data: for i in data['artists']: artists.append( Artist(id=quote(i['name']), full_name=i['name'], image=i['photo'], source=RadioJavan)) if 'albums' in data: for i in data['albums']: albums.append( Album(id=int(i["id"]), title=self._reformat(i['album_album']), artist=self._reformat(i['album_artist']), url=i['share_link'], image=i['photo'], source=RadioJavan)) return SearchResult(musics, albums, artists)
def get_artist(self, artist_id): # type: (str) -> SearchResult try: res = requests.get(self._download_url + "artist?query={id}".format(id=artist_id), timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Next1 server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Next1 server. (HTTPError)") musics = [] albums = [] data = res.json() if 'mp3s' in data: for i in data['mp3s']: musics.append( Music(id=int(i["id"]), title=i['song'], artist=i['artist'], url=i['share_link'], image=i['photo'], source=RadioJavan, download_url=i['link'])) if 'albums' in data: for i in data['albums']: albums.append( Album(id=int(i["album_id"]), title=self._reformat(i['album_album']), artist=self._reformat(i['album_artist']), url=i['album']['share_link'], image=i['photo'], source=RadioJavan)) return SearchResult(musics=musics, albums=albums, artists=[ Artist(full_name=data['query'], id=artist_id, source=RadioJavan) ])
def get_album(self, album_id): # type: (int) -> SearchResult try: res = requests.get(self._download_url + "mp3?id={id}".format(id=album_id), timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Next1 server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Next1 server. (HTTPError)") musics = [] data = res.json() albums = [] artist = [ Artist(full_name=data['artist'], id=quote(data['artist']).lower()) ] if 'album_tracks' in data: albums.append( Album(artist=data['album_artist'], title=data['album_album'])) for i in data['album_tracks']: if i['type'] == 'mp3': musics.append( Music(id=i['id'], title=self._reformat(i['song']), artist=i['artist'], url=i['share_link'], image=i["photo"], source=RadioJavan, download_link=i['link'])) else: if data['type'] == 'mp3': musics.append( Music(id=data['id'], title=self._reformat(data['song']), artist=data['artist'], url=data['share_link'], image=data["photo"], source=RadioJavan, download_link=data['link'])) return SearchResult(musics=musics, albums=albums, artists=artist)
def search(self, query): # type: (str) -> SearchResult try: res = requests.post(self._search_url.format(query=quote(query)), timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to WikiSeda server.") except HTTPError: raise SourceNetworkError( "Cannot connect to WikiSeda server. (HTTPError)") data = res.json() musics = [] albums = [] artists = [] for i in data: if i['type'] == 'song': musics.append( Music(id=int(i["id"]), title=self._reformat(i['songname']), artist=i['artist'], url=i['url'], image=i['poster'], source=WikiSeda, download_url=i['mp3'])) elif i['type'] == 'artist': artists.append( Artist(id=int(i["id"]), full_name=i['artist'], url=i['url'], image=i['poster'], source=WikiSeda)) elif i['type'] == 'album': albums.append( Album(id=i["id"], title=self._reformat(i['album']), artist=self._reformat(i['artist']), url=i['url'], image=i['poster'], source=WikiSeda)) return SearchResult(musics, albums, artists)
def get_album(self, album_id): # type: (int) -> SearchResult url = self._download_url + '/album.xml?album={}'.format(album_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to bia2music server.") except HTTPError: raise SourceNetworkError( "Cannot connect to bia2music server. (HTTPError)") data = ElementTree.fromstring(res.content)[0] musics = [] for i in data: musics.append( Music(id=int(i.get("id")), title=self._reformat(i.get("title")), artist=data.get('artist'), url=data.get('share_url'), image=data.get('cover'), source=Bia2)) return SearchResult(musics=musics)
def get_artist(self, artist_id): # type: (int) -> SearchResult url = self._download_url + '/artist.xml?id={}'.format(artist_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to bia2music server.") except HTTPError: raise SourceNetworkError( "Cannot connect to bia2music server. (HTTPError)") data = ElementTree.fromstring(res.content)[0] musics = [] albums = [] for i in range(len(data)): if data[i].tag == 'albums': for j in data[i]: albums.append( Album(id=int(j.get('id')), title=self._reformat(j.get('title')), artist=self._reformat(data.get("name")), image=j.get('cover'), source=Bia2)) if data[i].tag == 'singles': for j in data[i]: musics.append( Music(id=int(j.get('id')), title=self._reformat(j.get('title')), artist=data.get("name"), url=data.get('share_url'), image=j.get('cover'), source=Bia2)) return SearchResult(musics=musics, albums=albums, artists=[ Artist(full_name=data.get("name"), id=artist_id, image=data.get("cover"), source=Bia2) ])
def get_album(self, album_id): # type: (str) -> SearchResult url = self._api_url + "GetPlaylistSongs?playlistId={id}".format( id=album_id) try: res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Navahang server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Navahang server. (HTTPError)") musics = [] data = res.json()['SONG_LIST'] for i in data: musics.append( Music(id=i["post_id"], title=self._reformat(i['song_name']), artist=i['artist_name'], url=res.json()['SHARE_LINK']['link'], image=i['image_Mp3'], source=Navahang)) return SearchResult(musics=musics)
def get_artist(self, artist_id): # type: (int) -> SearchResult try: url = self._download_url + "getnewcases?signer_id={id}&lang=en".format( id=artist_id) res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Next1 server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Next1 server. (HTTPError)") musics = [] albums = [] data = res.json() for i in data['items']: if i['type'] == 'song': musics.append( Music(id=int(i["id"]), title=self._reformat(i['songname']), artist=i['artist'], url=i['url'], image=i['poster'], source=WikiSeda)) elif i['type'] == 'album': albums.append( Album(id=i["id"], title=self._reformat(i['album']), artist=self._reformat(i['artist']), url=i['url'], image=i['poster'], source=WikiSeda)) return SearchResult(musics=musics, albums=albums, artists=[ Artist(full_name=data['artist'][0]['artist'], image=data['artist'][0]['poster'], id=int(data['artist'][0]['id'])) ])
def get_album(self, album_id): # type: (str) -> SearchResult try: res = requests.post(self._download_url, data={"post_id": album_id}, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Next1 server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Next1 server. (HTTPError)") musics = [] data = json.loads(res.json()['AlbumTrackList']) for i in data: musics.append( Music(id=None, title=self._reformat(i['album_track_name_en']), artist=res.json()["ArtistEn"], url=res.json()['PostUrl'], image=res.json()["Image"], source=Nex1, download_url=i['album_track_link_320'])) return SearchResult(musics=musics)
def get_album(self, album_id): # type: (str) -> SearchResult try: url = self._download_url + "getalbumdetail?id={id}&lang=en".format( id=album_id) res = requests.get(url, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Next1 server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Next1 server. (HTTPError)") musics = [] data = res.json()['song'] for i in data['albumtracks']: musics.append( Music(id=int(i['id']), title=self._reformat(i['songname']), artist=i['artist'], url=i['url'], image=i["poster"], source=WikiSeda, download_link=i['mp3'])) return SearchResult(musics=musics)
def search(self, query): # type: (str) -> SearchResult try: res = requests.post(self._search_url.format(query=quote(query)), headers=self._headers, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to RapFarsi server.") except HTTPError: raise SourceNetworkError( "Cannot connect to RapFarsi server. (HTTPError)") data = res.json() musics = [] albums = [] artists = [] for i in data: if i['type'] == 'single': musics.append( Music(id=i["slug"], title=self._reformat(i['title']), artist=i['artistTitle'], url=self._site_url + i['link'], image=i['cover']['full'], source=RapFarsi, download_link=i['track320']['link'] if i['track320'] else i['track128']['link'] if 'track128' in i and i['track128'] else None)) elif i['type'] == 'album': albums.append( Album(id=i["slug"], title=self._reformat(i['title']), artist=self._reformat(i['artistTitle']), url=self._site_url + i['link'], image=i['cover']['full'], source=RapFarsi)) return SearchResult(musics, albums, artists)
def get(self): if os.path.isfile(os.path.join(self.path, self.file_name)): self.path = os.path.join(self.path, self.file_name) return pathlib.Path(self.path).mkdir(parents=True, exist_ok=True) with open(os.path.join(self.path, self.file_name), "wb") as f: start = time.time() try: req = requests.get(self.url, stream=True) except ConnectionError: raise SourceNetworkError("Cannot download this music.") except HTTPError: raise SourceNetworkError( "Cannot download this music. (HTTPError)") total_length = req.headers.get("content-length") if total_length is None: f.write(req.content) else: dl = 0 total_length = int(total_length) print("Downloading: %s" % self.file_name.split(".")[0]) for data in req.iter_content(chunk_size=4096): dl += len(data) f.write(data) done = int(100 * dl / total_length) percent = " " + str(done) if done < 10 else done sys.stdout.write( "\r%s%%|%s%s| (%s / %s) %s/s " % (percent, "█" * int(done // 3), " " * (int( (33 - done // 3))), convert_size(dl), convert_size(total_length), convert_size(int( dl // (time.time() - start))).replace(" ", ""))) sys.stdout.flush() print() self.path = os.path.join(self.path, self.file_name)
def get_album(self, album_id): # type: (str) -> SearchResult try: res = requests.post(self._download_url.format(id=album_id), timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to Next1 server.") except HTTPError: raise SourceNetworkError( "Cannot connect to Next1 server. (HTTPError)") musics = [] data = res.json()['Post'] for i in data['albumList']: artist, title = data['artistTitle'], i['title'] musics.append( Music(id=i['id'], title=self._reformat(title), artist=artist, url=self._site_url + data['link'], image=i["cover"]['full'], source=RapFarsi, download_link=i['track320']['link'] if i['track320'] else i['track128']['link'])) return SearchResult(musics=musics)
def search(self, query): # type: (str) -> SearchResult try: res_tracks = requests.post(self._api_url + "main_search_tracks.php", data={ "search_text": query, "page": 0, "is_iran": 1 }, timeout=MAX_TIME_OUT) res_albums = requests.post(self._api_url + "main_search_albums.php", data={ "search_text": query, "page": 0, "is_iran": 1 }, timeout=MAX_TIME_OUT) res_artists = requests.post(self._api_url + "main_search_artists.php", data={ "search_text": query, "page": 0 }, timeout=MAX_TIME_OUT) except ConnectionError: raise SourceNetworkError("Cannot connect to MrTehran server.") except HTTPError: raise SourceNetworkError( "Cannot connect to MrTehran server. (HTTPError)") data_tracks = res_tracks.json() data_albums = res_albums.json() data_artists = res_artists.json() musics = [] albums = [] artists = [] for i in data_tracks: musics.append( Music(id=int(i["track_id"]), title=self._reformat(i["track_title"]), artist=i["track_artist"], url=self._download_url + i["track_audio"], image=self._download_url + i["track_thumbnail"], source=MrTehran)) for i in data_artists: image = None if not i["artist_thumbnail"] is None: image = self._download_url + i["artist_thumbnail"] artists.append( Artist(id=int(i["artist_id"]), full_name=i["artist_name"], url=self._site_url + "artist/" + i["artist_id"], image=image, source=MrTehran)) for i in data_albums: image = None if not i["track_artwork"] is None: image = self._download_url + i["track_artwork"] albums.append( Album(id=int(i["album_id"]), title=self._reformat(i["album_title"]), artist=self._reformat(i["album_artist"]), url=(self._site_url + "mp3/" + i["track_id"] + "/" + i["track_artist"] + "/" + i["track_title"]).lower().replace(" ", "-"), image=image, source=MrTehran)) return SearchResult(musics, albums, artists)