Last active
January 26, 2017 23:53
-
-
Save tdmalone/25dd4f70f19a747d656b34294389399f to your computer and use it in GitHub Desktop.
Cron this every minute or two on a server with PHP installed, to post every new hottest 100 track to a Slack channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ********************* START CONFIGURATION ********************* | |
// Set up an incoming webhook at https://YOURDOMAIN.slack.com/apps/manage/custom-integrations | |
define( 'SLACK_INCOMING_WEBHOOK', 'https://hooks.slack.com/services/TXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX' ); | |
// Set your data folder, which is used to prevent multiple notifications for the same song :) | |
define( 'DATA_FOLDER', './hottest100-data' ); | |
// ********************* END CONFIGURATION ********************* | |
// Ensure data folder exists | |
if ( ! is_dir( DATA_FOLDER ) ) { | |
mkdir( DATA_FOLDER, 0777, true ); | |
} | |
$xml = simplexml_load_file( 'http://www.abc.net.au/triplej/feeds/playout/triplej_sydney_playout.xml' ); | |
foreach( $xml->items->item as $song ) { | |
if ( ! $song->playedtime || ! $song->title ) { | |
return; | |
} | |
$filename = DATA_FOLDER . '/' . base64_encode( $song->playedtime ) . '.played'; | |
if ( ! file_exists( $filename ) ) { | |
touch( $filename ); | |
$payload = array ( | |
'attachments' => array ( | |
array ( | |
'title' => $song->tracknote . ': ' . $song->title . ' - ' . $song->artist->artistname, | |
'text' => $song->album->albumname . "\n" . $song->duration, | |
'thumb_url' => 'http://www.abc.net.au/triplej/albums/' . $song->album->albumid . '/covers/100.jpg', | |
), | |
), | |
); | |
$params = 'payload=' . urlencode( json_encode( $payload ) ); | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, SLACK_INCOMING_WEBHOOK ); | |
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); | |
print_r( curl_exec( $ch ) ); | |
curl_close( $ch ); | |
} | |
} | |
// The end! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment