Skip to content

Commit

Permalink
use dio instead of http
Browse files Browse the repository at this point in the history
  • Loading branch information
ksh-b committed May 12, 2024
1 parent d63ba05 commit 5d1bdb8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
6 changes: 5 additions & 1 deletion lib/extractor/trend/google.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:hive/hive.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart' as html_parser;
Expand All @@ -20,14 +21,17 @@ class GoogleTrend extends Trend {
var country = "US";
if (Hive.isBoxOpen("settings"))
country = "${countryCodes[Store.countrySetting]}";
var response = await dio().get(url + country);
var response = await dio().get(url + country, options: Options(
responseType: ResponseType.plain,
));
List<String> queries = [];
if (response.statusCode == 200) {
Document document = html_parser.parse(response.data);
var jsonText = document.outerHtml
.replaceFirst(")]}',", "")
.replaceFirst("<html><head></head><body>", "")
.replaceFirst("</body></html>", "");
print(jsonText);
var somethings = json.decode(jsonText)["default"]["trendingSearchesDays"];
for (var something in somethings) {
var searches = something["trendingSearches"];
Expand Down
44 changes: 26 additions & 18 deletions lib/service/simplytranslate.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:dio/dio.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart' as html_parser;
import 'package:html/parser.dart';
import 'package:raven/brain/dio_manager.dart';
import 'package:raven/utils/store.dart';
Expand Down Expand Up @@ -163,13 +162,12 @@ class SimplyTranslate {
String language,
) async {
List<String> translated = [];
var mSentences = mergeSentences(sentences);
for (var ms in mSentences) {
for (var ms in sentences) {
var s = await translate(
ms,
language,
);
translated.addAll(s.split("~~~"));
translated.add(s);
}
return translated;
}
Expand Down Expand Up @@ -223,7 +221,7 @@ class SimplyTranslate {
for (String sentence in sentences) {
if (currentMergedSentence.length + sentence.length <= 4000) {
if (currentMergedSentence.isNotEmpty) {
currentMergedSentence += "~~~";
currentMergedSentence += " ";
}
currentMergedSentence += sentence;
} else {
Expand All @@ -241,30 +239,40 @@ class SimplyTranslate {
(element) => element.isEmpty,
)
..map(
(e) => e + "~~~",
(e) => e + " ",
);
}

List<String> splitParagraph(String paragraph, [int wordsPerSentence = 500]) {
final List<String> words = paragraph.replaceAll(" <", "<").replaceAll("> ", ">").split(new RegExp(r'\s+'));
final List<List<String>> groupedWords = _groupLists(words, wordsPerSentence);
final List<String> sentences = groupedWords.map((wordList) => wordList.join(' ')).toList();

return sentences;
}

List<List<String>> _groupLists(List<String> items, int groupSize) {
final List<List<String>> groups = [];
for (int i = 0; i < items.length; i += groupSize) {
groups.add(items.sublist(i, i + groupSize > items.length ? items.length : i + groupSize));
}
return groups;
}


Future<String> translateParagraph(
String paragraph,
String language,
) async {
if(paragraph.isEmpty) return paragraph;
Document document = html_parser.parse(paragraph.replaceAll("<", " <").replaceAll(">", "> "));
var html = document.outerHtml;
List<String> smallParas = mergeSentences(extractTextFromDocument(document));
var translatedHtml = "";
List<String> smallParas = splitParagraph(paragraph);
List<String> translatedSmallParas = List.filled(smallParas.length, '');
for (int i = 0; i < smallParas.length; i++) {
translatedSmallParas[i] = await translate(smallParas[i], language);
translatedHtml +=translatedSmallParas[i];
}
for (int i = 0; i < translatedSmallParas.length; i++) {
for (int j = 0; j < translatedSmallParas[i].split("~~~").length; j++) {
html = html.replaceFirst(
smallParas[i].split("~~~")[j],
translatedSmallParas[i].split("~~~")[j],
);
}
}
return html;

return translatedHtml;
}
}

0 comments on commit 5d1bdb8

Please sign in to comment.