-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbio.py
More file actions
92 lines (79 loc) · 3.03 KB
/
bio.py
File metadata and controls
92 lines (79 loc) · 3.03 KB
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
import os
import json
import getpass
import time
import requests
from telethon import TelegramClient, functions
import pylast
# Настройки Last.fm
API_KEY = "API_KEY" # Insert your Last.fm API Key
API_SECRET = "API_KEY_SECRET" # Insert your Last.fm API Secret
SESSION_FILE = os.path.expanduser("~/..lastfm_session.json")
LASTFM_USER = None # будет подставлен после входа
# Настройки Telegram
api_id = API_ID # твой api_id
api_hash = 'AI_HASH'
# === Функции для работы с session key ===
def save_session_key(session_key, username):
with open(SESSION_FILE, 'w') as f:
json.dump({'session_key': session_key, 'username': username}, f)
def load_session_key():
if os.path.exists(SESSION_FILE):
with open(SESSION_FILE, 'r') as f:
data = json.load(f)
return data.get("session_key"), data.get("username")
return None, None
def obtain_session_key():
username = input("Last.fm username: ")
password = getpass.getpass("Last.fm password: ")
password_hash = pylast.md5(password)
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)
skg = pylast.SessionKeyGenerator(network)
try:
session_key = skg.get_session_key(username, password_hash)
print("[INFO] Connected to Last.fm as", username)
save_session_key(session_key, username)
return session_key, username
except Exception as e:
print("[ERROR] Failed to get session_key:", e)
return None, None
# === Получаем или загружаем session key ===
session_key, LASTFM_USER = load_session_key()
if not session_key or not LASTFM_USER:
session_key, LASTFM_USER = obtain_session_key()
if not session_key:
print("Can't continue without Last.fm session key.")
exit(1)
# Создаём объект сети last.fm с сессией
lastfm_network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, session_key=session_key)
# === Telegram client ===
client = TelegramClient('tg_session', api_id, api_hash)
async def get_now_playing():
try:
user = lastfm_network.get_user(LASTFM_USER)
track = user.get_now_playing()
if track:
artist = track.get_artist().get_name()
title = track.get_title()
return f"🎵 {artist} — {title}"
else:
return None
except Exception as e:
print("[ERROR] Last.fm get_now_playing:", e)
return None
async def main():
await client.start()
last_bio = None
while True:
now_playing = await get_now_playing()
if now_playing and now_playing != last_bio:
try:
await client(functions.account.UpdateProfileRequest(about=now_playing))
print("Bio updated:", now_playing)
last_bio = now_playing
except Exception as e:
print("Error updating bio:", e)
await asyncio.sleep(60)
if __name__ == '__main__':
import asyncio
asyncio.run(main())