| 1 | import helper from './helper.js' |
| 2 | import async from 'async' |
| 3 | |
| 4 | async function get_words_info (all_words, words_info) { |
| 5 | const temp_added = [] |
| 6 | for (const all_words_key of Object.keys(all_words)) { |
| 7 | for (const all_words_word of all_words[all_words_key]) { |
| 8 | if (!temp_added.includes(all_words_word)) { |
| 9 | temp_added.push(all_words_word) |
| 10 | const temp_words_info = { |
| 11 | word: all_words_word, |
| 12 | text: '', |
| 13 | results: [] |
| 14 | } |
| 15 | try { |
| 16 | const url1 = 'https://api.duckduckgo.com/?q={0}&format=json&pretty=1&no_html=1&skip_disambig=1'.replace('{0}', all_words_word) |
| 17 | const url2 = 'https://api.duckduckgo.com/?q={0}&format=json&pretty=1'.replace('{0}', all_words_word) |
| 18 | const response1 = await helper.get_url_wrapper_json(url1) |
| 19 | const response2 = await helper.get_url_wrapper_json(url2) |
| 20 | if (response2.data !== '') { |
| 21 | if ('RelatedTopics' in response2.data) { |
| 22 | if (response2.data.RelatedTopics.length > 0) { |
| 23 | if (response2.data !== '') { |
| 24 | if ('AbstractText' in response1.data && response1.data.AbstractText !== '') { |
| 25 | temp_words_info.text = response1.data.AbstractText |
| 26 | } else if ('Abstract' in response1.data && response1.data.AbstractText !== '') { |
| 27 | temp_words_info.text = response1.data.Abstract |
| 28 | } else { |
| 29 | temp_words_info.text = 'unknown' |
| 30 | } |
| 31 | } |
| 32 | response2.data.RelatedTopics.forEach(function (item) { |
| 33 | if ('Name' in item) { |
| 34 | item.Topics.forEach(function (topic) { |
| 35 | temp_words_info.results.push({ |
| 36 | type: item.Name, |
| 37 | text: topic.Text, |
| 38 | url: topic.FirstURL |
| 39 | }) |
| 40 | }) |
| 41 | } else { |
| 42 | temp_words_info.results.push({ |
| 43 | type: 'Related', |
| 44 | text: item.Text, |
| 45 | url: item.FirstURL |
| 46 | }) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (temp_words_info.results.length > 0) { |
| 54 | words_info.push(temp_words_info) |
| 55 | } |
| 56 | } catch (error) { |
| 57 | helper.verbose && console.log(error) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | async function check_engines (req, info) { |
| 65 | try { |
| 66 | if (helper.google_api_key === '' || helper.google_api_cs === '') { |
| 67 | return |
| 68 | } |
| 69 | const url = 'https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}'.replace('{0}', helper.google_api_key).replace('{1}', helper.google_api_cs).replace('{2}', req.body.string) |
| 70 | const response = await helper.get_url_wrapper_json(url) |
| 71 | if (response.data !== '') { |
| 72 | try { |
| 73 | info.original = response.data.queries.request[0].searchTerms |
| 74 | } catch (e) {} |
| 75 | try { |
| 76 | info.corrected = response.data.spelling.correctedQuery |
| 77 | } catch (e) {} |
| 78 | try { |
| 79 | info.total = response.data.searchInformation.totalResults |
| 80 | } catch (e) {} |
| 81 | try { |
| 82 | response.data.items.forEach(function (item) { |
| 83 | info.items.push({ |
| 84 | title: item.title, |
| 85 | snippet: item.snippet |
| 86 | }) |
| 87 | }) |
| 88 | } catch (e) {} |
| 89 | try { |
| 90 | if (info.total === 0 && info.corrected !== '') { |
| 91 | info.checking = info.original + ' [Error]<br>Try this: ' + info.corrected |
| 92 | } else if (info.total > 0 && info.corrected !== '') { |
| 93 | info.checking = info.original + ' [Good]<br>Suggested word: ' + info.corrected + '<br>Total lookups: ' + info.total |
| 94 | } else if (info.total > 0 && info.corrected === '') { |
| 95 | info.checking = info.original + ' [Good]<br>Total lookups: ' + info.total |
| 96 | } else { |
| 97 | info.checking = 'Using ' + info.original + ' with no lookups' |
| 98 | } |
| 99 | } catch (e) {} |
| 100 | } |
| 101 | } catch (error) { |
| 102 | helper.verbose && console.log(error) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | async function custom_search_ouputs (req) { |
| 107 | const possible_parameters = ['user', 'profile', 'account'] |
| 108 | const time = new Date() |
| 109 | const functions = [] |
| 110 | possible_parameters.forEach((key) => { |
| 111 | functions.push(custom_search_ouputs_website.bind(null, req.body.uuid, req.body.string, key)) |
| 112 | }) |
| 113 | const results = await async.parallelLimit(functions, 6) |
| 114 | helper.verbose && console.log(`Total time ${new Date() - time}`) |
| 115 | const merged = [].concat.apply([], results.filter(item => item !== undefined)) |
| 116 | return merged |
| 117 | } |
| 118 | |
| 119 | async function custom_search_ouputs_website (uuid, name, key) { |
| 120 | return new Promise(async (resolve, reject) => { |
| 121 | try { |
| 122 | const results = [] |
| 123 | helper.log_to_file_queue(uuid, '[Custom Search Using] ' + key) |
| 124 | const url = 'https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}:{3}'.replace('{0}', helper.google_api_key).replace('{1}', helper.google_api_cs).replace('{2}', key).replace('{3}', name) |
| 125 | const response = await helper.get_url_wrapper_json(url) |
| 126 | if (response.data !== '') { |
| 127 | if ('items' in response.data) { |
| 128 | response.data.items.forEach((key) => { |
| 129 | results.push({ |
| 130 | site: helper.get_site_from_url(key.link), |
| 131 | link: key.link, |
| 132 | snippet: key.snippet |
| 133 | }) |
| 134 | }) |
| 135 | } |
| 136 | } |
| 137 | if (results.length > 0) { |
| 138 | resolve(results) |
| 139 | } else { |
| 140 | resolve(undefined) |
| 141 | } |
| 142 | } catch (err) { |
| 143 | resolve(undefined) |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | |
| 148 | export default{ |
| 149 | check_engines, |
| 150 | get_words_info, |
| 151 | custom_search_ouputs |
| 152 | } |
| 153 |