HTML Parsing -> changed to XML Parsing over API
This commit is contained in:
Ali
2024-11-25 19:26:03 +01:00
parent 6b5a1243c8
commit 67bd8c98bb
3 changed files with 36 additions and 11 deletions
+19 -11
View File
@@ -1,5 +1,8 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:xml/xml.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@@ -25,18 +28,23 @@ class FetchMenu {
return await loadCache(); return await loadCache();
} }
static Future<List<String>> fromRequest() async { static Future<List<String>> fromRequest() async {
List<String> menus = []; List<String> menus = [];
http.Response resp = await http.get(Uri.parse( var now = DateTime.now();
"https://www.swfr.de/essen/mensen-cafes-speiseplaene/mensa-loerrach")); var today = "${now.day.toString().padLeft(2, '0')}.${now.month.toString().padLeft(2, '0')}.${now.year}";
var content = resp.body.split("<h3>");
var innerContent = content[1].split("\n"); http.Response resp = await http.get(Uri.parse(
for (var s in innerContent) { "https://www.swfr.de/apispeiseplan?&type=98&tx_speiseplan_pi1[apiKey]=c3841e89a2c8c301b890723ecdb786ad&tx_speiseplan_pi1[ort]=677")); //Api Key lays here, modify this shit when you have time
if (s.contains("extra-text mb-15px")) { var xmlContent = XmlDocument.parse(utf8.decode(resp.bodyBytes));
var name = (s.split("\">")[1].split("</")[0]).replaceAll("<br>", " "); for (var tagesplan in xmlContent.findAllElements("tagesplan")){
menus.add(name); var datum = tagesplan.getAttribute('datum')?.trim();
if (datum == today){
for (var menu in tagesplan.findElements('menue')){
var name = menu.findElements('name').map((e) => e.text).join(", ");
menus.add(name);
}
}
} }
}
await storeCache(menus); await storeCache(menus);
+16
View File
@@ -224,6 +224,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.3.0" version: "2.3.0"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27
url: "https://pub.dev"
source: hosted
version: "6.0.2"
platform: platform:
dependency: transitive dependency: transitive
description: description:
@@ -397,6 +405,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.0" version: "1.1.0"
xml:
dependency: "direct main"
description:
name: xml
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
url: "https://pub.dev"
source: hosted
version: "6.5.0"
sdks: sdks:
dart: ">=3.5.2 <4.0.0" dart: ">=3.5.2 <4.0.0"
flutter: ">=3.24.0" flutter: ">=3.24.0"
+1
View File
@@ -35,6 +35,7 @@ dependencies:
flutter_rating_bar: ^4.0.1 flutter_rating_bar: ^4.0.1
http: ^1.2.2 http: ^1.2.2
shared_preferences: ^2.3.3 shared_preferences: ^2.3.3
xml: ^6.5.0
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.