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