This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiok.php
103 lines (93 loc) · 3.38 KB
/
radiok.php
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
<?php
/**
* Simple script to build a podcast feed out of Radio K's "Track of the Day"
* blog posts.
* Requires QueryPath (http://querypath.org)
*/
require_once('QueryPath/QueryPath.php');
define('RADIOK_URL', 'http://www.radiok.org');
/**
* URL of all blog posts tagged "track of the day"
*/
define('FEED_URL', 'http://www.radiok.org/blogs/new/tag/track%20of%20the%20day');
/**
* Array of podcast items, which are, in turn, arrays
*/
$podcast = array();
// Each item in the feed has a class of "blog_post", so that's handy
$qp = htmlqp(FEED_URL);
foreach ($qp->find('.blog_post') as $post) {
// Traverse the post to get all the pertinent info.
// QueryPath has an internal pointer that's always pointing to
// a particular place in the DOM. Every time you query something,
// it moves that pointer and all future queries are relative to that.
// Which means that any time I drill down, I have to back out again
// with the parent() method.
$pod = array();
$h3 = $post->children('h3');
if ($h3->size() == 0) {
continue;
}
$pod['title'] = $h3->text();
$pod['guid'] = $h3->children('a')->attr('href');
$h3->parent();
$post->parent();
$h2_list = $post->children('h2');
$song_text = $h2_list->last()->text();
$post->parent();
$p = $post->children('p');
foreach ($p as $kid) {
if ($kid->tag() == 'p') {
$pod['description'] = $kid->text();
break;
}
}
$post->parent();
$script = $post->children('script')->first()->text();
$ret = preg_match('/mp3: "(.*\.mp3)"/', $script, $matches);
if ($ret == 1) {
$pod['audio_url'] = RADIOK_URL.$matches[1];
}
$post->parent();
$pod['pub_date'] = '';
preg_match('/(\d+)-(\d+)-(\d+)/', $pod['title'], $dateMatches);
if (count($dateMatches) == 4) {
$pod['pub_date'] = date('r', mktime(0,0,0,$dateMatches[1], $dateMatches[2], $dateMatches['3']));
}
$pod['song_artist'] = '';
$pod['song_title'] = '';
preg_match('/^(.*) -.*"(.*)"/', $song_text, $songMatches);
if (count($songMatches) == 3) {
$pod['song_artist'] = $songMatches[1];
$pod['song_title'] = $songMatches[2];
}
$podcast[] = $pod;
}
$lastBuildDate = $podcast[0]['pub_date'];
?>
<?php print('<?xml version="1.0" encoding="UTF-8" ?>'); ?>
<?php print('<?xml-stylesheet type="text/xsl" href="/standard/xsl/mpr004/podcast.xsl"?>');?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Radio K Track of the Day</title>
<link><?php print(FEED_URL) ?></link>
<description>Radio K Song of the Day</description>
<language>en-us</language>
<lastBuildDate><?php print($lastBuildDate); ?></lastBuildDate>
<itunes:author>Radio K</itunes:author>
<?php
foreach ($podcast as $item) {
?>
<item>
<title><?php print(htmlspecialchars($item['song_title'])); ?></title>
<description><?php print(htmlspecialchars($item['description'])); ?></description>
<enclosure url="<?php print($item['audio_url']); ?>" length="3287042" type="audio/mpeg" />
<pubDate><?php print($item['pub_date']); ?></pubDate>
<guid isPermaLink="false"><?php print($item['guid']); ?></guid>
<itunes:author><?php print(htmlspecialchars($item['song_artist'])); ?></itunes:author>
</item>
<?php
}
?>
</channel>
</rss>