-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitunes.py
76 lines (64 loc) · 2.42 KB
/
itunes.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
"""
Custom Http handler
Author: Pascal Noisette
"""
import datetime
import hashlib
import urllib
import os
import PyRSS2Gen
from tinytag import TinyTag
class ItunesRSS2(PyRSS2Gen.RSS2):
def __init__(self, base_url, path, items):
"""
Override PyRSS2Gen.RSS2 constructor
- to simplify constructor args for user
- to have custom namespaces
"""
super().__init__(
title=path,
link=path,
lastBuildDate=datetime.datetime.now(),
items=items,
description="",
image=PyRSS2Gen.Image(base_url + "/feedIcon.png", path, path)
)
self.base_url=base_url
self.rss_attrs["xmlns:itunes"] = "http://www.itunes.com/dtds/podcast-1.0.dtd"
self.rss_attrs["xmlns:atom"] = "http://www.w3.org/2005/Atom"
self.rss_attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
self.rss_attrs["xmlns:dcterms"] = "http://purl.org/dc/terms/"
def publish(self, handler):
"""
Override PyRSS2Gen.RSS2.publish to add stylesheet to header
"""
handler.processingInstruction("xml-stylesheet" ,'type="text/xsl" href="'+self.base_url+'/style.xsl"');
handler.characters("\n");
super().publish(handler)
class ItunesRSSItem(PyRSS2Gen.RSSItem):
def __init__(self, base_url, rel_file, full_name):
"""
Override PyRSS2Gen.RSSItem constructor to simplify constructor args for user
"""
tag = TinyTag.get(rel_file)
if tag.title is None:
tag.title, ext = os.path.splitext(os.path.basename(rel_file))
super().__init__(
title=tag.title,
link=urllib.parse.quote(rel_file),
author=tag.artist,
enclosure=PyRSS2Gen.Enclosure(base_url + "/" + urllib.parse.quote(rel_file), tag.filesize, "audio/mpeg"),
guid=PyRSS2Gen.Guid(hashlib.sha256(full_name.encode('utf-8')).hexdigest(), isPermaLink=False),
pubDate=tag.year
)
def publish_extensions(self, handler):
"""
Override to set values of various itunes xmlns tags
"""
if self.enclosure.url.endswith(".mp3"):
PyRSS2Gen._element(handler, "image", PyRSS2Gen.Image(self.enclosure.url + ".png", self.title, self.link))
def is_valid_itunes_rss_item(rel_file):
"""
Hide TinyTag from user, supply him a predicate
"""
return TinyTag.is_supported(rel_file)