/
spotty.py
132 lines (115 loc) · 4.51 KB
/
spotty.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
""" Imports """
import discord
from discord.ext import commands
import asyncio
import spotipy
from utils.database import DatabasePointer
from utils.helpers import *
import traceback
# Spotify fetching code courtesy of ritiek https://github.com/plamere/spotipy/issues/246
# https://discordapp.com/oauth2/authorize?scope=bot&permissions=66321471&client_id=<bot-id> (optional)&guild_id=<server-id> Example on how to add with all perms
class Spotty(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(command_prefix=('!', '$', '(', ')', ';', '?', '.'))
self.__fetch_task = self.loop.create_task(self.fetch_all())
self.delay = delay
self._dbpointer = DatabasePointer()
self.credentials = generate_credentials()
self.spotify = spotipy.Spotify(client_credentials_manager=self.credentials)
self.__extensions = [
'cogs.commands.owner',
'cogs.commands.admin',
'cogs.commands.everyone',
'cogs.overrides'
]
""" Coroutine Fetch Functions """
async def fetch(self, channel_id, playlist_id, playlist_name, last_checked):
"""
Perform a fetch for a single database entry
:param channel_id: The channel id to post to
:param playlist_id: The playlist id to search
:param playlist_name: The playlist name for channels tracking multiple playlists
:return: None
"""
channel = self.get_channel(channel_id)
songs = await fetch_playlist(self.spotify, spotify_user_id, playlist_id, last_checked)
logging.info("Found {0} songs in '{1}'".format(
len(songs), playlist_name))
for song in songs:
await channel.send("New track added to '{0}'. {1}".format(playlist_name, song))
values = {
"last_checked": get_current_time(as_string=True),
"pid": playlist_id,
"channel_id": channel_id
}
self._dbpointer.update_time(values)
return len(songs)
async def fetch_all(self):
"""
Perform a fetch for all database entries
:return: None
"""
await self.wait_until_ready()
while not self.is_closed():
self.spotify = spotipy.Spotify(client_credentials_manager=self.credentials)
data = self._dbpointer.fetch_tracking_data()
for (playlist_id, playlist_name, channel_id, last_checked) in data:
await self.fetch(int(channel_id), playlist_id, playlist_name, convert_time(last_checked))
await asyncio.sleep(self.delay)
""" Construct an embedded message when !track is used """
async def track_embed(self, values):
thumbnail_url = await fetch_playlist_art(self.spotify, spotify_user_id, values[0]['playlist_id'])
embed = discord.Embed(
title='Playlists Added',
description='by %s' % values[0]['username'],
colour=discord.Colour.green()
)
embed.set_thumbnail(url=thumbnail_url)
embed.set_author(
name='Spotty',
icon_url=r'https://cdn.discordapp.com/attachments/509168690483298304/530556711329595392/spotty.png'
)
for val in values:
embed.add_field(name=val['playlist_name'], value='Success! :white_check_mark:', inline=True)
embed.set_footer(text='Use the tracking command to view the playlists being tracked.')
return embed
""" Embed for !stop command """
async def deleted_notify_embed(self, name=None):
if name is not None:
desc = str()
else:
desc = 'Channel is no longer tracking playlist'
embed = discord.Embed(
title='Stopped',
description=desc,
colour=discord.Colour.green()
)
embed.set_author(
name='Spotty',
icon_url=r'https://cdn.discordapp.com/attachments/509168690483298304/530556711329595392/spotty.png'
)
if name is not None:
embed.add_field(name=name, value='No longer being tracked. :x:', inline=True)
embed.set_footer(text='Use the tracking command to view the playlists being tracked.')
return embed
""" Embed for !tracking """
async def tracking_embed(self, data):
embed = discord.Embed(
title='Currently Tracking',
colour=discord.Colour.green()
)
embed.set_author(
name='Spotty',
icon_url=r'https://cdn.discordapp.com/attachments/509168690483298304/530556711329595392/spotty.png'
)
for (id, name) in data:
embed.add_field(name=name, value='Playlist ID: %d' % id, inline=True)
return embed
""" Load cogs """
def load_extensions(self):
for extension in self.__extensions:
try:
self.load_extension(extension)
except Exception as e:
logging.error('Failed to load extension %s' % str(extension))
traceback.print_exc()