Skip to content

Commit

Permalink
feat: dynamic ad build
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Apr 2, 2018
1 parent 8ce5537 commit d70e166
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env*
/static/index.json
6 changes: 6 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fs = require('fs');
const ad = require('./codefund');

ad().then(json =>
fs.writeFileSync(__dirname + '/static/index.json', JSON.stringify(json))
);
30 changes: 30 additions & 0 deletions buysellads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fetch = require('node-fetch');

module.exports = buysellads;

async function buySellAds(ip) {
const adURL = 'https://srv.buysellads.com/ads/CVADL2JJ.json';

if (ip === '::1') {
ip = '86.13.179.215';
}

const request = await fetch(`${adURL}?forwardedip=${ip}&ignore=yes`);
const data = await request.json();

const ads = data.ads.filter(ad => ad.active);
const ad = ads[(ads.length * Math.random()) | 0];

const pixelsUrls = (ad.pixel || '').split('||');
const time = Math.round(Date.now() / 10000) | 0;
const pixels = pixelsUrls.map(pixel => pixel.replace('[timestamp]', time));

const { title, description, statlink: url } = ad;

return {
url,
description,
title,
pixels,
};
}
19 changes: 19 additions & 0 deletions codefund.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fetch = require('node-fetch');

module.exports = codefund;

async function codefund() {
const request = await fetch(
'https://codefund.io/t/s/22e88dc3-30f2-451b-a14e-d257c1395ef1/details.json'
);
const data = await request.json();

const { title, description, link: url, pixel } = data;

return {
title,
description,
url,
pixels: [pixel],
};
}
52 changes: 15 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
const fetch = require('node-fetch');
const cors = require('micro-cors')();
const sponsor = require('./static/index.json');

async function buySellAds(ip) {
const adURL = 'https://srv.buysellads.com/ads/CVADL2JJ.json';

if (ip === '::1') {
ip = '86.13.179.215';
}

const request = await fetch(`${adURL}?forwardedip=${ip}&ignore=yes`);
const data = await request.json();

console.log(`${adURL}?forwardedip=${ip}&ignore=yes`);

const ads = data.ads.filter(ad => ad.active);
const ad = ads[(ads.length * Math.random()) | 0];

const pixelsUrls = (ad.pixel || '').split('||');
const time = Math.round(Date.now() / 10000) | 0;
const pixels = pixelsUrls.map(pixel => pixel.replace('[timestamp]', time));

const { title, description, statlink: url } = ad;

return {
url,
description,
title,
pixels,
};
}

async function codesponsor() {
return sponsor;
}

// const sponsor = require('./static/index.json');

const codefund = require('./codefund');

/**
* Exports expects:
*
* {
* url: String, // click through
* description: String // displayed subtext
* title: String, // link text
* pixels: String[] // tracking images
* }
*/
module.exports = cors(async req => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
return await codesponsor(ip);
return await codefund(ip);
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dotenv": true
},
"scripts": {
"start": "micro"
"start": "micro",
"build": "node build.js"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit d70e166

Please sign in to comment.