Advertisement
Guest User

Untitled

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