Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active December 3, 2022 20:31
Show Gist options
  • Save blha303/7409191 to your computer and use it in GitHub Desktop.
Save blha303/7409191 to your computer and use it in GitHub Desktop.
Python and PHP scripts for getting Now Playing from ocr.rainwave.cc. MIT license
<?php
# Because sometimes it's easier to load up a web browser.
# http://blha303.com.au/rainwave
# https://github.com/blha303/Rainwave-NowPlaying
header("Access-Control-Allow-Origin: *");
$pagecontent = file_get_contents("http://ocr.rainwave.cc/");
preg_match_all("/PRELOADED_APIKEY = '(.*?)'/", $pagecontent, $matches);
$url = "http://ocr.rainwave.cc/sync/2/init";
$data = array('refresh' => 'full',
'user_id' => '1',
'key' => $matches[1][0],
'in_order' => 'true');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context), true);
$songinfo = $result[3]["sched_current"]["song_data"][0];
$artists = array();
foreach($songinfo["artists"] as $item) {
$artists[] = $item["artist_name"];
}
$out = sprintf("%s - %s (from %s) %s",
implode(", ", $artists),
$songinfo["song_title"],
$songinfo["album_name"],
$songinfo["song_url"]);
if(isset($_GET['callback'])){
header('Content-Type: text/javascript');
$callback = preg_replace('/\W+/', '', $_GET['callback']); #sanitize
print $callback . "(" . json_encode($out) . ");";
} else {
header("Content-Type: text/plain");
print $out;
}
from urllib2 import urlopen
from urllib import urlencode
from json import loads
from re import search
def main():
apikey = search("PRELOADED_APIKEY = '(.*?)'", urlopen("http://ocr.rainwave.cc").read()).group(1)
data = loads(urlopen("http://ocr.rainwave.cc/sync/2/init",
data=urlencode({'refresh': 'full',
'user_id': '1',
'key': apikey,
'in_order': 'true'}
)).read())
songinfo = data[3]["sched_current"]["song_data"][0]
artists = []
for i in songinfo["artists"]:
artists.append(i["artist_name"])
print "%s by %s (from %s) %s" % (songinfo["song_title"],
", ".join(artists),
songinfo["album_name"],
songinfo["song_url"])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment