Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* package.json
  2. {
  3.   "name": "znb",
  4.   "version": "1.0.0",
  5.   "main": "index.js",
  6.   "license": "MIT",
  7.   "dependencies": {
  8.     "lowdb": "^1.0.0",
  9.     "phantom": "^4.0.5",
  10.     "request": "^2.82.0",
  11.     "request-promise-native": "^1.0.4",
  12.     "shuffle-array": "^1.0.1",
  13.     "snoowrap": "^1.14.2"
  14.   }
  15. }
  16. */
  17.  
  18. const SUBREDDITS = [
  19.     'FractalPorn',
  20.     'cats',
  21.     'foxes',
  22. ];
  23. const ZN_PROFILE_URL = 'http://127.0.0.1:43110/Me.ZeroNetwork.bit/?Profile/' +
  24.     '12h51ug6CcntU2aiBjhP8Ns2e5VypbWWtv/1HseT5JgPe86zAKi1e9MD9sAVoACm1xeaD/dqhufbjdxj@zeroid.bit';
  25. const REDDIT_POLL_LIMIT = 20;
  26. const REDDIT_CREDENTIALS = {
  27.     username: '...',
  28.     password: '...',
  29.     clientId: '...',
  30.     clientSecret: '...',
  31.     userAgent: 'nodejs',
  32. };
  33. const IMGUR_CREDENTIALS = {
  34.     clientId: '...',
  35.     clientSecret: '...',
  36. };
  37. const IMGUR_HEADERS = {
  38.     'Authorization': `Client-ID ${IMGUR_CREDENTIALS.clientId}`,
  39. };
  40.  
  41. const fs = require('fs');
  42. const request = require('request');
  43. const rp = require('request-promise-native');
  44. const phantom = require('phantom');
  45. const snoowrap = require('snoowrap');
  46. const lowdb = require('lowdb');
  47. const FileSync = require('lowdb/adapters/FileSync');
  48. const shuffle = require('shuffle-array');
  49.  
  50. const adapter = new FileSync('db.json');
  51. const db = lowdb(adapter);
  52.  
  53. if (!db.get('imageQueue').value()) {
  54.     db.set('imageQueue', []).write();
  55. }
  56.  
  57. function iq(images) {
  58.     const imageQueue = db.get('imageQueue').value();
  59.     if (images) {
  60.         db.set('imageQueue',
  61.             shuffle(imageQueue.concat(images))
  62.         ).write();
  63.     } else {
  64.         const image = imageQueue.pop();
  65.         db.set('imageQueue', shuffle(imageQueue)).write();
  66.         return image;
  67.     }
  68. }
  69.  
  70. function sleep(ms) {
  71.     return new Promise(resolve =>
  72.         setTimeout(resolve, ms)
  73.     );
  74. }
  75.  
  76. async function getImgurAlbumImages(albumId) {
  77.     const response = await rp.get({
  78.         url: `https://api.imgur.com/3/album/${albumId}/images`,
  79.         headers: IMGUR_HEADERS,
  80.     });
  81.     const data = JSON.parse(response).data;
  82.     return data.link ||
  83.         data.map(entry => entry.link);
  84. }
  85.  
  86. async function getImgurGalleryImages(galleryId) {
  87.     const response = await rp.get({
  88.         url: `https://api.imgur.com/3/gallery/${galleryId}`,
  89.         headers: IMGUR_HEADERS,
  90.     });
  91.     const data = JSON.parse(response).data;
  92.     return data.images
  93.         ? data.images.map(entry => entry.link)
  94.         : data.link;
  95. }
  96.  
  97. async function getImgurImage(imageId) {
  98.     const response = await rp.get({
  99.         url: `https://api.imgur.com/3/image/${imageId}`,
  100.         headers: IMGUR_HEADERS,
  101.     });
  102.     return JSON.parse(response).data.link;
  103. }
  104.  
  105. function downloadFile(uri, path) {
  106.     return new Promise(resolve =>
  107.         request(uri).pipe(fs.createWriteStream(path)).on('close', resolve)
  108.     );
  109. }
  110.  
  111. async function postImage(path) {
  112.     const instance = await phantom.create();
  113.     const page = await instance.createPage();
  114.     await page.open(ZN_PROFILE_URL);
  115.     await page.switchToFrame('inner-iframe');
  116.     await page.evaluate(function () {
  117.         document.querySelector('.icon-image').click();
  118.     });
  119.     await page.uploadFile('input[type=file]', path);
  120.     await sleep(3000);
  121.     await page.evaluate(function () {
  122.         document.querySelector('.button-submit').click();
  123.     });
  124.     await sleep(3000);
  125.     await instance.exit();
  126. }
  127.  
  128. (async function () {
  129.     const reddit = new snoowrap(REDDIT_CREDENTIALS);
  130.     while (true) {
  131.         for (const subreddit of SUBREDDITS) {
  132.             const submissions = await reddit.getNew(subreddit, { limit: REDDIT_POLL_LIMIT });
  133.             for (const submission of submissions) {
  134.                 const url = submission.url;
  135.                 const url_ = url.replace(/\./g, '_');
  136.                 try {
  137.                     if (!db.get(url_).value()) {
  138.                         db.set(url_, true).write();
  139.                         if (url.indexOf('.gifv') !== -1) {
  140.                             continue;
  141.                         }
  142.                         const imgurAlbumMatch = url.match(/\/imgur.com\/a\/(.*)/);
  143.                         if (imgurAlbumMatch) {
  144.                             const images = await getImgurAlbumImages(imgurAlbumMatch[1]);
  145.                             iq(images);
  146.                             continue;
  147.                         }
  148.                         const imgurGalleryMatch = url.match(/\/imgur.com\/gallery\/(.*)/);
  149.                         if (imgurGalleryMatch) {
  150.                             const images = await getImgurGalleryImages(imgurGalleryMatch[1]);
  151.                             iq(images);
  152.                             continue;
  153.                         }
  154.                         const imgurImageMatch = url.match(/\/imgur.com\/(.*)/);
  155.                         if (imgurImageMatch) {
  156.                             const imageId = imgurImageMatch[1];
  157.                             if (imageId.indexOf('.') === -1) {
  158.                                 const image = await getImgurImage(imageId);
  159.                                 iq([image]);
  160.                             } else {
  161.                                 iq([url]);
  162.                             }
  163.                             continue;
  164.                         }
  165.                         if (
  166.                             url.indexOf('i.redd.it') !== -1 ||
  167.                             url.indexOf('i.imgur.com') !== -1
  168.                         ) {
  169.                             iq([submission.url]);
  170.                         }
  171.                     }
  172.                 } catch (e) {
  173.                     console.warn(`Error while processing '${url}': ${e}`);
  174.                 }
  175.             }
  176.         }
  177.         const imageUrl = iq();
  178.         console.log(`[${new Date().toTimeString().slice(0, 8)}] posting ${imageUrl}...`);
  179.         const ext = imageUrl.split('.').pop().split(/\#|\?/)[0];
  180.         const path = `D:/Pictures/zerome-reddit/${Date.now()}.${ext}`;
  181.         await downloadFile(imageUrl, path);
  182.         await postImage(path);
  183.         console.log('-> success!');
  184.         await sleep(600000);
  185.     }
  186. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement