-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
127 lines (108 loc) · 3.93 KB
/
helpers.py
File metadata and controls
127 lines (108 loc) · 3.93 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
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
import requests
import re
import asyncio
import discord
#$ Helper Functions
#^ Populate embeds
def populate_np_embed(song_details):
"""Create a Discord embed for now playing song details"""
embed = discord.Embed(
title=f"{song_details['name']}",
color=0x1DB954 # green
)
embed.add_field(
name="Artist(s)",
value=", ".join(song_details['artists']),
inline=True
)
embed.add_field(
name="Album",
value=f"{song_details['album']}",
inline=True
)
embed.add_field(
name="Release Date",
value=f"{song_details['release_date']}",
inline=True
)
if song_details['image_url']:
embed.set_thumbnail(url=song_details['image_url'])
if song_details['spotify_url']:
embed.add_field(
name="Listen on Spotify",
value=f"[Spotify Link]({song_details['spotify_url']})",
inline=False
)
return embed
def populate_lp_embed(songs, count):
"""Create a Discord embed for last played songs"""
embed = discord.Embed(
title=f"Last Played - {count} Songs",
color=0xe4001b
)
lines = []
for i, song in enumerate(songs, 1):
lines.append(f"{i}. **{song['title'].title()}** Played at {song['time']}")
embed.description = "\n".join(lines)
return embed
#^ Update activity
async def update_activity(status, bot):
"""Update the bot's Discord activity status"""
if status:
# Set activity as Listening
activity = discord.Activity(type=discord.ActivityType.listening, name=status)
await bot.change_presence(status=discord.Status.online, activity=activity)
else:
# Clear activity
await bot.change_presence(status=discord.Status.online, activity=None)
#^ other helpers
def get_title_from_api(songs_url):
"""Fetch the current song title from API"""
"""This will depend on the actual API structure -> adjust as needed"""
response = requests.get(songs_url)
response.raise_for_status()
data = response.json()
icestats = data.get("icestats", {})
title = None
for key, value in icestats.items():
if isinstance(value, dict) and "title" in value:
title = value["title"]
break
return title.title() if title else None
async def get_song_details(title, spotify):
"""Fetch detailed song information from Spotify"""
if not spotify:
return None
try:
# Clean up the title for better search results
clean_title = re.sub(r'[^\w\s-]', '', title)
# Add retry logic for connection issues
for attempt in range(3): # Try up to 3 times
try:
# Search for the track
results = spotify.search(q=clean_title, type='track', limit=1)
break # Success, exit retry loop
except Exception as e:
print(f"Spotify search attempt {attempt + 1} failed: {e}")
if attempt == 2: # Last attempt
return None
await asyncio.sleep(2) # Wait 2 seconds before retry
if results['tracks']['items']:
track = results['tracks']['items'][0]
# Get album info
album = track['album']
artists = [artist['name'] for artist in track['artists']]
return {
'name': track['name'],
'artists': artists,
'album': album['name'],
'release_date': album['release_date'],
'image_url': album['images'][0]['url'] if album['images'] else None,
'spotify_url': track['external_urls']['spotify'],
}
except Exception as e:
print(f"Spotify API error: {e}")
return None
def get_title_normalized(title):
"""Normalize title for comparison"""
return re.sub(r"\s+", " ", title).strip().lower()