Improve error handling and parsing a little

This commit is contained in:
2025-02-22 16:12:52 +01:00
parent 406be305e2
commit 0a40e5bbcf
5 changed files with 31 additions and 72 deletions

View File

@@ -1,7 +1,6 @@
import 'package:gamer_updater/db.dart'; import 'package:gamer_updater/db.dart';
import 'package:gamer_updater/utils.dart'; import 'package:gamer_updater/utils.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:dart_rss/dart_rss.dart';
class Game { class Game {
final String name; final String name;
@@ -33,31 +32,29 @@ class Game {
} }
Future<void> updateActualVersion() async { Future<void> updateActualVersion() async {
final response = await http.get(Uri.parse(rssFeedUrl)); if (rssFeedUrl.isEmpty) {
final document = RssFeed.parse(response.body); throw Exception('No rss feed url for $name');
final pages = document.items;
pages.sort((a, b) {
var lhs = parseRfc822Date(a.pubDate!);
var rhs = parseRfc822Date(b.pubDate!);
return rhs.compareTo(lhs);
});
final versions =
pages
.map(
(e) =>
_internalVersionRegex.firstMatch(e.title!)?.group(1)?.trim(),
)
.toList();
for (int i = 0; i < versions.length; i++) {
final version = versions[i];
final page = pages[i];
if (version != null) {
actualVersion = version;
lastUpdated = parseRfc822Date(page.pubDate!).toIso8601String();
break;
}
} }
final response = await http.get(Uri.parse(rssFeedUrl));
if (response.statusCode != 200) {
throw Exception(
'Failed to update actual version for $name, rss responded with ${response.statusCode}',
);
}
final body = response.body;
final match = _internalVersionRegex.firstMatch(body);
if (match == null || match.groupCount == 0) {
throw Exception('No version found for $name');
}
final version = match.group(1);
if (version == null) {
throw Exception('No version found for $name');
}
actualVersion = version;
lastUpdated =
parseRfc822Date(
response.headers['last-modified'] ?? '',
).toIso8601String();
} }
} }

View File

@@ -27,21 +27,8 @@ DateTime parseRfc822Date(String date) {
final minute = int.parse(timeParts[1]); final minute = int.parse(timeParts[1]);
final second = int.parse(timeParts[2]); final second = int.parse(timeParts[2]);
// Handle the timezone offset
final timezone = parts[5];
final isNegative = timezone.startsWith('-');
final tzHours = int.parse(timezone.substring(1, 3));
final tzMinutes = int.parse(timezone.substring(3, 5));
// Create the DateTime object // Create the DateTime object
DateTime dateTime = DateTime(year, month, day, hour, minute, second); DateTime dateTime = DateTime(year, month, day, hour, minute, second);
// Adjust for timezone
if (isNegative) {
dateTime = dateTime.subtract(Duration(hours: tzHours, minutes: tzMinutes));
} else {
dateTime = dateTime.add(Duration(hours: tzHours, minutes: tzMinutes));
}
return dateTime; return dateTime;
} }

View File

@@ -106,7 +106,15 @@ class _GameCardState extends State<GameCard>
_controller.repeat(); _controller.repeat();
final updatedGame = widget.game; final updatedGame = widget.game;
await updatedGame.updateActualVersion(); try {
await updatedGame.updateActualVersion();
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(e.toString())));
}
}
widget.onGameUpdated(updatedGame); widget.onGameUpdated(updatedGame);
_controller.stop(); _controller.stop();

View File

@@ -49,14 +49,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.8" version: "1.0.8"
dart_rss:
dependency: "direct main"
description:
name: dart_rss
sha256: "73539d4b7153b47beef8b51763ca55dcb6fc0bb412b29e0f5e74e93fabfd1ac6"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@@ -107,14 +99,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.1.2" version: "4.1.2"
intl:
dependency: transitive
description:
name: intl
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
url: "https://pub.dev"
source: hosted
version: "0.19.0"
leak_tracker: leak_tracker:
dependency: transitive dependency: transitive
description: description:
@@ -179,14 +163,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.9.1" version: "1.9.1"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: "07c8f0b1913bcde1ff0d26e57ace2f3012ccbf2b204e070290dad3bb22797646"
url: "https://pub.dev"
source: hosted
version: "6.1.0"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@@ -304,14 +280,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.0" version: "1.1.0"
xml:
dependency: transitive
description:
name: xml
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
url: "https://pub.dev"
source: hosted
version: "6.5.0"
sdks: sdks:
dart: ">=3.7.0 <4.0.0" dart: ">=3.7.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54" flutter: ">=3.18.0-18.0.pre.54"

View File

@@ -36,7 +36,6 @@ dependencies:
cupertino_icons: ^1.0.8 cupertino_icons: ^1.0.8
sqflite_common_ffi: ^2.3.5 sqflite_common_ffi: ^2.3.5
http: ^1.3.0 http: ^1.3.0
dart_rss: ^3.0.3
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: