-
Notifications
You must be signed in to change notification settings - Fork 0
/
culturepub.js
190 lines (166 loc) · 6.61 KB
/
culturepub.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
"use strict";
const config_file_path = 'config.json';
const Promise = require('bluebird');
const rp = require('request-promise');
const changeCase = require('change-case');
const moment = require('moment-timezone');
const debug = require('debug')('culturePub');
const removeDiacritics = require('diacritics').remove;
const configReader = require('./config_reader.js').create(config_file_path);
const culturePub = () => {
const FIELD_NAME = {
brand: 'Marque',
country: 'Pays',
year: 'Année',
agency: 'Agence',
role: 'Rôle',
product: 'Produit'
};
const randomId = function randomId() {
// From poking around, the lowest id is 100 and goes up to 5 figures. The
// max is around 24,000 at the moment but I assume it will go all the way
// to 99,999 eventually
const min = 60;
const max = 99999; // 99,999
const id = Math.floor(Math.random() * (max - min + 1)) + min;
debug(`Random id: ${id}`);
return id;
};
const getAdMetadata = function getAdMetadata(id) {
const uri = `http://api.cbnews.webtv.flumotion.com/pods/${id}?extended=true`;
debug(`Fetching '${uri}'`);
return rp(uri)
.then(function parseToJson(ad_metadata) {
return JSON.parse(ad_metadata);
})
.then(function checkAdValidity(json_metadata) {
if (isAnAd(json_metadata)) {
return Promise.resolve(json_metadata);
}
debug(`Not a valid ad`);
return getAdMetadata(randomId());
})
.then(function checkScore(ad_metadata) {
const scoreAboveThreshold = scoreIsAboveThreshold(ad_metadata);
if (!scoreAboveThreshold) {
debug('Score below minimum');
return getAdMetadata(randomId());
}
return ad_metadata;
})
.catch(function handleError(err) {
if (err.statusCode === 404) {
debug("Ad doesn't exist");
return getAdMetadata(randomId());
}
throw new Error(err);
})
};
const extractValue = function extractValue(json_metadata, field) {
const field_regex = new RegExp(field);
let data_match = json_metadata.filter((item) => item.name.match(field_regex));
data_match = data_match[0] && data_match[0].value;
return data_match;
};
const extractFilename = function extractFilename(json_metadata) {
const field_name = 'OriginalFilename';
let filename_match = json_metadata.filter((item) => item.name === field_name);
filename_match = (filename_match[0] && filename_match[0].value);
filename_match = filename_match.match(/[\d_]+/);
return filename_match;
};
const propertyToSlug = function propertyToSlug(json_metadata, field) {
const property = extractValue(json_metadata, field);
let slug;
if (property) {
slug = removeDiacritics(property);
slug = changeCase.paramCase(slug);
} else {
slug = '';
}
return slug;
};
const buildImageUrl = function buildThumbnailUrl(json_metadata) {
const extra_fields = json_metadata.extra_fields;
const ad_id = json_metadata.id;
const ad_name = removeDiacritics(changeCase.paramCase(json_metadata.title));
const advertiser_name = propertyToSlug(extra_fields, FIELD_NAME.brand);
const product_name = propertyToSlug(extra_fields, FIELD_NAME.product);
const publish_date = moment(json_metadata.publishDate);
let publish_year;
let publish_month;
// It seems some entries' thumbnails were all added at the same time, in
// October 2014. The later ones get their actual publishing dates.
if (publish_date.isSameOrBefore('2014-10-31')) {
publish_year = '2014';
publish_month = '10';
} else {
publish_year = publish_date.format('YYYY');
publish_month = publish_date.format('MM');
}
let image_url;
if (product_name) {
image_url = `http://static.culturepub.fr/assets/${publish_year}/${publish_month}/poster-${ad_id}-${advertiser_name}-${product_name}-${ad_name}-236x132.jpg`;
} else {
image_url = `http://static.culturepub.fr/assets/${publish_year}/${publish_month}/poster-${ad_id}-${advertiser_name}-${ad_name}-236x132.jpg`;
}
debug(`image_url: '${image_url}'`)
return image_url;
};
const isAnAd = function isAnAd(json_metadata) {
// It seems every ad has a brand property, other videos don't; this is how
// we can tell whether it's an ad
const extra_fields = json_metadata.extra_fields;
const advertiser_name = extractValue(extra_fields, FIELD_NAME.brand);
return !!advertiser_name;
};
const scoreIsAboveThreshold = function scoreIsAboveThreshold(json_metadata) {
const votes_count = json_metadata.total_rates;
// Some ads are good but come up with a rating of 0 because they have no
// votes.
const allow_voteless = true;
const score = parseFloat(json_metadata.average_rate, 10);
const min_score = parseInt(configReader.get('culturepub:min_score'), 10);
debug(`Ad score is ${score}, at least ${min_score} is required`);
if (allow_voteless) {
debug(`Low score threshold overriden by lack of votes`)
}
return (min_score <= score) || (votes_count === 0 && allow_voteless);
};
return {
getAd: Promise.method(function getAd() {
return getAdMetadata(randomId())
.then(function pickMetadata(json_metadata) {
const extra_fields = json_metadata.extra_fields;
const advertiser_name = extractValue(extra_fields, FIELD_NAME.brand);
const ad_country = extractValue(extra_fields, FIELD_NAME.country);
const ad_year = extractValue(extra_fields, FIELD_NAME.year);
const ad_agency = extractValue(extra_fields, FIELD_NAME.agency);
const ad_director = changeCase.titleCase(extractValue(extra_fields, FIELD_NAME.role));
const ad_title = json_metadata.title;
const video_filename = extractFilename(extra_fields);
const video_url = `http://wpc.cf8d.edgecastcdn.net/80CF8D/cbnews/video/mp4/hd/${video_filename}.mp4`;
const image_url = buildImageUrl(json_metadata);
const formatted_metadata = {
video_url: video_url,
image_url: image_url,
advertiser_name: advertiser_name,
ad_country: ad_country,
ad_year: ad_year,
ad_agency: ad_agency,
ad_director: ad_director,
ad_title: ad_title
};
debug(`formatted_metadata: ${JSON.stringify(formatted_metadata, null, 4)}`);
debug(`Got ad: '${ad_title}'`);
return formatted_metadata;
})
.catch(function handleError(err) {
throw new Error(err);
});
})
};
};
module.exports = {
create: culturePub
};