def upload(self, chat_id, order_list, tmp_dir, cover_path, artist, album, cover_url): try: #save album in db a = Albums(name=album, cover=cover_url, artist=artist) db.session.add(a) db.session.commit() album_id = a.id album_messages = [] #upload cover to storage group cover = open(cover_path, 'rb') bot.send_photo(STORAGE_GROUP_ID, cover, caption='{} - {}'.format(artist, album), disable_notification=True) for info in order_list: file = info[1] name = info[2] try: audio = open(file, 'rb') message = bot.send_audio(STORAGE_GROUP_ID, audio, performer=artist, title=name, disable_notification=True) album_messages.append([name, message.message_id]) except Exception as e: bot.send_message( chat_id, '⚠️ Error, skipping track - {}. Reason:\n{}'.format( name, e)) os.remove(file) #upload cover to chat cover = open(cover_path, 'rb') bot.send_photo(chat_id, cover, caption='{} - {}'.format(artist, album), disable_notification=True) os.remove(cover_path) os.rmdir('{}/{}'.format(UPLOAD_DIR, tmp_dir)) #forward from storage group order_id = 0 for album_message in album_messages: name = album_message[0] message_id = album_message[1] bot.forward_message(chat_id, STORAGE_GROUP_ID, message_id) #save in db s = Songs(id=message_id, name=name, album_id=album_id, order_id=order_id) db.session.add(s) order_id += 1 db.session.commit() except Exception as e: bot.send_message( chat_id, '⚠️ Oooops, an error occurred during album upload, we are on it' ) log.error(e)
def handle_search(message): try: arg = message.text.replace('/search ', '') results = search(arg) key = types.InlineKeyboardMarkup() for result in results: if len(result['url']) <= 64: callback = result['url'] else: search_key = 'user:{}:search'.format(message.chat.id) if result['type'] == 'album': r.hset(search_key, 'album', result['url']) r.expire(search_key, 120) callback = 'album' elif result['type'] == 'band': r.hset(search_key, 'album', result['url']) r.expire(search_key, 120) callback = 'band' if result['type'] == 'album': key.add(types.InlineKeyboardButton('{} - {} ({})'.format(result['band'], result['album'], result['type']), callback_data=callback)) if result['type'] == 'band': key.add(types.InlineKeyboardButton('{} ({})'.format(result['band'], result['type']), callback_data=callback)) text = 'Select resource:' bot.send_message(message.chat.id, text, reply_markup=key) except Exception as e: bot.send_message(message.chat.id, e)
def badcamp_search(chat_id, query): results = search(query) key = types.InlineKeyboardMarkup() if len(results) != 0: for result in results: if len(result['url']) <= 64: callback = result['url'] else: search_key = 'user:{}:search'.format(chat_id) if result['type'] == 'album': callback = 'album:{}'.format(random_string()) r.hset(search_key, callback, result['url']) r.expire(search_key, 240) elif result['type'] == 'band': callback = 'band:{}'.format(random_string()) r.hset(search_key, callback, result['url']) r.expire(search_key, 240) if result['type'] == 'album': key.add( types.InlineKeyboardButton('{} - {} ({})'.format( result['band'], result['album'], result['type']), callback_data=callback)) if result['type'] == 'band': key.add( types.InlineKeyboardButton('{} ({})'.format( result['band'], result['type']), callback_data=callback)) text = 'ЁЯС╛ Select resource:' bot.send_message(chat_id, text, reply_markup=key) else: bot.send_message(chat_id, 'тЪая╕П Nothing found')
def get_songs(self, id, chat_id): results = self.spotify.album(id) artist = results["artists"][0]["name"] album = results["name"] cover = results["images"][0]["url"] songs = results["tracks"]["items"] if in_db(artist, album, chat_id) == False: id = 0 songs_list = [] for song in songs: query = artist + song["name"] try: try: url = self.search_youtube_parse(query) except: url = self.search_youtube_api(query) songs_list.append({id: [song["name"], url]}) id += 1 except: bot.send_message( chat_id, 'Skipping track: {} \nReason: not found'.format( song["name"])) return { 'artist': artist, 'album': album, 'cover': cover, "songs": songs_list }
def handle_start(message): try: if 'album' in message.text or 'track' in message.text: call_get_songs(message.text, message.chat.id) elif re.match(r'https:\/\/*.*\.bandcamp\.com\/', message.text): call_get_albums(message.text[:-1], message.chat.id) except Exception as e: bot.send_message(message.chat.id, e)
def callback_inline(call): try: if call.message: if 'album' in call.data: call_get_songs(call.data, call.message.chat.id) else: call_get_albums(call.data, call.message.chat.id) except Exception as e: bot.send_message(call.message.chat.id, e)
def handle_start(message): try: if 'album' in message.text or 'track' in message.text: call_get_songs(message.text, message.chat.id) elif re.match(r'https:\/\/*.*\.bandcamp\.com\/', message.text): call_get_albums(message.text[:-1], message.chat.id) except Exception as e: bot.send_message(message.chat.id, '⚠️ Oooops, an error occurred, we are on it') log.error(e)
def call_get_spoti_albums(message, chat_id): url = message spotisearch = SpootySearch() albums = spotisearch.get_albums(url) key = types.InlineKeyboardMarkup() if len(albums) != 0: for album in albums: callback = 'spotify_al:' + album['url'] key.add( types.InlineKeyboardButton(album['name'], callback_data=callback)) text = 'ЁЯС╛ Select album:' bot.send_message(chat_id, text, reply_markup=key) else: bot.send_message(chat_id, 'тЪая╕П Nothing found')
def spoti_search(chat_id, query): spootisearch = SpootySearch() results = spootisearch.get_artists(query) if len(results) != 0: key = types.InlineKeyboardMarkup() for result in results: callback = 'spotify_artist:' + result['url'] key.add( types.InlineKeyboardButton('{} ({})'.format( result['band'], result['type']), callback_data=callback)) text = 'ЁЯС╛ Select resource:' bot.send_message(chat_id, text, reply_markup=key) else: bot.send_message(chat_id, 'тЪая╕П Nothing found')
def blame_song(message): try: if message.text == '/blame': bot.send_message(message.chat.id, '⚙️ Use /blame command to report for a wrong track downloaded from youtube. Please follow this syntax:\n\n\ /blame Artist\nAlbum\nSong\nURL from youtube', parse_mode='markdown') else: args = message.text.replace('/blame ', '') args = args.split('\n') artist = args[0] album = args[1] song = args[2] url = args[3] blame(message.chat.id, artist, album, song, url) except Exception as e: bot.send_message(message.chat.id, '⚠️ Oooops, an error occurred, we are on it') log.error(e)
def call_get_albums(message, chat_id): if message == 'band': url = r.hget('user:{}:search', 'band') else: url = message albums = get_albums(url) key = types.InlineKeyboardMarkup() for album in albums: if len(album['url']) <= 64: callback = album['url'] else: search_key = 'user:{}:search'.format(chat_id) r.hset(search_key, 'album', album['url']) r.expire(search_key, 120) callback = 'album' key.add( types.InlineKeyboardButton(album['name'], callback_data=callback)) text = 'Select album:' bot.send_message(chat_id, text, reply_markup=key)
def blame(self, chat_id, album_id, song_id, url, name, artist): try: tmp_dir = random_string() tmp_song = random_string() os.mkdir('{}/{}'.format(UPLOAD_DIR, tmp_dir)) if 'youtube' in url: order_element = download_youtube(0, tmp_dir, tmp_song, url, name) else: order_element = download_badcamp(0, tmp_dir, tmp_song, url, name) song_path = order_element[1] self.uploader.upload_blame.call_async(chat_id, tmp_dir, song_path, artist, name, album_id, song_id) except Exception as e: bot.send_message(chat_id, '⚠️ Oooops, an error occurred, we are on it') log.error(e)
def upload_blame(self, chat_id, tmp_dir, song_path, artist, name, album_id, song_id): try: audio = open(song_path, 'rb') message = bot.send_audio(STORAGE_GROUP_ID, audio, performer=artist, title=name, disable_notification=True) new_message_id = message.message_id os.remove(song_path) os.rmdir('{}/{}'.format(UPLOAD_DIR, tmp_dir)) d = Songs.query.filter_by(id=song_id).first() d.id = new_message_id db.session.commit() bot.send_message(chat_id, 'Done! You can now re-download the whole album') except Exception as e: bot.send_message(chat_id, '⚠️ Oooops, an error occurred, we are on it') log.error(e)
def callback_inline(call): try: if call.message: #Remove markup to prevent doubleclicks bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None) if 'album' in call.data: bot.send_message(call.message.chat.id, '👾 Your download has been started,\ it can take several minutes depending on server load. Please be patient') call_get_songs(call.data, call.message.chat.id) elif 'spotify_artist' in call.data: call_get_spoti_albums(call.data.split(':')[1], call.message.chat.id) elif 'spotify_al' in call.data: bot.send_message(call.message.chat.id, '👾 Your download has been started,\ it can take several minutes depending on server load. Please be patient') call_get_spoti_songs(call.data.split(':')[1], call.message.chat.id) elif 'bandcamp_search' in call.data: badcamp_search(call.message.chat.id, call.data.split(':')[1]) elif 'spoti_search' in call.data: spoti_search(call.message.chat.id, call.data.split(':')[1]) else: call_get_albums(call.data, call.message.chat.id) except Exception as e: bot.send_message(call.message.chat.id, '⚠️ Oooops, an error occurred, we are on it') log.error(e)
def call_get_albums(message, chat_id): if 'band:' in message: url = r.hget('user:{}:search', message) else: url = message albums = get_albums(url) key = types.InlineKeyboardMarkup() if len(albums) != 0: for album in albums: if len(album['url']) <= 64: callback = album['url'] else: search_key = 'user:{}:search'.format(chat_id) callback = 'album:{}'.format(random_string()) r.hset(search_key, callback, album['url']) r.expire(search_key, 240) key.add( types.InlineKeyboardButton(album['name'], callback_data=callback)) text = 'ЁЯС╛ Select album:' bot.send_message(chat_id, text, reply_markup=key) else: bot.send_message(chat_id, 'тЪая╕П Nothing found')
def download(self, obj, chat_id): try: artist = obj["artist"] album = obj["album"] songs = obj["songs"] cover = obj["cover"] tmp_dir = random_string() os.mkdir('{}/{}'.format(UPLOAD_DIR, tmp_dir)) #downloadcover r = requests.get(cover) cover_path = '{}/{}/{}.jpeg'.format(UPLOAD_DIR, tmp_dir, random_string()) with open(cover_path, 'wb') as f: f.write(r.content) #download songs order_list = [] for song in songs: for num, info in song.items(): name = info[0] url = info[1] tmp_song = random_string() if 'youtube' in url: order_element = download_youtube( num, tmp_dir, tmp_song, url, name) else: order_element = download_badcamp( num, tmp_dir, tmp_song, url, name) order_list.append(order_element) #call uploader self.uploader.upload.call_async(chat_id, order_list, tmp_dir, cover_path, artist, album, cover) except Exception as e: bot.send_message( chat_id, '⚠️ Oooops, an error occurred during album download, we are on it' ) log.error(e)
def handle_test(message): try: if message.text == '/search': bot.send_message(message.chat.id, '⚙️ What are you searching for?\n*Format*: _/search [artist/album]_', parse_mode='markdown') else: query = message.text.replace('/search ', '') key = types.InlineKeyboardMarkup() key.add(types.InlineKeyboardButton('Search and download from bandcamp', callback_data='bandcamp_search:{}'.format(query))) key.add(types.InlineKeyboardButton('Search in spotify, download from youtube', callback_data='spoti_search:{}'.format(query))) bot.send_message(message.chat.id, '👾 Select search method:', reply_markup=key) except Exception as e: bot.send_message(message.chat.id, '⚠️ Oooops, an error occurred, we are on it') log.error(e)