├── screenshots ├── screenshot1.png ├── screenshot2.jpg ├── screenshot3.png ├── screenshot4.jpg ├── screenshot5.png ├── screenshot6.jpg └── screenshot7.jpg ├── README.md └── GaanaBot.py /screenshots/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot1.png -------------------------------------------------------------------------------- /screenshots/screenshot2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot2.jpg -------------------------------------------------------------------------------- /screenshots/screenshot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot3.png -------------------------------------------------------------------------------- /screenshots/screenshot4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot4.jpg -------------------------------------------------------------------------------- /screenshots/screenshot5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot5.png -------------------------------------------------------------------------------- /screenshots/screenshot6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot6.jpg -------------------------------------------------------------------------------- /screenshots/screenshot7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayank26saxena/gaanabot/HEAD/screenshots/screenshot7.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Synopsis 2 | GaanaBot is a bot for Telegram. It makes the process of downloading songs and finding lyrics for any song as easy as ever. 3 | To download a song, send name of the song and GaanaBot will send you the song in the form of an audio message. 4 | To find lyrics, send a message in the format - **_Lyrics \ - \_** 5 | 6 | # Motivation 7 | It has always been really hard to download music for free. The process is long and cumbersome and doesn't guarantee whether the correct song will be downloaded at the end or not. I decided to fix this problem by developing **GaanaBot**. 8 | 9 | # How to Use 10 | - Download Telegram app for Android/iOS platform. 11 | - Search for GaanaBot. 12 | - Send name of song to download song. 13 | - To find lyrics send a message in the formatin - **_Lyrics \ - \_** 14 | 15 | That's it. It is that simple! 16 | 17 | # Screenshots 18 | ![alt tag](https://github.com/mayank26saxena/GaanaBot/blob/master/screenshots/screenshot4.jpg) 19 | ![alt tag](https://github.com/mayank26saxena/GaanaBot/blob/master/screenshots/screenshot2.jpg) 20 | ![alt tag](https://github.com/mayank26saxena/GaanaBot/blob/master/screenshots/screenshot6.jpg) 21 | ![alt tag](https://github.com/mayank26saxena/GaanaBot/blob/master/screenshots/screenshot7.jpg) 22 | 23 | -------------------------------------------------------------------------------- /GaanaBot.py: -------------------------------------------------------------------------------- 1 | import telepot 2 | import urllib 3 | import urllib2 4 | from bs4 import BeautifulSoup 5 | import webbrowser 6 | import json 7 | import time 8 | import os 9 | import pynotify 10 | from datetime import datetime 11 | import re 12 | from slugify import slugify 13 | import codecs 14 | 15 | #Download path for music file 16 | path = '/home/mayank/Desktop/TelegramBot/Songs/' 17 | 18 | database_path = '/home/mayank/Desktop/TelegramBot/Database/' 19 | userdata_file_path = database_path + 'userdata.json' 20 | songdata_file_path = database_path + 'songdata.json' 21 | searchdata_file_path = database_path + 'searchdata.json' 22 | 23 | #Telegram bot token ID 24 | TOKEN = '###############################################' 25 | 26 | #Base url for fetching json object 27 | BASE_URL = 'http://www.youtubeinmp3.com/fetch/?format=JSON&video=' 28 | 29 | #Keywords 30 | SUCCESS = 'Success' 31 | FAIL = 'Fail' 32 | ERROR = 'Error in pushing song.' 33 | WELCOME_MSG = 'To start using GaanaBot send the name of the song you want to download. To get lyrics send a message in the format \'Lyrics - \'' 34 | ONE_MB = 1000000 35 | SONG_SENT_MESSAGE = 'Download is complete. Song is being sent to you. Please wait. To save the song click on it and choose Save to Music option.' 36 | 37 | BASE_LYRICS_URL = 'http://lyric-api.herokuapp.com/api/find/' 38 | SLASH = '/' 39 | LYRICS_ERROR_MSG = 'Lyrics not found. Message should be in the following format : Lyrics - ' 40 | LYRICS_WAITING_MSG = 'Looking up lyrics for the song you requested. Please wait.' 41 | 42 | 43 | def handle(msg): 44 | #Get chat_id and text from message which has been received 45 | print 'Message received:', msg 46 | username = msg['from']['first_name'] 47 | chat_id = msg['from']['id'] 48 | command = msg['text'] 49 | 50 | 51 | print ('Username: ' + username) 52 | print ('Got message: %s' % command) 53 | 54 | if command == '/start': 55 | bot.sendMessage(chat_id, WELCOME_MSG) 56 | userdata = {'userid': msg['from']['id'], 57 | 'username': msg['from'], 58 | } 59 | savedata(userdata,userdata_file_path) 60 | return 61 | 62 | first_word = command.split(' ', 1)[0] 63 | print 'first word is : ' + first_word 64 | lower_first_word = first_word.lower() 65 | 66 | 67 | getlyrics = False 68 | if lower_first_word == 'lyrics' : 69 | getlyrics = True 70 | sendlyrics(msg) 71 | else: 72 | sendsong(msg) 73 | searchdata = {'userid': msg['from']['id'], 74 | 'username': msg['from'], 75 | 'searchterm': msg['text'], 76 | 'date' : msg['date'], 77 | 'lyrics' : getlyrics 78 | } 79 | savedata(searchdata,searchdata_file_path) 80 | return 81 | 82 | def sendlyrics(msg): 83 | username = msg['from']['first_name'] 84 | chat_id = msg['from']['id'] 85 | command = msg['text'] 86 | 87 | bot.sendMessage(chat_id, LYRICS_WAITING_MSG) 88 | 89 | first_word = command.split(' ', 1)[0] 90 | s = command.split(first_word + ' ', 1)[1] 91 | p = s.index('-') 92 | song_name = s[:p] 93 | song_name = song_name.lower() 94 | 95 | if song_name[-1] == ' ' : 96 | song_name = song_name[0:-1].strip() 97 | print song_name 98 | 99 | artist_name = s[p+1:] 100 | artist_name = artist_name.lower() 101 | 102 | if artist_name[-1] == ' ' : 103 | artist_name = artist_name[0:-1] 104 | print artist_name 105 | 106 | artist_name = artist_name.replace(' ', '%20') 107 | song_name = song_name.replace(' ', '%20') 108 | 109 | print artist_name 110 | print song_name 111 | 112 | LYRICS_URL = BASE_LYRICS_URL + artist_name + SLASH + song_name 113 | print LYRICS_URL 114 | 115 | data = json.load(urllib2.urlopen(LYRICS_URL)) 116 | 117 | if data['lyric'] == '' : 118 | print 'Lyrics not found.' 119 | bot.sendMessage(chat_id, LYRICS_ERROR_MSG) 120 | else : 121 | lyrics = data['lyric'] 122 | print lyrics 123 | bot.sendMessage(chat_id, lyrics) 124 | return 125 | 126 | def sendsong(msg): 127 | username = msg['from']['first_name'] 128 | chat_id = msg['from']['id'] 129 | command = msg['text'] 130 | command = codecs.encode(command,'utf-8') 131 | bot.sendMessage(chat_id, username + ', your song is on its way..') 132 | 133 | #Searching YouTube with text which has been received 134 | 135 | query = urllib.quote(command) 136 | url = "https://www.youtube.com/results?search_query=" + query 137 | response = urllib2.urlopen(url) 138 | 139 | #Reading response and finding top result 140 | html = response.read() 141 | soup = BeautifulSoup(html, "lxml") 142 | 143 | for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}): 144 | #Getting Video Url from first link in search results 145 | VIDEO_URL = 'https://www.youtube.com' + vid['href'] 146 | print ('Video URL : ' + VIDEO_URL) 147 | 148 | #bot.sendMessage(chat_id, VIDEO_URL) 149 | 150 | #Creating Json Url for obtaining download link, length and title of song 151 | JSON_URL = BASE_URL + VIDEO_URL 152 | print ('JSON URL : ' + JSON_URL) 153 | 154 | #bot.sendMessage(chat_id, JSON_URL) 155 | 156 | #Loading data from json object 157 | response = urllib.urlopen(JSON_URL) 158 | 159 | try: 160 | data = json.loads(response.read()) #Json object 161 | print (data) 162 | 163 | #Getting length, download url and title of song from json object 164 | if 'length' not in data: 165 | raise ValueError("No length in given data") 166 | print ('No length in given data') 167 | break 168 | if 'link' not in data: 169 | raise ValueError("No link in given data") 170 | print ('No link in given data') 171 | break 172 | if 'title' not in data: 173 | raise ValueError("No title in given data") 174 | print ('No title in given data') 175 | break 176 | 177 | length = data['length'] 178 | print ('Length : ' + str(length)) 179 | DOWNLOAD_URL = data['link'] 180 | print ('DOWNLOAD_URL : ' + DOWNLOAD_URL) 181 | title = data['title'] 182 | print ('Title = ' + title) 183 | 184 | title = slugify(title) 185 | upload_file = path + title.lower() + '.mp3' 186 | print ('upload_file name : ' + upload_file) 187 | 188 | if not (os.path.exists(upload_file)) : 189 | bot.sendMessage(chat_id, 'Download for your song has started..') 190 | print ('File does not exist: downloading') 191 | #DownloadSong method being called for downloading song 192 | downloadSong(DOWNLOAD_URL, upload_file) 193 | file_size = checkFileSize(upload_file) 194 | if (file_size < ONE_MB) : 195 | os.remove(upload_file) 196 | print ('Deleted small file.') 197 | continue 198 | bot.sendMessage(chat_id, SONG_SENT_MESSAGE) 199 | print ('Download is complete.') 200 | else: 201 | print ('File exists: no need to download') 202 | 203 | print ('Uploading song') 204 | print ('Start time - ' + str(datetime.now())) 205 | 206 | #Opening latest file in downloads folder and sending file as message 207 | audio = open(upload_file , 'rb') 208 | bot.sendAudio(chat_id, audio, length , '', title) 209 | 210 | print ('Successful') 211 | print ('End time - ' + str(datetime.now())) 212 | sendmessage(SUCCESS, title + ' was pushed successfully to ' + username + '.') 213 | songdata = {'searchterm': command, 214 | 'searchresult': title.lower(), 215 | 'date' : msg['date'] 216 | } 217 | savedata(songdata,songdata_file_path) 218 | break 219 | 220 | except ValueError: 221 | print ('No song found') 222 | bot.sendMessage(chat_id, 'No song found. Please try again with a different keyword.') 223 | sendmessage(FAIL, ERROR) 224 | break 225 | 226 | return 227 | 228 | def savedata(data, filename): 229 | msg = json.dumps(data) 230 | with open(filename, 'a') as f: 231 | json.dump(msg, f) 232 | f.write(os.linesep) 233 | 234 | def sendmessage(title, message): 235 | pynotify.init("Test") 236 | notice = pynotify.Notification(title, message) 237 | notice.show() 238 | return 239 | 240 | def downloadSong(url, title): 241 | usock = urllib2.urlopen(url) 242 | print ('info: ', usock.info()) 243 | f = open(title, 'wb') 244 | try : 245 | file_size = int(usock.info().getheaders("Content-Length")[0]) 246 | print ('Downloading : %s Bytes: %s' % (title, file_size)) 247 | except IndexError: 248 | print ('Unknown file size: index error') 249 | 250 | downloaded = 0 251 | block_size = 8192 252 | while True: 253 | buff = usock.read(block_size) 254 | if not buff: 255 | break 256 | 257 | downloaded = downloaded + len(buff) 258 | f.write(buff) 259 | #download_status = r"%3.2f%%" % (downloaded * 100.00 / file_size) 260 | #download_status = download_status + (len(download_status)+1) * chr(8) 261 | #print download_status,"done" 262 | 263 | f.close() 264 | 265 | def checkFileSize(upload_file_path): 266 | b = os.path.getsize(upload_file_path) 267 | return b 268 | 269 | 270 | 271 | bot = telepot.Bot(TOKEN) 272 | bot.getMe() 273 | bot.notifyOnMessage(handle) 274 | print ('I am listening..') 275 | --------------------------------------------------------------------------------