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/utils.dart';
import 'package:http/http.dart' as http;
import 'package:dart_rss/dart_rss.dart';
class Game {
final String name;
@@ -33,31 +32,29 @@ class Game {
}
Future<void> updateActualVersion() async {
final response = await http.get(Uri.parse(rssFeedUrl));
final document = RssFeed.parse(response.body);
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;
}
if (rssFeedUrl.isEmpty) {
throw Exception('No rss feed url for $name');
}
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 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
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;
}

View File

@@ -106,7 +106,15 @@ class _GameCardState extends State<GameCard>
_controller.repeat();
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);
_controller.stop();