forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstagram.html
160 lines (105 loc) · 3.13 KB
/
instagram.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<link rel="stylesheet" href="/adorn/adorn.css"/>
<script src="/adorn/adorn.js" async></script>
<script src="client_ids.js"></script>
<script src="../src/hello.polyfill.js"></script>
<script src="../src/hello.js"></script>
<script src="../src/modules/instagram.js"></script>
<title>hello( instagram )</title>
<h1>hello( instagram )</h1>
<button id='profile' onclick="getPhotos()">Get instagram photos</button>
<div id="result"></div>
<button id="more" style="display:none;">Load more</button>
<p>The button above executes <code>getPhotos()</code></p>
<script class="pre">
function getPhotos(){
// Define an instagram instance
var instagram = hello( 'instagram' );
// Trigger login to instagram
instagram.login().then( function(){
// Get Profile
instagram.api('me').then( profileHandler, errorHandler);
// Get user photos
instagram.api('me/photos').then( photosHandler, errorHandler );
},errorHandler);
}
function profileHandler( r ){
var profile = document.getElementById( 'profile' );
profile.innerHTML = "<img src='"+ r.thumbnail + "' width=24/>Connected to instagram as " + r.name;
}
function photosHandler( r ){
for(var i=0;i<r.data.length;i++){
var pic = r.data[i];
var img = document.createElement('img');
img.title = pic.name;
img.src = pic.thumbnail;
document.getElementById( 'result' ).appendChild( img );
}
var next = r.paging && r.paging.next;
more.style.display = next ? 'block' : 'none';
more.onclick = function(){
hello( 'instagram' ).api( next ).then( photosHandler, errorHandler );
};
}
</script>
<p>Set up your client id</p>
<script class="pre">
hello.init({
instagram : INSTAGRAM_CLIENT_ID
},{
scope : 'photos',
redirect_uri:'../redirect.html'
});
</script>
<h2>Like</h2>
<button onclick="getPopular()">Get popular photos</button>
<p>Once photos are loaded, click the photo to 'like'</p>
<div id="popular"></div>
<style>
.liked{
border:5px solid lime;
}
</style>
<script class="pre">
function getPopular(){
var instagram = hello('instagram');
instagram.login({scope:'publish'}).then(function(){
// Custom instagram endpoint
return instagram.api('media/popular');
})
.then(popularMediaHandler, errorHandler)
}
function popularMediaHandler( r ){
var popularContainer = document.getElementById( 'popular' );
r.data.forEach(function(pic){
var img = document.createElement('img');
img.title = pic.caption.text;
img.src = pic.images.thumbnail.url;
img.className = ( pic.user_has_liked ? 'liked' : '' );
img.onclick = likePic.bind( pic, img );
popularContainer.appendChild( img );
});
}
function likePic(img){
var pic = this;
var method = !pic.user_has_liked ? 'post' : 'delete';
hello( 'instagram' ).api( 'me/like', method, {
id: pic.id
})
.then(function(r) {
if (r.meta.code === 200) {
pic.user_has_liked = method === 'post';
img.className = ( pic.user_has_liked ? 'liked' : '' );
}
}, console.error.bind(console));
};
</script>
<script>
function errorHandler(e){
log("Failed making API request " + e.error.message );
}
function log(str){
console.log(str);
document.getElementById('result').appendChild(document.createTextNode(str));
}
</script>