Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios');
- const ffmpeg = require('ffmpeg');
- const Telegram = require('telegraf/telegram');
- const https = require('https');
- const fs = require('fs');
- const token = ':AAFkOQQGhepGz0GSqojDBSVkhwbLFh6PiW4';
- const MongoClient = require('mongodb').MongoClient;
- const bot = new Telegram(token);
- const MongoUrl = 'mongodb://@ds259410.mlab.com:59410/2ch';
- const webm = /.webm/;
- //https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread=178558207&post=1
- //https://2ch.hk/b/res/178558207.json
- //функция загрузки по url и отправки данных ботом
- async function download(url) {
- https.get(url, (res) => {
- //console.log('statusCode:', res.statusCode);
- //console.log('headers:', res.headers);
- //console.log(res.data);
- //var stream =
- const file = fs.createWriteStream('example.webm');
- res.on('data', (d) => {
- //process.stdout.write(d);
- //console.log(d.length);
- file.write(d);
- //console.log('got');
- });
- res.on('end', () => {
- //console.log('now here');
- const converted = (async function() {
- await convert();
- })();
- bot.sendDocument('-1001392629997', { source: fs.readFileSync('example.webm') } , '');
- file.destroy();
- fs.unlinkSync('example.webm');
- fs.unlinkSync('example.mp4');
- });
- //console.log('here');
- }).on('error', (e) => {
- console.error(e);
- });
- }
- //webm=>mp4
- const convert = async () => {
- try {
- var process = new ffmpeg('./example.webm');
- process.then(function (video) {
- video
- .setVideoFormat('mp4')
- .save('./example.mp4', function (error, file) {
- if (!error){}
- //console.log('Video file: '); //+ file);
- //return video;
- })
- }, function (err) {
- console.log('Error: ' + err);
- });
- } catch (e) {
- console.log(e.code);
- console.log(e.msg);
- }
- }
- //тестовая(не обращай внимания)
- const find_in = async () => {
- //+data[i].url+'&post='+data[i].post
- //https://2ch.hk/b/res/178777055.html
- axios.get('https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread=178735072&post=1').then(function (response) {
- //console.log(response.data[21].files);
- //console.log(JSON.parse(data));
- //console.log(response.data.threads[0].posts);
- for(let i=0; i<response.data.length; i++){
- for(let j=0; j<response.data[i].files.length; j++ ){
- if(webm.test(response.data[i].files[j].name)){
- console.log('here');
- (async function() {
- await download('https://2ch.hk/'+response.data[i].files[j].path);
- })();
- }
- }
- };
- console.log(response.data.length);
- }).catch (function (e) {
- console.log(e);
- });
- }
- //find_in();
- function find(j) {
- axios.get('http://2ch.hk/b/'+`${j}`+'.json')
- .then( (response) => {
- //console.log(response.data.threads.length);
- for (let i = 0; i < response.data.threads.length; i++) {
- //console.log(response.data.threads[i].posts[0].comment+'\n');
- if (webm.test(response.data.threads[i].posts[0].comment)){
- console.log('true');
- console.log(response.data.threads[i]);
- threads.fi
- }
- }
- })
- .catch( (err) => {
- console.log(err);
- });
- };
- //возвращает количество постов, уже просмотренных мною
- function mongo_check(url) {
- threads.findOne({"url":url})
- .then( (data) => {
- console.log(data);
- if (data !== null){
- console.log('not null');
- console.log(data.count);
- return data.count
- }
- })
- .catch( (e) => {
- console.log(e);
- });
- }
- //добавление нового треда
- function mongo_find(url) {
- threads.findOne({"url":url})
- .then( (data) => {
- console.log(data);
- if (data == null){
- threads.insertOne({"url":url,"count":1,});
- }
- })
- .catch( (e) => {
- console.log(e);
- });
- }
- //поиск на странице (url берем из бд)
- function thread_pass(url) {
- //https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread=178854739&post=1
- axios.get('https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=b&thread='+url+'&post='+mongo_check(url))
- .then( (response) =>{
- for (let i = 0; i < response.data.length; i++) {
- for (var j = 0; j < response.data[i].files.length; j++) {
- //if (webm.test(response.data[i].files[j]))
- console.log(response.data[i].files[j].path);
- if (webm.test(response.data[i].files[j].name)){
- download('https://2ch.hk'+response.data[i].files[j].path);
- }
- }
- }
- })
- .catch( (e) =>{
- threads.remove({'url' : url});
- })
- }
- function thread_mongo() {
- for (var i = 0; i < threads.length; i++) {
- thread_pass(threads)
- }
- }
- //чекаем первые 3 страницы на webm в шапке треда
- function board_check() {
- for (var i = 0; i < 4; i++) {
- axios.get('http://2ch.hk/b/'+`${i}`+'.json')
- .then( (response) => {
- //console.log(response.data.threads.length);
- for (let i = 0; i < response.data.threads.length; i++) {
- //console.log(response.data.threads[i].posts[0].comment+'\n');
- if (webm.test(response.data.threads[i].posts[0].comment)){
- console.log('true');
- mongo_find(response.data.threads[i].parent);
- }
- }
- })
- .catch( (err) => {
- console.log(err);
- });
- }
- }
- MongoClient.connect(MongoUrl)
- .then( (client) => {
- const db = client.db('2ch');
- const threads = db.collection('threads');
- console.log(threads);
- //convert();
- }).catch( (err) => {
- console.log(err);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement