-
Notifications
You must be signed in to change notification settings - Fork 0
/
dribbbleimage.js
249 lines (244 loc) · 6.54 KB
/
dribbbleimage.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var request = require('request'),
imagehost = 'http://dribbble.com',
mysqldb = require('./mysqldb.js'),
events = require('events');
/**
* 时间对象的格式化;
*/
Date.prototype.format = function(format) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"Y+" :this.getFullYear(),
"M+" :this.getMonth() + 1, // month
"d+" :this.getDate(), // day
"h+" :this.getHours(), // hour
"m+" :this.getMinutes(), // minute
"s+" :this.getSeconds(), // second
"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
"S" :this.getMilliseconds()
// millisecond
}
if (format === undefined){
format = "YYYY-MM-dd hh:mm:ss";
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
Array.prototype.unique2 = function () {
return this.sort().join(",,").replace(/(,|^)([^,]+)(,,\2)+(,|$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(",");
}
Object.prototype.Clone = function(){
var objClone;
if ( this.constructor == Object ) objClone = new this.constructor();
else objClone = new this.constructor(this.valueOf());
for ( var key in this )
{
if ( objClone[key] != this[key] )
{
if ( typeof(this[key]) == 'object' )
{
objClone[key] = this[key].Clone();
}
else
{
objClone[key] = this[key];
}
}
}
//objClone.toString = this.toString;
//objClone.valueOf = this.valueOf;
return objClone;
}
var getInfoBylinks = function(infopage, prelink){
var item = {
'title':'notitle',
'tags':'',
'date':new Date().format(),
'color':''
};
for (var i = 0; i < infopage.length; i++){
if (infopage[i].links.indexOf(prelink) != -1){
var info = infopage[i].info;
item['title'] = info[0].title;
item['tags'] = info[1].tags;
item['date'] = info[3].date.format();
item['color'] = info[2]['color'];
break;
}
}
return item;
}
var saveDataToDataBase = function(data){
var infopage = data[1];
var imgurls = data[2];
mysqldb.use();
for (var i = 0; i < imgurls.length; i++){
var prelink = imgurls[i].prelink;
var item = getInfoBylinks(infopage, prelink);
item['imgurl'] = imgurls[i].links.join(',');
mysqldb.insertLink(item['imgurl'], item['title'], item['date'], item['tags'], item['color']);
}
mysqldb.close();
}
var dribbbleRule = {
stopRule:'$(\'.next_page\').length == 0',
links:(function(maxuse){
var links = [];
var baseUrl = 'http://dribbble.com/shots/popular/';
links.push(baseUrl);
for (var i = 2; i<maxuse; i++){
links.push(baseUrl + '?page=' + i);
}
return links;
})(100),
info:[
{
key:'title',
keyselector:''
}
],
method:'serial',
selector:"$(\".extras img[alt=\'Attachments\']\").closest(\'.dribbble\').find(\'a.dribbble-over\')",
target:'href',
nextRule:{
selector:'$(\'.attachments ul li a\')',
target:'href',
method:'serial',
info:[
{
key:'title',
keyselector:'$(\'#screenshot-title\').text()'
},
{
key:'tags',
keyselector:'$(\'#tags li strong\').text(function(i,c){return c+\',\'}).text().replace(\',,\',\',\')'
},{
key:'color',
keyselector:'$(\'.color-chips li a\').text(function(i,c){return c+\',\'}).text().replace(\',,\',\',\')'
},{
key:'date',
keyselector:'new Date($(\'span.screenshot-dash\').text())'
}
],
nextRule:{
selector:'$(\'#viewer-img img\')',
target:'src',
method:'parallel',
save:true
}
}
}
var dribbbleFilter = function(url, rule, callb){
if (url == undefined) return;
var htmlfilter = function(url, rule){
var jsdom = require('jsdom');
request({ uri:url }, function (error, response, body) {
if (error) {
console.log('Error when connect to ' + url);
return;
}
console.log('Get response at ' + url);
jsdom.env({
html: body,
scripts: [
'jquery.js'
]
}, function (err, window) {
if (err){
console.log(err);
}
var $ = window.jQuery;
var queue = eval(rule.selector);
var info = [];
var links = [];
for (var i = 0; i< queue.length; i ++){
var link = $(queue[i]).attr(rule.target);
var args = url.match(/^(([a-z]+):\/\/)?([^\/\?#]+)\/*([^\?#]*)\??([^#]*)#?(\w*)$/i);
var parase = {
'schema': args[2],
'host': args[3],
'path': args[4],
'query': args[5],
'anchor': args[6]
};
if (link.indexOf('http://') < 0){
link = parase['schema'] + '://' + parase['host'] + link;
}
links.push(link);
}
if (rule.info != undefined){
for (i = 0; i < rule.info.length; i++){
var item = rule.info[i];
var temp = {}
if (temp['key'] == undefined){
temp[item['key']] = eval(item.keyselector);
}
info.push(temp);
}
}
info.push({links:links});
callb(url, links, info, eval(rule.stopRule));
console.log(eval(rule.stopRule));
});
});
}
htmlfilter(url, rule);
}
var data = [];
var dribbbleEngine = function(dribbbleRule){
if (dribbbleRule == undefined) return;
var urls = dribbbleRule.links;
var async = require('async');
var rule = {};
rule.selector = dribbbleRule.selector;
rule.target = dribbbleRule.target;
rule.info = dribbbleRule.info;
rule.stopRule = dribbbleRule.stopRule;
var automtask = function(url, rule){
var that = {};
that.url = url;
that.rule = rule.Clone();
return function(callback){
//console.log(that);
new dribbbleFilter(that.url, that.rule, function(url, links, info, go){
(function(url, links, info, go){
setTimeout(function(){
callback(go, {prelink:url, links:links, info:info});
}, Math.random()*500 + 500);
})(url,links, info, go);
});
};
};
var tasklist = [];
for (var i = 0; i < urls.length; i ++ ){
if (typeof(urls[i]) == 'string'){
tasklist.push(new automtask(urls[i], rule));
}
}
async.series(tasklist, function(err, results){
data.push(results);
var links = [];
for (var i = 0; i < results.length; i++){
links = links.concat(results[i].links);
}
if (dribbbleRule.nextRule != undefined){
var nextRule = dribbbleRule.nextRule.Clone();
nextRule.links = links;
new dribbbleEngine(nextRule);
}else{
saveDataToDataBase(data);
}
});
}
dribbbleEngine(dribbbleRule);