forked from tmcw-up-for-adoption/yell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yell2
executable file
·67 lines (55 loc) · 2.17 KB
/
yell2
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
#!/usr/bin/env node
var cheerio = require('cheerio'),
fs = require('fs'),
path = require('path'),
_ = require('underscore'),
get = require('get');
var userid = process.argv[2],
itr = 0,
reviews = [];
if (!userid) throw "Please specify a Yelp userid as the only argument to this " +
"utility. Find it by going to your profile page and looking int he URL.";
function yurl(start) {
console.log('fetching articles starting with ' + start);
return 'http://www.yelp.com/user_details_reviews_self?userid=' +
userid + '&rec_pagestart=' + start;
}
function done(reviews) {
fs.writeFileSync('yelp.json', JSON.stringify(reviews), 'utf8');
draw(reviews);
drawxml(reviews);
console.log('\nFinished! Find in this directory:\n- yelp.html, a hreview-formatted HTML version\n- yelp.json: raw JSON data.\n- yelp.xml: for transfering your content to your blog');
}
function draw(r) {
fs.writeFileSync('yelp.html', _.template(fs.readFileSync(path.join(__dirname, 'page._'), 'utf8'))({
reviews: r
}), 'utf8');
}
function drawxml(r) {
fs.writeFileSync('yelp.xml', _.template(fs.readFileSync(path.join(__dirname, 'xml._'), 'utf8'))({
reviews: r
}), 'utf8');
}
function load(n) {
get({ uri: yurl(n) }).asString(function(err, str) {
if (err) throw err;
var $ = cheerio.load(str);
$('div.review').each(function(i, elem) {
reviews.push({
text: $(this).children('div.review-content p').html(),
date: $(this).children('span.rating-qualifier').text(),
name: $(this).children('div.media-title a').text(),
stars: parseInt($(this).children('i.star-img img').attr('alt'), 10),
featuredimage: $(this).children('div.review-content ul li div.photo-box.photobox--interactive img').attr('src'),
//id: $(this).children('div.review').attr('data-review-id')
//id: $(this).children('h4 a').attr('href').match(/\/list\/([\d\w\-]*)/)[1]
});
});
if ($('#pager_page_next').length) {
load(n + 10);
} else {
done(reviews);
}
});
}
load(0);