-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-mp3.py
176 lines (131 loc) · 4.7 KB
/
create-mp3.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from eyed3 import id3
from wordpress_xmlrpc import WordPressPost
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts, media
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import taxonomies
import yaml
import os
import glob
import youtube_dl
import sys
import time
# To Fix : URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:841)> in Python
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
os.system("clear")
print("Removing existing mp3 files...")
oldFiles = glob.glob("*.mp3")
for file in oldFiles:
try:
os.remove(file)
except OSError:
pass
print("Processing the config file...")
audio_info = yaml.load(open('mp3Info.yaml'))
local_file = audio_info['local_file_name']
url = audio_info['youtube_video_url']
if url:
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
'outtmpl':'%(title)s-%(id)s.%(ext)s',
'nocheckcertificate':True
}
print("Downloading YouTube File...")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
mp3FileName = ydl.prepare_filename(info).replace(info['ext'], 'mp3')
print("Downloading Completed...")
else:
mp3FileName = audio_info['local_file_name']
audioTitleInEnglish = audio_info['audio_title_in_english']
title = audio_info['title']
if audio_info['audio_artist']:
audioArtist = u""+audio_info['audio_artist']
else:
audioArtist = u"கணியம்"
if audio_info['audio_album']:
audioAlbum = u""+audio_info['audio_album']
else:
audioAlbum = u"கணியம்"
if audio_info['audio_genre']:
audioGenre = audio_info['audio_genre']
else:
audioGenre = "Podcast"
if audio_info['audio_source_url']:
audioSourceUrl = audio_info['audio_source_url']
else:
audioSourceUrl = " "
if audio_info['audio_publisher_url']:
audioPublisherUrl = audio_info['audio_publisher_url']
else:
audioPublisherUrl = "http://www.kaniyam.com"
if audio_info['audio_license']:
audioLicense = audio_info['audio_license']
else:
audioLicense = "https://creativecommons.org/licenses/by/4.0/"
if audio_info['audio_comments']:
audioComments = audio_info['audio_comments']
else:
audioComments = ""
if audio_info['audio_language']:
audioLang = audio_info['audio_language']
else:
audioLang = ""
print("Updating MetaData...")
tag = id3.Tag()
tag.parse(os.path.abspath(mp3FileName))
tag.artist = u""+audioArtist
tag.album = u""+audioAlbum
tag.non_std_genre = u""+audioGenre
tag.title = u""+audioTitleInEnglish
tag.artist_url = str(audioSourceUrl).encode("utf-8")#;b""+bytes(audioSourceUrl,'utf-8')
tag.audio_source_url = str(audioSourceUrl).encode("utf-8")#b""+bytes(audioSourceUrl,'utf-8')
tag.publisher_url = str(audioPublisherUrl).encode("utf-8")#b""+bytes(audioPublisherUrl,'utf-8')
tag.copyright_url = str(audioLicense).encode("utf-8")#b""+bytes(audioLicense,'utf-8')
tag.comments.set(u""+audioComments, description=u"")
# read image into memory
imagedata = open(audio_info['audio_art_name'], "rb").read()
# append image to tags
tag.images.set(3, imagedata, "image/jpeg", u"Cover")
tag.save()
print("MetaData updated successfully.....")
timestamp = time.strftime('%Y-%m-%d-%H-%M-%S')
audioTitleInEnglish = audio_info['audio_title_in_english']
ia_identifier = audioTitleInEnglish + "-" + timestamp
os.rename(os.path.abspath(mp3FileName), audioTitleInEnglish + ".mp3")
ia_upload = "ia upload " + ia_identifier + \
" -m collection:opensource -m mediatype:audio -m sponsor:Kaniyam -m language:ta " + \
audioTitleInEnglish + ".mp3"
print("Uploading to Internet Archive")
os.system(ia_upload)
audioURL = "https://archive.org/download/%s/%s" % (ia_identifier, audioTitleInEnglish + ".mp3");
print("Uploaded to " + audioURL)
print("Posting into WordPress")
wp_username = audio_info['wp_username']
wp_password = audio_info['wp_password']
wpBlogUrl = audio_info['wp_blog_url']+'/xmlrpc.php'
client = Client(wpBlogUrl, wp_username, wp_password)
post = WordPressPost()
content = "%s \n %s"% (audioURL, audioComments)
post.title = title
post.content = content
post.post_status = 'publish'
post.comment_status = 'open'
post.terms_names = {'category': ['Podcast']}
post.slug = audioTitleInEnglish
if "kaniyam.com" in wpBlogUrl:
post.post_type = 'podcast'
post.terms_names = ""
print("Publishing to Kaniyam")
post.id = client.call(posts.NewPost(post))
print("Posted into WordPress")