Skip to content

Commit

Permalink
Merge pull request #4 from mauricesvay/feature/api-v3
Browse files Browse the repository at this point in the history
Feature/api v3
  • Loading branch information
mauricesvay authored Oct 18, 2016
2 parents 4ee363f + 81d5ca6 commit b56d640
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
$profile = json_decode(file_get_contents($PROFILE_PATH), true);

if (!is_file($MODELS_PATH)) {
importModels($profile['uid'], $MODELS_PATH);
importModels($profile['username'], $MODELS_PATH);
}
$models = json_decode(file_get_contents($MODELS_PATH), true);

Expand Down
10 changes: 9 additions & 1 deletion app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ $(document).ready(function(){
}
}

$('button[data-action]').on('click', function(){
$('button[data-action="forceUpdate"]').on('click', function(){
var $this = $(this);
var attribute = $this.attr('data-action').replace('sort', 'data');
$('button[data-action]').removeClass('active');
$this.addClass('active');
tinysort('ul.skfb-grid>li', { sortFunction: getSortFunction(attribute)});
});

// Check for update
$.get('https://api.github.com/repos/mauricesvay/sketchfab-portfolio/releases/latest').then(function(response){
var currentVersion = $('#current-version').text();
if (response.name !== currentVersion) {
$('#current-version').html('<b>New version available: ' + response.name + '</b>');
}
});
});
33 changes: 23 additions & 10 deletions app/lib/api/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
/**
* Fetch paginated models for given user id
*/
function fetchModels($userId, $offset=0) {
$baseUrl = 'https://api.sketchfab.com/v2/models';
function fetchModels($username, $cursor='') {
$baseUrl = 'https://api.sketchfab.com/v3/models';
$params = array(
'user' => $userId,
'offset' => $offset
'user' => $username,
'sort_by' => '-createdAt',
'cursor' => $cursor
);
$url = $baseUrl . '?' . http_build_query($params);
return fetchJson($url);
Expand All @@ -16,14 +17,26 @@ function fetchModels($userId, $offset=0) {
/**
* Fetch all models for given user id
*/
function fetchAllModels($userId) {
$offset = 0;
function fetchAllModels($username) {
$cursor = '';
$models = array();

do {
$response = fetchModels($userId, $offset);
$response = fetchModels($username, $cursor);
$params = array();

// Extract cursor
if ($response['next'] !== null) {
$parsed = parse_url($response['next'], PHP_URL_QUERY);
if($parsed !== null) {
parse_str($parsed, $params);
if (array_key_exists ('cursor', $params)) {
$cursor = $params['cursor'];
}
}
}

$models = array_merge($models, $response['results']);
$offset = $offset + $response['count'];
} while ($response['next']);

return $models;
Expand All @@ -32,8 +45,8 @@ function fetchAllModels($userId) {
/**
* Save all user models to file
*/
function importModels($uid, $filePath) {
$models = fetchAllModels($uid);
function importModels($username, $filePath) {
$models = fetchAllModels($username);
makeDirForPath($filePath);
return file_put_contents($filePath, json_encode($models));
}
4 changes: 3 additions & 1 deletion app/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
-
{{ "now"|date('Y', timezone="UTC") }}
<br>
powered by <a href="https://github.com/mauricesvay/sketchfab-portfolio">Sketchfab Portfolio</a>
powered by
<a href="https://github.com/mauricesvay/sketchfab-portfolio">Sketchfab Portfolio</a>
(<a href="https://github.com/mauricesvay/sketchfab-portfolio/releases" id="current-version">1.1.0</a>)
and <a href="https://sketchfab.com/">Sketchfab</a>
<br>
<button data-action="forceUpdate" class="bt-linkified">Update portfolio</button>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<ul class="skfb-grid">
{% for model in models %}
<li class="skfb-grid-cell model" data-date="{{ model.publishedAt }}" data-views="{{ model.viewCount }}" data-likes="{{ model.likeCount }}">
<a href="{{ model.viewerUrl }}" data-type="iframe" data-href="{{ model.embedUrl }}" data-gall="models" title="{{ model.name | e }}" class="ratio-16-9" style="background-image: url({{ model.thumbnails.images | imageUrlSize(640) }})">
<a href="https://sketchfab.com/models/{{ model.uid }}" data-type="iframe" data-href="{{ model.embedUrl }}" data-gall="models" title="{{ model.name | e }}" class="ratio-16-9" style="background-image: url({{ model.thumbnails.images | imageUrlSize(640) }})">
<div class="ratio-inner">
<img src="{{ model.thumbnails.images | imageUrlSize(640) }}" alt="{{ model.name | e }}">
</div>
Expand Down

0 comments on commit b56d640

Please sign in to comment.