forked from An0nimia/DeezSpot_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
197 lines (148 loc) · 4.55 KB
/
utils.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
#!/usr/bin/python3
from time import sleep
from requests import get
from os import path, makedirs
from bs4 import BeautifulSoup
import spotipy.oauth2 as oauth2
from configparser import ConfigParser
from sqlite3 import (
connect, OperationalError, IntegrityError
)
from settings import (
db_file, loc_dir,
ini_file, song_default_image
)
config = ConfigParser()
config.read(ini_file)
try:
ya_key = config['yandex']['key']
except KeyError:
print("Bot utils: Something went wrong with configuration file")
exit()
def request(url):
try:
thing = get(url)
except:
thing = get(url)
return thing
def translate(language, sms):
try:
language = language.split("-")[0]
if not "en" in language:
api = (
"https://translate.yandex.net/api/v1.5/tr.json/translate?key=%s&text=%s&lang=en-%s"
% (
ya_key,
sms,
language
)
)
sms = request(api).json()['text'][0]
except:
pass
return sms
def write_db(execution):
conn = connect(db_file)
c = conn.cursor()
while True:
sleep(1)
try:
c.execute(execution)
conn.commit()
conn.close()
break
except OperationalError:
pass
except IntegrityError:
break
def view_db(execution):
conn = connect(db_file)
c = conn.cursor()
match = c.execute(execution).fetchone()
conn.close()
return match
def check_image(image1, ids, kind):
if kind == "track":
if not image1:
URL = "https://www.deezer.com/track/%s" % ids
body = get(URL).text
image1 = (
BeautifulSoup(body, "html.parser")
.find("meta", property="og:image")
.get("content")
.replace("500x500", "1000x1000")
)
elif kind == "album":
if not image1:
URL = "https://www.deezer.com/album/%s" % ids
body = request(URL).text
image1 = (
BeautifulSoup(body, "html.parser")
.find("img", class_="img_main")
.get("src")
.replace("200x200", "1000x1000")
)
ima = request(image1).content
if len(ima) == 13:
image1 = song_default_image
return image1
def get_image(image):
imag = image.split("/")
if imag[-2] == "image" or imag[-2] == "":
imag = loc_dir + imag[-1]
else:
imag = loc_dir + imag[-2]
image = request(image)
img = open(imag, "wb")
img.write(image.content)
img.close()
image.url = imag
return image
def statisc(chat_id, do):
conn = connect(db_file)
c = conn.cursor()
if do == "USERS":
c.execute("SELECT chat_id FROM CHAT_ID where chat_id = '%d'" % chat_id)
if not c.fetchone():
write_db("INSERT INTO CHAT_ID (chat_id) values ('%d')" % chat_id)
c.execute("SELECT chat_id FROM CHAT_ID")
elif do == "TRACKS":
c.execute("SELECT id FROM DWSONGS")
infos = len(
c.fetchall()
)
conn.close()
return infos
def initialize():
if not path.isdir(loc_dir):
makedirs(loc_dir)
conn = connect(db_file)
c = conn.cursor()
c.execute(
"CREATE TABLE IF NOT EXISTS DWSONGS (id text, query text, quality text, PRIMARY KEY (id, quality))")
c.execute("CREATE TABLE IF NOT EXISTS BANNED (banned int, PRIMARY KEY (banned))")
c.execute(
"CREATE TABLE IF NOT EXISTS CHAT_ID (chat_id int, PRIMARY KEY (chat_id))")
conn.commit()
conn.close()
#def generate_token():
#return oauth2.SpotifyClientCredentials(
# client_id = "c6b23f1e91f84b6a9361de16aba0ae17",
# client_secret = "237e355acaa24636abc79f1a089e6204"
#).get_access_token(as_dict = False)
#def generate_token():
# os.environ["SPOTIPY_CLIENT_ID"] = "c6b23f1e91f84b6a9361de16aba0ae17"
# os.environ["SPOTIPY_CLIENT_SECRET"] = "237e355acaa24636abc79f1a089e6204"
# spo = spotipy.Spotify(
# client_credentials_manager=SpotifyClientCredentials())
# print(spo)
# return spo
def fast_split(thing):
return (
thing
.split("(")[-1]
.split(")")[0]
)
def image_resize(image, size):
size = "{}x{}".format(size, size)
return image.replace("1000x1000", size)