-
Notifications
You must be signed in to change notification settings - Fork 0
/
last2mast.py
148 lines (112 loc) · 4.09 KB
/
last2mast.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
import pylast
import mastodon
from datetime import datetime, timedelta
import time
import re
import requests
# Last.fm API credentials
LASTFM_API_KEY = "YOUR_LASTFM_API_KEY"
LASTFM_API_SECRET = "YOUR_LASTFM_API_SECRET"
LASTFM_USERNAME = "YOUR_LASTFM_USERNAME"
# Mastodon API credentials
MASTODON_ACCESS_TOKEN = "YOUR_MASTODON_ACCESS_TOKEN"
MASTODON_API_BASE_URL = "YOUR_MASTODON_INSTANCE_URL"
# Shlink server credentials
SHLINK_API_KEY = "YOUR_SHLINK_API_KEY"
SHLINK_SERVER_URL = "YOUR_SHLINK_SERVER_URL/"
# Initialize Last.fm network
network = pylast.LastFMNetwork(
api_key=LASTFM_API_KEY,
api_secret=LASTFM_API_SECRET,
)
# Initialize Mastodon API client
mastodon_client = mastodon.Mastodon(
access_token=MASTODON_ACCESS_TOKEN,
api_base_url=MASTODON_API_BASE_URL
)
posted_track_titles = set()
posted_weekly_update = False
def post_loved_track(track):
"""Posts the loved track to Mastodon, avoiding duplicates."""
artist = track.artist.name
title = track.title
album = track.get_album()
url = track.get_url()
lastfm_profile = f"https://www.last.fm/user/{LASTFM_USERNAME}"
if title in posted_track_titles:
print(f"Skipping duplicate track: {artist} - {title}")
return
try:
response = requests.post(
f"{SHLINK_SERVER_URL}/rest/v2/short-urls",
headers={
"X-Api-Key": f"{SHLINK_API_KEY}",
"Content-Type": "application/json; charset=utf-8",
},
json={"longUrl": url},
)
response.raise_for_status()
short_url = response.json()["shortUrl"]
except requests.exceptions.RequestException as e:
print(f"Error shortening URL: {e}")
short_url = url
hashtags = []
hashtags.append(f"#{re.sub(r'[^a-zA-Z0-9]', '', artist)}")
if album:
hashtags.append(f"#{re.sub(r'[^a-zA-Z0-9]', '', album.title)}")
try:
genre = track.get_top_tags()[0].item.name
hashtags.append(f"#{genre}")
except IndexError:
pass
hashtag_string = " ".join(hashtags)
message = f"❤️ Loved: {artist} - {title} {short_url}\n\n{hashtag_string}\n\n{lastfm_profile}"
if len(message) > 500:
message = message[:497] + "..."
mastodon_client.status_post(message)
posted_track_titles.add(title)
def post_weekly_top_artists_and_tracks():
"""Posts the weekly top artists and tracks to Mastodon, only once per Friday."""
global posted_weekly_update
if posted_weekly_update:
return
top_artists = network.get_user(LASTFM_USERNAME).get_top_artists(
period='7day',
limit=5,
)
top_tracks = network.get_user(LASTFM_USERNAME).get_top_tracks(
period='7day',
limit=5,
)
lastfm_profile = f"https://www.last.fm/user/{LASTFM_USERNAME}"
message = "🎶 Weekly Top Artists and Tracks:\n\n"
message += "**Top Artists:**\n"
for artist in top_artists:
message += f"- {artist.item.name} ({artist.weight} plays)\n"
message += "\n**Top Tracks:**\n"
for track in top_tracks:
message += f"- {track.item.artist.name} - {track.item.title} ({track.weight} plays)\n"
message += f"\n{lastfm_profile}"
if len(message) > 500:
message = message[:497] + "..."
mastodon_client.status_post(message)
posted_weekly_update = True
def main():
last_loved_track = None
global posted_weekly_update
while True:
try:
loved_tracks = network.get_user(LASTFM_USERNAME).get_loved_tracks(limit=1)
except pylast.NetworkError as e:
print(f"Error fetching loved tracks: {e}")
loved_tracks = None
# Access the track title correctly
if loved_tracks and loved_tracks[0].track.title != last_loved_track:
post_loved_track(loved_tracks[0].track) # Pass the Track object
last_loved_track = loved_tracks[0].track.title # Store the track title
if datetime.today().weekday() == 4: # Friday is 4
post_weekly_top_artists_and_tracks()
posted_weekly_update = False
time.sleep(300)
if __name__ == "__main__":
main()