Created
March 31, 2018 20:47
-
-
Save tschoffelen/000e3ec9f0cf592901ef0fbd13a3a6fd to your computer and use it in GitHub Desktop.
This file contains 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 | |
// Set username | |
$username = 'tschoffelen'; | |
// Get page contents | |
$content = file_get_contents('https://dribbble.com/' . $username . '?page=1&per_page=12'); | |
// Parse page content into $shots array | |
$shots = []; | |
$meta = []; | |
$matches = []; | |
preg_match_all('#<img alt="[^"]+" src="([^"]+)" />\s+</picture>\s+</a>\s+<a class="dribbble-over" href="([^"]+)">\s+<strong>([^<]+)</strong>#i', $content, $matches, PREG_SET_ORDER); | |
preg_match('#<script>\s+var newestShots = (\[[^;]+)#i', $content, $json); | |
$json = preg_replace('#\s(\w+):#i', '"$1":', $json[1]); | |
$json = preg_replace('#("published_at":)\s\'([^\']+)\'#i', '$1"$2"', $json); | |
$json = json_decode($json, true); | |
foreach ($json as $shot) { | |
$meta[$shot['path']] = $shot; | |
} | |
foreach ($matches as $match) { | |
$shot_meta = @$meta[$match[2]] ?: []; | |
$shots[] = array_merge($shot_meta, [ | |
'image' => $match[1], | |
'url' => 'https://dribbble.com' . $match[2], | |
'title' => $match[3] | |
]); | |
} | |
// Output shots array | |
echo json_encode($shots, JSON_PRETTY_PRINT); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment