-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.js
138 lines (124 loc) · 3.62 KB
/
instagram.js
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
var Instagram = (function(){
var _collection = [];
var preLoad = function(data){
for(var em in data){
for(var i=0,len=data[em].srclist.length;i<len;i++){
var src = data[em].bigSrclist[i];
var img = new Image();
img.src = src;
}
}
}
var render = function(data){
for(var em in data){
var liTmpl = "";
for(var i=0,len=data[em].srclist.length;i<len;i++){
liTmpl += '<li>\
<div class="img-box">\
<a class="img-bg" rel="example_group" href="'+data[em].bigSrclist[i]+'" title="'+data[em].text[i]+'"></a>\
<img lazy-src="'+data[em].srclist[i]+'" alt="">\
</div>\
</li>';
}
$('<section class="archives album"><h1 class="year">'+data[em].year+'<em>'+data[em].month+'月</em></h1>\
<ul class="img-box-ul">'+liTmpl+'</ul>\
</section>').appendTo($(".instagram"));
}
$(".instagram").lazyload();
changeSize();
setTimeout(function(){
preLoad(data);
},3000);
$("a[rel=example_group]").fancybox();
}
var replacer = function(str){
if(str.indexOf("outbound-distilleryimage") >= 0 ){
var cdnNum = str.match(/outbound-distilleryimage([\s\S]*?)\//)[1];
var arr = str.split("/");
return "http://distilleryimage"+cdnNum+".ak.instagram.com/"+arr[arr.length-1];
}else{
var url = "http://photos-g.ak.instagram.com/hphotos-ak-xpf1/";
var arr = str.split("/");
return url+arr[arr.length-1];
}
}
var ctrler = function(data){
var imgObj = {};
for(var i=0,len=data.length;i<len;i++){
var d = new Date(data[i].created_time*1000);
var y = d.getFullYear();
var m = d.getMonth()+1;
var src = replacer(data[i].images.low_resolution.url);
var bigSrc = replacer(data[i].images.standard_resolution.url);
var text = data[i].caption ? data[i].caption.text : ''; // data[i].caption 有可能为 null
var key = y+"-"+m;
if(imgObj[key]){
imgObj[key].srclist.push(src);
imgObj[key].bigSrclist.push(bigSrc);
imgObj[key].text.push(text);
}else{
imgObj[key] = {
year:y,
month:m,
srclist:[bigSrc],
bigSrclist:[bigSrc],
text:[text]
}
}
}
render(imgObj);
}
var getList = function(url){
$(".open-ins").html("图片来自instagram,正在加载中…");
$.ajax({
url: url,
type:"GET",
dataType:"jsonp",
success:function(re){
if(re.meta.code == 200){
_collection = _collection.concat(re.data);
var next = re.pagination.next_url;
if(next){
getList(next);
}else{
$(".open-ins").html("图片来自instagram,点此访问");
ctrler(_collection);
}
}else{
alert("access_token timeout!");
}
}
});
}
var changeSize = function(){
if($(document).width() <= 600){
$(".img-box").css({"width":"auto", "height":"auto"});
}else{
var width = $(".img-box-ul").width();
var size = Math.max(width*0.26, 157);
$(".img-box").width(size).height(size);
}
}
var bind = function(){
$(window).resize(function(){
changeSize();
});
}
return {
init:function(){
//getList("https://api.instagram.com/v1/users/438522285/media/recent/?access_token=438522285.2082eef.ead70f432f444a2e8b1b341617637bf6&count=100");
var insid = $(".instagram").attr("data-client-id");
var userId = $(".instagram").attr("data-user-id");
if(!insid){
alert("Didn't set your instagram client_id.\nPlease see the info on the console of your brower.");
console.log("Please open 'http://instagram.com/developer/clients/manage/' to get your client-id.");
return;
}
getList("https://api.instagram.com/v1/users/"+ userId +"/media/recent/?client_id="+insid+"&count=100");
bind();
}
}
})();
$(function(){
Instagram.init();
})