Skip to content

Instantly share code, notes, and snippets.

@lkLeonov
Last active August 11, 2016 12:04
Show Gist options
  • Save lkLeonov/477f86e9ccb50e82953112ee78fa89b0 to your computer and use it in GitHub Desktop.
Save lkLeonov/477f86e9ccb50e82953112ee78fa89b0 to your computer and use it in GitHub Desktop.
'use strict'
const http = require('http');
const querystring = require('querystring');
const VK = {
'groupId': {
name: 'groups.getById',
group_ids: 'boroda.land'
}
};
const vkApiUrl = 'http://api.vk.com/method/' + VK.groupId.name + '?group_id=' + VK.groupId.group_ids;
http.createServer((servReq, servRes) => {
http.get(vkApiUrl, (res) => {
console.log('SERVER::GET: ' + vkApiUrl, res.statusCode);
let body = '';
res.on('data', (d) => { body += d; });
res.on('end', () => {
/// GROUP ID:
var GID;
const parsedResponse = JSON.parse(body).response;
if (parsedResponse && parsedResponse[0] && parsedResponse[0].gid) {
GID = -JSON.parse(body).response[0].gid;
}
console.log('GID: ' + GID);
// Ogiginal POST query encoded:
// const xhrBody = '_smt=market%3A6&al=1&id=-57616626&load=1&offset=42&price_from=&price_to=&q=&sort=0';
const postData = querystring.stringify({
_smt: 'market:6',
al: 1,
id: GID,
load: 1,
offset: 0,
sort: 0
});
const postReqOptions = {
hostname: 'vk.com',
path: '/al_market.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
"origin":"http://vk.com",
"referer": vkApiUrl
}
};
const postReq = http.request(postReqOptions, (res) => {
console.log('SERVER::POST: ' + postReqOptions.hostname + postReqOptions.path, res.statusCode);
let vkData = '';
res.on('data', (chunk) => {
vkData += chunk;
});
res.on('end', () => {
// parsing images from raw vk response
const imgs = reFirstGroup(/src=(.*?\.jpg)/g, vkData);
// making html
let html = '';
imgs.forEach((img) => {
html += '<img src=' + img + '">';
});
// form server response with html
servRes.writeHeader(200, {"Content-Type": "text/html"});
servRes.end(html)
});
});
postReq.write(postData);
postReq.end();
});
});
}).listen(80);
// helpers
// first matched group in regex
function reFirstGroup (re, str) {
var match = re.exec(str);
var arr = [];
if (match && match[1]) arr.push(match[1])
while (match != null) {
match = re.exec(str);
if (match && match[1]) arr.push(match[1])
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment