Advertisement
theatz

Untitled

Jul 6th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2. const ffmpeg = require('ffmpeg');
  3. const Telegram = require('telegraf/telegram');
  4. const https = require('https');
  5. const fs = require('fs');
  6. const token = ':AAFkOQQGhepGz0GSqojDBSVkhwbLFh6PiW4';
  7. const MongoClient = require('mongodb').MongoClient;
  8.  
  9.  
  10. const bot = new Telegram(token);
  11.  
  12. const MongoUrl = 'mongodb://@ds259410.mlab.com:59410/2ch';
  13.  
  14.  
  15. const webm = /.webm/;
  16.  
  17. //https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread=178558207&post=1
  18. //https://2ch.hk/b/res/178558207.json
  19.  
  20. //функция загрузки по url и отправки данных ботом
  21. async function download(url) {
  22.   https.get(url, (res) => {
  23.     //console.log('statusCode:', res.statusCode);
  24.     //console.log('headers:', res.headers);
  25.  
  26.     //console.log(res.data);
  27.     //var stream =
  28.     const file = fs.createWriteStream('example.webm');
  29.  
  30.  
  31.     res.on('data', (d) => {
  32.     //process.stdout.write(d);
  33.     //console.log(d.length);
  34.     file.write(d);
  35.     //console.log('got');
  36.     });
  37.  
  38.  
  39.     res.on('end', () => {
  40.       //console.log('now here');
  41.  
  42.       const converted = (async function() {
  43.           await convert();
  44.       })();
  45.  
  46.       bot.sendDocument('-1001392629997', { source: fs.readFileSync('example.webm') } , '');
  47.  
  48.       file.destroy();
  49.  
  50.       fs.unlinkSync('example.webm');
  51.       fs.unlinkSync('example.mp4');
  52.  
  53.  
  54.     });
  55.  
  56.  
  57.     //console.log('here');
  58.  
  59.  
  60.   }).on('error', (e) => {
  61.     console.error(e);
  62.   });
  63. }
  64.  
  65.  
  66.  
  67. //webm=>mp4
  68. const convert = async () => {
  69.  
  70.  
  71. try {
  72.   var process = new ffmpeg('./example.webm');
  73.   process.then(function (video) {
  74.  
  75.     video
  76.     .setVideoFormat('mp4')
  77.     .save('./example.mp4', function (error, file) {
  78.       if (!error){}
  79.         //console.log('Video file: '); //+ file);
  80.         //return video;
  81.     })
  82.  
  83.   }, function (err) {
  84.     console.log('Error: ' + err);
  85.   });
  86. } catch (e) {
  87.   console.log(e.code);
  88.   console.log(e.msg);
  89. }
  90.  
  91. }
  92.  
  93.  
  94. //тестовая(не обращай внимания)
  95. const find_in = async () => {
  96.     //+data[i].url+'&post='+data[i].post
  97.     //https://2ch.hk/b/res/178777055.html
  98.     axios.get('https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread=178735072&post=1').then(function (response) {
  99.  
  100.  
  101.     //console.log(response.data[21].files);
  102.     //console.log(JSON.parse(data));
  103.     //console.log(response.data.threads[0].posts);
  104.  
  105.     for(let i=0; i<response.data.length; i++){
  106.       for(let j=0; j<response.data[i].files.length; j++ ){
  107.         if(webm.test(response.data[i].files[j].name)){
  108.           console.log('here');
  109.           (async function() {
  110.               await download('https://2ch.hk/'+response.data[i].files[j].path);
  111.           })();
  112.         }
  113.       }
  114.     };
  115.  
  116.     console.log(response.data.length);
  117.  
  118.  
  119.     }).catch (function (e) {
  120.  
  121.         console.log(e);
  122.  
  123.     });
  124.   }
  125.  
  126.  
  127.  
  128. //find_in();
  129.  
  130. function find(j) {
  131.  
  132.  
  133. axios.get('http://2ch.hk/b/'+`${j}`+'.json')
  134.   .then( (response) => {
  135.     //console.log(response.data.threads.length);
  136.     for (let i = 0; i < response.data.threads.length; i++) {
  137.       //console.log(response.data.threads[i].posts[0].comment+'\n');
  138.       if (webm.test(response.data.threads[i].posts[0].comment)){
  139.         console.log('true');
  140.         console.log(response.data.threads[i]);
  141.         threads.fi
  142.       }
  143.     }
  144.   })
  145.   .catch( (err) => {
  146.     console.log(err);
  147.   });
  148.  
  149.  
  150. };
  151.  
  152. //возвращает количество постов, уже просмотренных мною
  153. function mongo_check(url) {
  154.   threads.findOne({"url":url})
  155.     .then( (data) => {
  156.        console.log(data);
  157.        if (data !== null){
  158.        console.log('not null');
  159.        console.log(data.count);
  160.        return data.count
  161.      }
  162.   })
  163.     .catch( (e) => {
  164.       console.log(e);
  165.     });
  166.  
  167. }
  168.  
  169. //добавление нового треда
  170. function mongo_find(url) {
  171.   threads.findOne({"url":url})
  172.     .then( (data) => {
  173.        console.log(data);
  174.        if (data == null){
  175.          threads.insertOne({"url":url,"count":1,});
  176.      }
  177.  
  178.   })
  179.     .catch( (e) => {
  180.       console.log(e);
  181.     });
  182. }
  183.  
  184. //поиск на странице (url берем из бд)
  185. function thread_pass(url) {
  186.     //https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread=178854739&post=1
  187.     axios.get('https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread='+url+'&post='+mongo_check(url))
  188.       .then( (response) =>{
  189.         for (let i = 0; i < response.data.length; i++) {
  190.           for (var j = 0; j < response.data[i].files.length; j++) {
  191.             //if (webm.test(response.data[i].files[j]))
  192.             console.log(response.data[i].files[j].path);
  193.             if (webm.test(response.data[i].files[j].name)){
  194.               download('https://2ch.hk'+response.data[i].files[j].path);
  195.             }
  196.           }
  197.         }
  198.       })
  199.     .catch( (e) =>{
  200.       threads.remove({'url' : url});
  201.     })
  202. }
  203.  
  204. function thread_mongo() {
  205.   for (var i = 0; i < threads.length; i++) {
  206.     thread_pass(threads)
  207.   }
  208. }
  209.  
  210. //чекаем первые 3 страницы на webm в шапке треда
  211. function board_check() {
  212.   for (var i = 0; i < 4; i++) {
  213.   axios.get('http://2ch.hk/b/'+`${i}`+'.json')
  214.     .then( (response) => {
  215.       //console.log(response.data.threads.length);
  216.       for (let i = 0; i < response.data.threads.length; i++) {
  217.         //console.log(response.data.threads[i].posts[0].comment+'\n');
  218.         if (webm.test(response.data.threads[i].posts[0].comment)){
  219.           console.log('true');
  220.           mongo_find(response.data.threads[i].parent);  
  221.         }
  222.       }
  223.     })
  224.     .catch( (err) => {
  225.       console.log(err);
  226.     });
  227.   }
  228. }
  229.  
  230.  
  231. MongoClient.connect(MongoUrl)
  232.   .then( (client) => {
  233.  
  234.   const db = client.db('2ch');
  235.  
  236.   const threads =  db.collection('threads');
  237.  
  238.   console.log(threads);
  239.   //convert();
  240.  
  241.  
  242.  
  243. }).catch( (err) => {
  244.   console.log(err);
  245. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement