-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathxvideos.js
129 lines (98 loc) · 3.47 KB
/
xvideos.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 cheerio = require("cheerio"),
http = require("http"),
qs = require("querystring"),
url = require("url");
var XVideos = module.exports;
XVideos.resolveId = function resolveId(id, cb) {
if (typeof id !== "number") {
return cb(Error("wrong type for id; expected number but got " + typeof id));
}
var req = http.get("http://www.xvideos.com/video" + id, function(res) {
if (res.statusCode === 404) {
return cb(Error("video not found"));
}
if (res.statusCode !== 301) {
return cb(Error("incorrect status code; expected 301 but got " + res.statusCode));
}
return cb(null, res.headers.location);
});
req.once("error", cb);
};
XVideos.details = function details(url, cb) {
var req = http.get(url, function(res) {
var body = Buffer(0);
res.on("readable", function() {
var chunk;
while (chunk = res.read()) {
body = Buffer.concat([body, chunk]);
}
});
if (res.statusCode === 404) {
return cb(Error("video not found"));
}
if (res.statusCode !== 200) {
return cb(Error("incorrect status code; expected 200 but got " + res.statusCode));
}
res.on("end", function() {
body = body.toString("utf8");
var $ = cheerio.load(body);
var title = $("#main > h2").text().replace(/-[^-]+$/, "").trim();
var tags = $("#video-tags > li > a").map(function(i, e) {
return $(e).text().trim();
}).filter(function(e) {
return e !== "tags";
});
var duration = $(".duration").text().trim().replace(/^-/, "").trim();
var flv;
if (matches = body.match(/flv_url=(http%3A%2F%2F.+?)&/)) {
flv = unescape(matches[1]);
}
if (!flv) {
return cb(Error("couldn't find flv"));
}
var thumb;
if (matches = body.match(/url_bigthumb=(http:\/\/.+?)&/)) {
thumb = unescape(matches[1]);
}
return cb(null, {title: title, duration: duration, tags: tags, flv: flv, thumb: thumb});
});
});
req.once("error", cb);
};
XVideos.constructSearchUrl = function constructSearchUrl(parameters) {
return "http://www.xvideos.com/?" + qs.stringify(parameters);
};
XVideos.search = function search(parameters, cb) {
var req = http.get(this.constructSearchUrl(parameters), function(res) {
var body = Buffer(0);
res.on("readable", function() {
var chunk;
while (chunk = res.read()) {
body = Buffer.concat([body, chunk]);
}
});
if (res.statusCode !== 200) {
return cb(Error("incorrect status code; expected 200 but got " + res.statusCode));
}
res.on("end", function() {
body = body.toString("utf8");
var $ = cheerio.load(body);
var videos = $(".thumbBlock > .thumbInside").map(function(i, e) {
var find;
if ($(e).find("script").length) {
find = cheerio.load($(e).find("script").text().replace(/^thumbcastDisplayRandomThumb\('(.+?)'\);$/, "$1"));
} else {
find = $(e).find.bind($(e));
}
return {
url: url.resolve("http://www.xvideos.com/", find("div.thumb > a").attr("href").replace("/THUMBNUM/", "/")),
title: find("p > a").text(),
duration: $(e).find("span.duration").text().replace(/[\(\)]/g, "").trim(),
};
});
var total = parseInt($("h3.blackTitle").text().replace(/[\r\n]/g, " ").replace(/^.*- (\d+) results.*$/, "$1"), 10);
return cb(null, {total: total, videos: videos});
});
});
req.once("error", cb);
};