-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (49 loc) · 2.19 KB
/
main.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
from telethon import TelegramClient, sync, events
import telebot
import requests
import spotipy
import spotipy.util as util
import random
from spotipy.oauth2 import SpotifyClientCredentials
from yandex_music import Client as YMClient
import re
class TGCredentials:
''' Данные от телеграма '''
api_id = 1111111
api_hash = 'api_hash'
session_name = 'session_name'
class SpotifyCredentials:
''' Данные от spotify '''
client_id = 'chat_id'
client_secret = 'client_secret'
class MusikReverse(object):
def __init__(self, spotify_client_id, spotify_client_secret):
self.spotify_service = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=spotify_client_id, client_secret=spotify_client_secret))
self.ym_service = YMClient()
def reverse(self, link):
if 'open.spotify.com' in link:
track_info = self.spotify_service.track(link)
search_result = self.ym_service.search(track_info['artists'][0]['name'] + track_info['name'])
ym_link = 'https://music.yandex.ru/track/' + str(search_result['best']['result']['id'])
return ym_link
elif 'music.yandex.ru' in link:
id_song = re.findall(r'track/(\d+)', link)[0]
track_info = self.ym_service.tracks(id_song)
search_result = self.spotify_service.search(track_info[0]['title'] + ' ' + track_info[0]['artists'][0]['name'] + ' ' + track_info[0]['albums'][0]['title'])
spotify_link = search_result['tracks']['items'][0]['external_urls']['spotify']
return spotify_link
tg_client = TelegramClient(
TGCredentials.session_name,
TGCredentials.api_id,
TGCredentials.api_hash)
musik_reverse = MusikReverse(
SpotifyCredentials.client_id,
SpotifyCredentials.client_secret
)
@tg_client.on(events.NewMessage())
async def normal_handler(event):
if 'open.spotify.com' in event.message.to_dict()['message'] or 'music.yandex.ru' in event.message.to_dict()['message']:
reverse_link = musik_reverse.reverse(event.message.to_dict()['message'])
await event.message.reply(reverse_link)
tg_client.start()
tg_client.run_until_disconnected()