-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·152 lines (128 loc) · 5.23 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
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
149
150
151
152
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 22 10:23:26 2019
@author: cwong
"""
import praw
from html.parser import HTMLParser
from urllib.parse import urlparse
import pprint
from config import Config
from musicplatform import YoutubePlaylist, SpotifyPlaylist
def main():
c = Config()
reddit = get_reddit(c)
# for item in reddit.inbox.all(limit=None):
# print(repr(item))
platforms = {
'Youtube': YoutubePlaylist(
client_id=c.google_playlistconverter_client_id,
secret=c.google_playlistconverter_secret,
scopes=['https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/youtube.readonly']
),
# 'Google': [],
'Spotify': SpotifyPlaylist(username=c.spotify_username,
client_id=c.spotify_client_id,
secret=c.spotify_secret)
}
msg_template = '\n - {} playlist here: {}'
for unread in reddit.inbox.unread(limit=10):
# pprint.pprint(unread.body)
if c.reddit_username not in unread.body:
print('no username mentioned')
# Should check to see if the parent comment has a link in it
unread.mark_read()
continue
msg = '🎵🎵🎵 \n\nHello! The converted playlists are below: '
urls = get_urls(unread.body_html)
if len(urls) == 0:
# Get the parent comment and see if it has URLs in it
pass
for url in urls:
if 'youtube' in url.lower():
continue
src_p = get_platform(url)
dst_platforms = [key for key in platforms.keys() if key != src_p]
try:
src_platform = platforms[src_p]
except KeyError:
print('Platform is unsupported: {}'.format(url))
continue
# Obtain playlist information from source platform
src_playlist = src_platform.get_playlist(url)
src_playlist_name = src_playlist.name
src_playlist_desc = src_playlist.description
# Retrieve track information from the source playlist
tracks = src_platform.get_playlist_tracks(url)
# Loop through the destination platforms and create playlists
for dst_p in dst_platforms:
dst_platform = platforms[dst_p]
# Create the playlist on the destination platform
dst_playlist = dst_platform.create_playlist(src_playlist_name,\
description=src_playlist_desc)
dst_playlist_url = dst_playlist.uri
dst_platform.add_songs_to_playlist(dst_playlist.id, tracks)
msg += msg_template.format(
dst_platform.platform_name, dst_playlist_url)
print(msg)
if dst_platform.platform_name == 'YouTube':
print('YouTube Quota: {}'.format(dst_platform.quota))
msg += msg_template.format('YouTube Music',
dst_playlist_url.replace(
'www.youtube',
'music.youtube'))
print(msg)
msg += ('\n\n\nSend me a PM if you encountered an error. The YouTube '
'API has a limit to the number of playlists a given user can '
'create in a day and the bot could\'ve exceeded the limit for '
'the day.')
print(msg)
unread.reply(msg)
unread.mark_read()
def get_reddit(config):
return praw.Reddit(
user_agent='Playlist Converter (by /u/PlaylistConverter)',
client_id=config.reddit_client_id,
client_secret=config.reddit_secret,
username=config.reddit_username,
password=config.reddit_password)
def get_platform(url: str) -> str:
url = url.lower()
try:
parsed_uri = urlparse(url)
except ValueError:
return 'Unsupported'
netloc = parsed_uri.netloc
if 'spotify.com' in netloc:
return 'Spotify'
elif 'youtube.com' in netloc:
return 'Youtube'
elif 'google.com' in netloc:
return 'Google'
elif 'apple.com' in netloc:
return 'Apple'
else:
return 'Unsupported'
def get_urls(html: str) -> list:
parser = AnchorParser()
parser.feed(html)
return parser.URLS
class AnchorParser(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.URLS = []
def clean(self):
self.URLS = []
def handle_starttag(self, tag, attrs):
if tag == 'a':
for attr in attrs:
if attr[0] == 'href':
self.URLS.append(attr[1])
def handle_endtag(self, tag):
pass
def handle_data(self, data):
pass
if __name__ == '__main__':
main()