diff --git a/aws.js b/aws.js deleted file mode 100644 index 00ab5b6..0000000 --- a/aws.js +++ /dev/null @@ -1,66 +0,0 @@ -var async = require('async'); -var fs = require('fs'); -var path = require('path'); -var filenamify = require('filenamify'); -var express = require('express'); - -const { BOT_URL, WORDS_DIR, WORDS_JSON_DIR } = require('./constants'); -const WordService = require('./helpers/WordService'); - -const app = express(); - -app.get('/', (req, res) => { - -}); - -var words = process.argv.find(x => x.indexOf('--words=') !== -1); - -app.listen(1071, () => { - console.log('1071', 'dinleniyor'); - - if (words !== undefined) { - words = words.split('=')[1].split(','); - - var res = []; - - words.map(x => { - var fileName = path.join(WORDS_DIR, x + '.json'); - var original_words = fs.readFileSync(fileName).toString(); - original_words = JSON.parse(original_words); - - res = res.concat(original_words); - }); - - words = res; - } - - var run = (index = 0) => { - var word = words[index]; - - var cb = (data) => { - console.log('İstek bitti', data); - run(index + 1); - }; - - const fileName = path.join(WORDS_JSON_DIR, filenamify(word) + '.json'); - - if (fs.existsSync(fileName)) { - cb(fs.readFileSync(fileName).toString(), index); - return; - } - - WordService.saveWord(word).then(res => { - - fs.writeFile(fileName, res, function(err) { - if(err) { - console.log(err); - } - }); - - cb(JSON.stringify(res), index); - }) - .catch(err => cb(err, index)); - }; - - run(); -}) diff --git a/constants.js b/constants.js index 71528b8..1bbdaaa 100644 --- a/constants.js +++ b/constants.js @@ -3,7 +3,6 @@ const path = require('path'); module.exports = { API_URL: "http://www.tdk.org.tr/index.php?option=com_gts&arama=gts&kelime=WORD", TDK_WORD_LIST_URL: "http://tdk.gov.tr/index.php?option=com_yazimkilavuzu&view=yazimkilavuzu&kategori1=yazim_listeli&ayn1=bas&kelime1=WORD&sayfa1=PAGE", - AWS_API_URL: "https://nnv7qx39h3.execute-api.us-east-1.amazonaws.com/dev/getWord/?word=WORD", WORDS_DIR: path.join(__dirname, 'words'), WORDS_JSON_DIR: path.join(__dirname, 'words_json'), }; diff --git a/helpers/WordService.js b/helpers/WordService.js index aeb9cdc..252053b 100644 --- a/helpers/WordService.js +++ b/helpers/WordService.js @@ -3,7 +3,6 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const { API_URL, TDK_WORD_LIST_URL, - AWS_API_URL, WORDS_DIR, } = require('../constants'); @@ -67,71 +66,6 @@ const WordService = { }); }, - /* - * AWS üzerinde açık olan servise istek atar ve kaydedilmesini sağlar - * @param {string} word - kaydedilecek gelime - */ - saveWord: (word) => { - return new Promise((resolve, reject) => { - var errorCount = 0; - - var sendError = () => { - reject(new NE.HttpException('AWS request error.', 500)); - }; - - var sendRequest = () => { - var data = null; - - var xhr = new XMLHttpRequest(); - - xhr.addEventListener("readystatechange", function () { - if (this.readyState == 4) { - if (this.status === 200) { - resolve(xhr.responseText); - } - else if (this.status === 404) { - resolve("[]"); - } - else { - console.log('AWS request error, Trying again.'); - if (errorCount++ < 10) { - setTimeout(() => { - sendRequest(); - }, 5500); - } - else { - sendError(); - } - } - } - }); - - xhr.onerror = function(err) { - console.log('AWS request error, Trying again.', err); - if (errorCount++ < 10) { - setTimeout(() => { - sendRequest(); - }, 5500); - } - else { - sendError(); - } - }; - - const URL = AWS_API_URL.replace('WORD', encodeURI(word)); - - xhr.open("GET", URL); - - console.log(word, URL); - - xhr.send(data); - }; - - sendRequest(); - - }); - }, - /* * Tdk'nın sitesinden getirilen raw html değerinin içinden kelimeye ait tüm özellikleri getirir * @param {string} raw - tdk kelime sayfasından gelen raw html değer diff --git a/index.js b/index.js deleted file mode 100644 index a4c8f70..0000000 --- a/index.js +++ /dev/null @@ -1,47 +0,0 @@ -var async = require('async'); -var fs = require('fs'); -var path = require('path'); - -const { BOT_URL, WORDS_DIR } = require('./constants'); -const WordService = require('./helpers/WordService'); - -var words = process.argv.find(x => x.indexOf('--words=') !== -1); - - -if (words !== undefined) { - words = words.split('=')[1].split(','); -} - -var calls = []; - -words.map(word => { - calls.push((cb) => { - const fileName = path.join(WORDS_DIR, word + '.json'); - - if (fs.existsSync(fileName)) { - cb(fs.readFileSync(fileName).toString()); - return; - } - - WordService.getFirstWord(word).then(res => { - - fs.writeFile(fileName, JSON.stringify(res), function(err) { - if(err) { - console.log(err); - } - }); - - cb(JSON.stringify(res)); - }) - .catch(cb); - }); -}) - -async.parallel(calls, function(err, result) { - /* this code will run after all calls finished the job or - when any of the calls passes an error */ - if (err) - return console.log(err); - console.log(result); -}); -