-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicscope.py
executable file
·93 lines (85 loc) · 3.34 KB
/
musicscope.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
#!/bin/env python
from __future__ import unicode_literals
import youtube_dl
import praw
version = '1.3.0'
clientid = 'M4t6FAQerQ3hqA' # I got this from reddit.com once
clientsecret = None # Registered as an "installed app" so not nesc
uagent = 'rpi:setup-disk-radio:v'+version+' by /u/mttpgn' # This is made up
qlDB = 'trends'
songspath = '/srv/mp3/'
options = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True, # only keep the audio
'audioformat' : "mp3", # convert to mp3
'outtmpl': songspath + '%(id)s', #name the file the ID of the video
'noplaylist' : True, # only download single song, not playlist
}
with open('subreddits.txt', 'r') as cfg:
subs = cfg.read().splitlines()
dlim = 150
def dlSong(source, domain):
with youtube_dl.YoutubeDL(options) as ydl:
if domain == u'youtu.be':
try:
name = source.split("be/")[1]
if os.path.isfile(name):
print "File already exists."
else:
print "Downloading song from " + source
try:
ydl.download([source])
print "MP3 downloaded."
except:
print "Error, video not downloaded."
except:
print "Warning: not sure of the YouTube ID yet. Possible dupe. Trying anyway."
try:
ydl.download([source])
except:
print "Error, video failed to downloaded."
elif domain == u'youtube.com':
try:
name = source.split("v=","&")[1]
if os.path.isfile(name):
print "File already exists"
else:
print "Downloading song from " + source
try:
ydl.download([source])
except:
print "Error, video not downloaded."
except:
print "Warning: not sure of the YouTube ID yet. Possible dupe. Trying again."
try:
ydl.download([source])
except:
print "Error, video failed to downloaded."
else:
print "Could not pull mp3 from domain " + domain
def main():
reddit = praw.Reddit(client_id=clientid, \
client_secret=clientsecret, \
user_agent=uagent)
ctr = 0
subreddits = [reddit.subreddit(q) for q in subs]
for sub in subreddits:
print sub
print "In Subreddit \""+sub.display_name+"\":"
for submission in sub.hot(limit=dlim):
# Top dlim # of posts in this subreddit sorted by what's "Hot".
songObj = submission
d = songObj.domain
u = songObj.url
# d is the external site the reddit user submission points too.
if u'youtu' in d:
# This includes youtu.be as well as youtube.com
print "Adding song " + str(ctr) + " source: " + d
ctr += 1
dlSong(u, d)
with open('log/dl.log', 'w') as logfile:
logfile.write("Downloaded {1} new files on {2}.".format( \
str(ctr),
str(datetime.now()) )
if __name__ == "__main__":
main()