Last active 1670063512

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

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