-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
130 lines (108 loc) · 3.24 KB
/
app.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
/*
*/
var Q = require('q'),
fs = require('fs'),
handlebars = require('handlebars'),
request = require('request'),
utils = require('./utils'),
config = require('./config.json'),
videos = [],
appQ, channel, nextPageToken, noMoreItems,
channelsListURL = utils.ytapiURL('channels', {
part:'contentDetails',
forUsername:config.forUsername,
key:config.key
});
appQ = Q(ytapiRequest(channelsListURL))
.then(function (channels) {
channel = channels.items[0].contentDetails.relatedPlaylists.uploads;
return loopPlaylistItemsCall();
})
.then(function() {
//console.log(videos[0]);
return Q.nfcall(fs.readFile, 'feed_template.xml', 'utf-8');
})
.then(function(feedsrc) {
var feed = handlebars.compile(feedsrc);
var data = {videos: videos};
return feed(data);
})
.then(function(fd) {
//console.log(fd);
return postFeed('http://posttestserver.com/post.php?dir=myfeedexample','test','test',new Buffer('test','utf-8'));
})
.then(function(postresp) {
console.log(postresp);
})
appQ.done(function() {
console.log('done!');
})
//appQ.done(function() {});
appQ.fail(function (err) {
console.log(err);
});
function postFeed(url,datasource,feedtype,data) {
return Q.Promise(function(resolve, reject) {
var post = request.post(url, function(err, response, body) {
if (err) reject(err);
resolve(body);
});
var form = post.form();
form.append('datasource', datasource);
form.append('feedtype', feedtype);
form.append('data', data);
});
}
function ytapiRequest(url) {
return Q.Promise(function(resolve, reject) {
request(url, function(err, response, body) {
if (err) reject(err);
if (response.statusCode!=200) reject(new Error('Unsuccessful response code!'));
resolve(JSON.parse(body));
});
});
}
function parsePlaylistItems() {
var deferred = Q.defer(),
playlistItemsParams = {
part:'snippet',
maxResults: 50,
playlistId:channel,
key:config.key
};
if (nextPageToken) playlistItemsParams.pageToken = nextPageToken;
var playlistItemsURL = utils.ytapiURL('playlistItems', playlistItemsParams);
ytapiRequest(playlistItemsURL).then(function (playlistItems) {
nextPageToken = (playlistItems.nextPageToken) ? playlistItems.nextPageToken : '';
if (!Boolean(nextPageToken)) noMoreItems = true;
return addPlaylistItemVideos(playlistItems.items);
}).then(function() {
deferred.resolve();
});
return deferred.promise;
}
var addPlaylistItemVideos = function (playlistItems) {
var deferred = Q.defer();
for (var v = 0, maxV = playlistItems.length; v < maxV; v++) {
videos.push({
id: playlistItems[v].snippet.resourceId.videoId,
title: playlistItems[v].snippet.title,
description: playlistItems[v].snippet.description,
bite: playlistItems[v].snippet.description.replace(/\s+/g, ' '),
thumbnails: {
default: playlistItems[v].snippet.thumbnails.default
}
});
}
deferred.resolve();
return deferred.promise;
};
function loopPlaylistItemsCall() {
var deferred = Q.defer();
function loop() {
if (noMoreItems) return deferred.resolve();
Q.when(parsePlaylistItems(), loop, deferred.reject);
}
Q.nextTick(loop);
return deferred.promise;
}