Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package.json
- {
- "name": "znb",
- "version": "1.0.0",
- "main": "index.js",
- "license": "MIT",
- "dependencies": {
- "lowdb": "^1.0.0",
- "phantom": "^4.0.5",
- "request": "^2.82.0",
- "request-promise-native": "^1.0.4",
- "shuffle-array": "^1.0.1",
- "snoowrap": "^1.14.2"
- }
- }
- */
- const SUBREDDITS = [
- 'FractalPorn',
- 'cats',
- 'foxes',
- ];
- const ZN_PROFILE_URL = 'http://127.0.0.1:43110/Me.ZeroNetwork.bit/?Profile/' +
- '12h51ug6CcntU2aiBjhP8Ns2e5VypbWWtv/1HseT5JgPe86zAKi1e9MD9sAVoACm1xeaD/dqhufbjdxj@zeroid.bit';
- const REDDIT_POLL_LIMIT = 20;
- const REDDIT_CREDENTIALS = {
- username: '...',
- password: '...',
- clientId: '...',
- clientSecret: '...',
- userAgent: 'nodejs',
- };
- const IMGUR_CREDENTIALS = {
- clientId: '...',
- clientSecret: '...',
- };
- const IMGUR_HEADERS = {
- 'Authorization': `Client-ID ${IMGUR_CREDENTIALS.clientId}`,
- };
- const fs = require('fs');
- const request = require('request');
- const rp = require('request-promise-native');
- const phantom = require('phantom');
- const snoowrap = require('snoowrap');
- const lowdb = require('lowdb');
- const FileSync = require('lowdb/adapters/FileSync');
- const shuffle = require('shuffle-array');
- const adapter = new FileSync('db.json');
- const db = lowdb(adapter);
- if (!db.get('imageQueue').value()) {
- db.set('imageQueue', []).write();
- }
- function iq(images) {
- const imageQueue = db.get('imageQueue').value();
- if (images) {
- db.set('imageQueue',
- shuffle(imageQueue.concat(images))
- ).write();
- } else {
- const image = imageQueue.pop();
- db.set('imageQueue', shuffle(imageQueue)).write();
- return image;
- }
- }
- function sleep(ms) {
- return new Promise(resolve =>
- setTimeout(resolve, ms)
- );
- }
- async function getImgurAlbumImages(albumId) {
- const response = await rp.get({
- url: `https://api.imgur.com/3/album/${albumId}/images`,
- headers: IMGUR_HEADERS,
- });
- const data = JSON.parse(response).data;
- return data.link ||
- data.map(entry => entry.link);
- }
- async function getImgurGalleryImages(galleryId) {
- const response = await rp.get({
- url: `https://api.imgur.com/3/gallery/${galleryId}`,
- headers: IMGUR_HEADERS,
- });
- const data = JSON.parse(response).data;
- return data.images
- ? data.images.map(entry => entry.link)
- : data.link;
- }
- async function getImgurImage(imageId) {
- const response = await rp.get({
- url: `https://api.imgur.com/3/image/${imageId}`,
- headers: IMGUR_HEADERS,
- });
- return JSON.parse(response).data.link;
- }
- function downloadFile(uri, path) {
- return new Promise(resolve =>
- request(uri).pipe(fs.createWriteStream(path)).on('close', resolve)
- );
- }
- async function postImage(path) {
- const instance = await phantom.create();
- const page = await instance.createPage();
- await page.open(ZN_PROFILE_URL);
- await page.switchToFrame('inner-iframe');
- await page.evaluate(function () {
- document.querySelector('.icon-image').click();
- });
- await page.uploadFile('input[type=file]', path);
- await sleep(3000);
- await page.evaluate(function () {
- document.querySelector('.button-submit').click();
- });
- await sleep(3000);
- await instance.exit();
- }
- (async function () {
- const reddit = new snoowrap(REDDIT_CREDENTIALS);
- while (true) {
- for (const subreddit of SUBREDDITS) {
- const submissions = await reddit.getNew(subreddit, { limit: REDDIT_POLL_LIMIT });
- for (const submission of submissions) {
- const url = submission.url;
- const url_ = url.replace(/\./g, '_');
- try {
- if (!db.get(url_).value()) {
- db.set(url_, true).write();
- if (url.indexOf('.gifv') !== -1) {
- continue;
- }
- const imgurAlbumMatch = url.match(/\/imgur.com\/a\/(.*)/);
- if (imgurAlbumMatch) {
- const images = await getImgurAlbumImages(imgurAlbumMatch[1]);
- iq(images);
- continue;
- }
- const imgurGalleryMatch = url.match(/\/imgur.com\/gallery\/(.*)/);
- if (imgurGalleryMatch) {
- const images = await getImgurGalleryImages(imgurGalleryMatch[1]);
- iq(images);
- continue;
- }
- const imgurImageMatch = url.match(/\/imgur.com\/(.*)/);
- if (imgurImageMatch) {
- const imageId = imgurImageMatch[1];
- if (imageId.indexOf('.') === -1) {
- const image = await getImgurImage(imageId);
- iq([image]);
- } else {
- iq([url]);
- }
- continue;
- }
- if (
- url.indexOf('i.redd.it') !== -1 ||
- url.indexOf('i.imgur.com') !== -1
- ) {
- iq([submission.url]);
- }
- }
- } catch (e) {
- console.warn(`Error while processing '${url}': ${e}`);
- }
- }
- }
- const imageUrl = iq();
- console.log(`[${new Date().toTimeString().slice(0, 8)}] posting ${imageUrl}...`);
- const ext = imageUrl.split('.').pop().split(/\#|\?/)[0];
- const path = `D:/Pictures/zerome-reddit/${Date.now()}.${ext}`;
- await downloadFile(imageUrl, path);
- await postImage(path);
- console.log('-> success!');
- await sleep(600000);
- }
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement