Created
April 1, 2017 01:28
-
-
Save GoodChancer/67344ebf62f7b47f7a82dfe8c257a5ba to your computer and use it in GitHub Desktop.
Putting weekly music data in database
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 | |
require_once($_SERVER['DOCUMENT_ROOT'].'/music/init.php'); | |
$albumCount = 0; | |
/* Get title and convert to date */ | |
$pageTitle = $parsehub['PageTitle']; | |
$timeString = str_replace("Week of ","",$pageTitle); | |
$week = date("Y-m-d", strtotime($timeString)); | |
/* Insert weekly data into db */ | |
$stmt = $pdo->prepare('INSERT INTO `weekly` (`week`, `title`, `start_url`, `run_token`, `start_value`) VALUES (:week, :pageTitle, :startURL, :token, :startValue) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(`id`), run_token=VALUES(`run_token`), start_value=VALUES(`start_value`)'); | |
$stmt->execute(['week'=>$week, 'pageTitle'=>$pageTitle, 'startURL'=>$startURL, 'token'=>$token, 'startValue'=>$startValue]); | |
$weeklyID = $pdo->lastInsertId(); | |
/* Loop through albums and insert into db */ | |
while($albumCount < count($parsehub['Albums'])) { | |
$albumCurrent = $parsehub['Albums'][$albumCount]; | |
$album = $albumCurrent['Album']; | |
$albumAllmusicURL = $albumCurrent['PageURL']; | |
$albumRelease = $albumCurrent['Release']; | |
$albumDuration = $albumCurrent['Duration']; | |
$genre = $albumCurrent['Genre']; | |
$artAllmusic = $albumCurrent['Art']; | |
$artist = $albumCurrent['Artist']; | |
$artistAllmusicURL = $albumCurrent['ArtistURL']; | |
/* Search iTunes for album */ | |
require_once($_SERVER['DOCUMENT_ROOT'].'/music/get-itunes.php'); | |
if ($itunes['resultCount'] > 0) { | |
$albumItunesURL = $itunes['results'][0]['collectionViewUrl']; | |
$artistItunesURL = $itunes['results'][0]['artistViewUrl']; | |
$albumItunesArt = $itunes['results'][0]['artworkUrl100']; | |
} else{ | |
$albumItunesURL = NULL; | |
$artistItunesURL = NULL; | |
$albumItunesArt = NULL; | |
} | |
/* Save fallback album art locally from Allmusic */ | |
include($_SERVER['DOCUMENT_ROOT'].'/music/crawler.php'); | |
/* Insert Artist */ | |
$stmt = $pdo->prepare('INSERT INTO `artist` (`name`, `allmusic_url`, `itunes_url`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(`id`)'); | |
$stmt->execute([$artist, $artistAllmusicURL, $artistItunesURL]); | |
$artistID = $pdo->lastInsertId(); | |
/* Insert Album */ | |
$stmt = $pdo->prepare('INSERT INTO `album` (`title`, `artist_id`, `release_date`, `duration`, `art_itunes`, `art_allmusic`, `art`, `weekly_id`, `streamable`, `itunes_url`, `allmusic_url`) VALUES (:album, :artistID, :albumRelease, :albumDuration, :albumItunesArt, :artAllmusic, :imgSecondary, :weeklyID, :streamable, :albumItunesURL, :albumAllmusicURL) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(`id`), art_itunes=VALUES(art_itunes), streamable=VALUES(streamable), itunes_url=VALUES(itunes_url)'); | |
$stmt->execute(['album'=>$album, 'artistID'=>$artistID, 'albumRelease'=>$albumRelease, 'albumDuration'=>$albumDuration, 'albumItunesArt'=>$albumItunesArt, 'artAllmusic'=>$artAllmusic, 'imgSecondary'=>$imgSecondary, 'weeklyID'=>$weeklyID, 'streamable'=>$streamable, 'albumItunesURL'=>$albumItunesURL, 'albumAllmusicURL'=>$albumAllmusicURL]); | |
$albumID = $pdo->lastInsertId(); | |
/* Insert Styles */ | |
if (is_array($albumCurrent['Styles']) || is_object($albumCurrent['Styles'])) | |
{ | |
foreach($albumCurrent['Styles'] as $style) { | |
$styleCurrent = $style['Style']; | |
$stmt = $pdo->prepare('SELECT id FROM style WHERE name = ?'); | |
$stmt->execute([$styleCurrent]); | |
$styleID = $stmt->fetchColumn(); | |
$stmt = $pdo->prepare('INSERT IGNORE INTO album_style (album_id, style_id) VALUES (?, ?)'); | |
$stmt->execute([$albumID, $styleID]); | |
} | |
} | |
/* Insert Tracks */ | |
if (is_array($albumCurrent['Tracks']) || is_object($albumCurrent['Tracks'])) | |
{ | |
foreach($albumCurrent['Tracks'] as $track) { | |
$title = $track['Title']; | |
$titleNum = $track['TrackNumber']; | |
$titleDur = $track['Duration']; | |
$stmt = $pdo->prepare('INSERT IGNORE INTO track (title, duration, number, album_id) values (?, ?, ?, ?)'); | |
$stmt->execute([$title, $titleDur, $titleNum, $albumID]); | |
} | |
} | |
$albumCount++; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment