-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkpodcast
executable file
·162 lines (133 loc) · 4.99 KB
/
mkpodcast
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
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
import re
import os
import sys
import glob
import shutil
import hashlib
import logging
from os import path
from datetime import date
sys.path.extend(glob.glob(path.join(path.dirname(__file__), "mkpodcast-libs/*")))
import requests
from mutagen import File
from mutagen.id3 import APIC, error as mp3_error
log = logging.getLogger()
class obj(object):
def __init__(self, attrs):
self.__dict__.update(attrs)
def seconds2duration(seconds):
return "%d:%02d" %(seconds / 60, seconds % 60)
def mp3_attr(mp3, key, attr="text"):
if key not in mp3:
return ""
val = mp3[key]
val = getattr(val, attr)
if isinstance(val, list) and len(val) > 0:
val = val[0]
return val or ""
def check_url(url):
res = requests.get(url)
if res.status_code > 299:
print "ERROR %s checking that URL is valid: %s" %(
res.status_code, url,
)
return 1
print "URL looks good: HTTP %s %s" %(res.status_code, url)
def main():
logging.basicConfig()
if len(sys.argv) != 3:
audio_dir = os.path.expanduser("~/Google Drive/xvzf/xvzf audio/")
print "Usage: %s MP3_FILE TARGET_NAME" %(sys.argv[0], )
print "Example: %s '%s' xvzf.persistence.dessy.mp3" %(sys.argv[0], audio_dir)
print
print "Speaker spreadsheet: https://docs.google.com/spreadsheets/d/1VJpqRNu5ixO6kmiCeqe5SxrA1kcO5RwgNdi2Xf9lz4o/edit#gid=0"
available_audio = glob.glob(audio_dir + "*.mp3")
if available_audio:
print "Available audio files:"
print " " + "\n ".join(available_audio)
return 1
mp3_file, target_name = sys.argv[1:]
image_name = re.sub("\.[a-zA-Z]+\.mp3", ".png", target_name)
img_path = os.path.join(os.path.dirname(__file__), "source/podcast/img/", image_name)
if not os.path.exists(img_path):
print "ERROR: %s does not exist" %(img_path, )
return 1
if check_url("http://src.xvzf.io/img/%s" %(image_name, )):
return 1
# See: http://stackoverflow.com/questions/409949/how-do-you-embed-album-art-into-an-mp3-using-python
mp3 = File(mp3_file)
#{u'APIC:': APIC(encoding=0, mime=u'image/png', type=0, desc=u'', data=...'), 'TDRC': TDRC(encoding=0, text=[u'2014']), 'TIT2': TIT2(encoding=0, text=[u'Hoss Gifford on Feb 27th']), u"COMM::'eng'": COMM(encoding=0, lang='eng', desc=u'', text=[u'These are the comments ']), 'TRCK': TRCK(encoding=0, text=[u'1']), 'TPE1': TPE1(encoding=0, text=[u'xvzf']), 'TALB': TALB(encoding=0, text=[u'nontrivial']), 'TCOM': TCOM(encoding=0, text=[u'Hoss Gifford'])}
try:
mp3.add_tags()
except mp3_error:
# raised if the mp3 already has IDv3 tags
pass
mp3.tags.add(
APIC(
encoding=3, # 3 is for utf-8
mime='image/png', # image/jpeg or image/png
type=0, # 0 is for the 'other', which seems to be the one iTunes uses
desc='',
data=open(img_path).read()
)
)
mp3.save()
md5 = hashlib.md5()
with open(mp3_file) as f:
for hunk in iter(lambda: f.read(4096), ""):
md5.update(hunk)
remote_path = "/var/www/xvzf.io/shared/mp3/%s" %(target_name, )
res = os.system("scp '%s' 'xvzf.io:%s'" %(
mp3_file.replace("'", "\\'"),
remote_path,
))
if res != 0:
print "ERROR: scp failed with status", res
return res
res = os.system("ssh xvzf.io 'chmod +r %s'" %(remote_path, ))
if res != 0:
print "ERROR: ssh failed to chmod file:", res
return res
print "File copied to:", remote_path
if check_url("http://src.xvzf.io/mp3/%s" %(target_name, )):
return 1
mp3_file_size = os.path.getsize(mp3_file)
published_dir = os.path.join(os.path.dirname(mp3_file), "published/")
if os.path.exists(published_dir):
shutil.move(mp3_file, published_dir)
print "%s moved to %s" %(os.path.basename(mp3_file), published_dir)
else:
print "!!! WARNING !!!"
print published_dir, "DOES NOT EXIST"
print "Manually move the MP3 into the 'published/' directory."
raw_input("Press enter to continue...")
image_url = "xvzf.%s.png" %(target_name.split(".")[-3], )
image_path = "./source/podcast/img/%s" %(image_url, )
if not os.path.exists(image_path):
print "ERROR: image does not exist: %s" %(image_path, )
sys.exit(1)
print u"""
Put this at the top of data/talks.yaml:
----- snip ------
- filename: {target_name}
md5: {md5_hex}
guid: {md5_hex}
title: !! TODO - ADD A TITLE - TODO !!
pubDate: "{pubDate}"
duration: "{duration}"
image_url: {image_url}
fileSize: {size}
explicit: !! TODO - EITHER 'clean' OR 'yes' - TODO !!
----- snip ------
""".format(**{
"target_name": target_name,
"md5_hex": md5.hexdigest(),
"pubDate": date.today().isoformat(),
"duration": seconds2duration(mp3.info.length),
"image_url": image_url,
"size": mp3_file_size,
})
sys.exit(main())