Skip to content

Commit

Permalink
[search] fix language and support json format
Browse files Browse the repository at this point in the history
  • Loading branch information
BrightDV committed Dec 1, 2023
1 parent b125fa1 commit c30adbc
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions lib/api/searx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,85 @@ import 'package:http/http.dart' as http;

class SearXSearch {
final List<String> instances = [
'https://search.unlocked.link',
'https://search.sapti.me',
'https://search.neet.works',
'https://dynabyte.ca',
'https://searx.fmac.xyz',
'https://priv.au',
'https://searx.be',
'https://searx.tiekoetter.com',
'https://searx.work',
'https://darmarit.org/searx',
'https://search.bus-hit.me',
'https://search.demoniak.ch',
'https://northboot.xyz',
'https://search.in.projectsegfau.lt',
];

final List<String> instancesWithJsonFormat = [
'https://search.neet.works',
'https://searx.prvcy.eu',
'https://etsi.me',
'https://search.projectsegfau.lt',
];

Future<List> searchArticles(String query) async {
List results = await searchArticlesWithoutScraping(query);
if (results.isEmpty) {
results = await searchArticlesWithScraping(query);
}
return results;
}

Future<List> searchArticlesWithoutScraping(String query) async {
late http.Response response;
List results = [];
for (String instance in instancesWithJsonFormat) {
response = await http.get(
Uri.parse(
'$instance/search?q="https://formula1.com/en/latest/article" $query&language=all&format=json',
),
headers: {
'user-agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
},
);
if (response.body != 'Too Many Requests' &&
response.body != 'Rate limit exceeded' &&
response.statusCode == 200) {
break;
}
}
Map responseAsJson = jsonDecode(utf8.decode(response.bodyBytes));
for (var result in responseAsJson['results']) {
results.add(
{
'url': result['url'],
'title': result['title'],
'content': result['content'],
},
);
}

return results;
}

Future<List> searchArticlesWithScraping(String query) async {
late Uri url;
late http.Response response;
instances.shuffle();
for (String instance in instances) {
url = Uri.parse(
'$instance/search?q="formula1.com/en/latest/article" $query&language=en-US',
'$instance/search?q="https://formula1.com/en/latest/article" $query&language=all',
);
response = await http.get(
url,
headers: {
'user-agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
},
);
if (response.body != 'Too Many Requests' &&
response.body != 'Rate limit exceeded') {
response.body != 'Rate limit exceeded' &&
response.statusCode == 200) {
break;
}
}
Expand Down

0 comments on commit c30adbc

Please sign in to comment.