Last active 1670063512

Python and PHP scripts for getting Now Playing from ocr.rainwave.cc. MIT license

Revision bfd1ddc154b1d14a66a0df5be24e16e1a143dace

OCRrainwaveNP.php Raw
1<?php
2# Because sometimes it's easier to load up a web browser.
3# http://blha303.com.au/util/ocr.rainwave.cc.php
4
5header("Access-Control-Allow-Origin: *");
6$pagecontent = file_get_contents("http://ocr.rainwave.cc/");
7preg_match_all("/PRELOADED_APIKEY = '(.*?)'/", $pagecontent, $matches);
8$url = "http://ocr.rainwave.cc/sync/2/init";
9$data = array('refresh' => 'full',
10 'user_id' => '1',
11 'key' => $matches[1][0],
12 'in_order' => 'true');
13
14$options = array(
15 'http' => array(
16 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
17 'method' => 'POST',
18 'content' => http_build_query($data),
19 ),
20);
21
22$context = stream_context_create($options);
23$result = json_decode(file_get_contents($url, false, $context), true);
24$songinfo = $result[3]["sched_current"]["song_data"][0];
25$artists = array();
26foreach($songinfo["artists"] as $item) {
27 $artists[] = $item["artist_name"];
28}
29$out = sprintf("%s - %s (from %s) %s",
30 implode(", ", $artists),
31 $songinfo["song_title"],
32 $songinfo["album_name"],
33 $songinfo["song_url"]);
34if(isset($_GET['callback'])){
35 header('Content-Type: text/javascript');
36 $callback = preg_replace('/\W+/', '', $_GET['callback']); #sanitize
37 print $callback . "(" . json_encode($out) . ");";
38} else {
39 header("Content-Type: text/plain");
40 print $out;
41}
42
OCRrainwaveNP.py Raw
1from urllib2 import urlopen
2from urllib import urlencode
3from json import loads
4from re import search
5
6
7def main():
8 apikey = search("PRELOADED_APIKEY = '(.*?)'", urlopen("http://ocr.rainwave.cc").read()).group(1)
9 data = loads(urlopen("http://ocr.rainwave.cc/sync/2/init",
10 data=urlencode({'refresh': 'full',
11 'user_id': '1',
12 'key': apikey,
13 'in_order': 'true'}
14 )).read())
15 songinfo = data[3]["sched_current"]["song_data"][0]
16 artists = []
17 for i in songinfo["artists"]:
18 artists.append(i["artist_name"])
19 print "%s by %s (from %s) %s" % (songinfo["song_title"],
20 ", ".join(artists),
21 songinfo["album_name"],
22 songinfo["song_url"])
23
24
25if __name__ == "__main__":
26 main()
27