mirror of
https://gitlab.com/mysocialportal/fediverse-archiving-tools.git
synced 2024-10-18 08:53:31 +00:00
Initial implementatino of the FriendicaEntry
This commit is contained in:
parent
799b14e020
commit
0302fa2cd0
3 changed files with 60 additions and 9 deletions
|
@ -64,10 +64,13 @@ void main(List<String> arguments) async {
|
|||
username: username,
|
||||
password: password,
|
||||
serverName: settings['server-name']);
|
||||
final timelineResult = await client.getTimeline(username, 1, 3);
|
||||
final timelineResult = await client.getTimeline(username, 1, 20);
|
||||
timelineResult.match(
|
||||
onSuccess: (posts) => File('/tmp/test.json')
|
||||
.writeAsStringSync(PrettyJsonEncoder().convert(posts)),
|
||||
onSuccess: (posts) {
|
||||
posts.forEach(print);
|
||||
File('/tmp/test.json').writeAsStringSync(PrettyJsonEncoder()
|
||||
.convert(posts.map((p) => p.originalJson).toList()));
|
||||
},
|
||||
onError: (error) => print('Error getting posts: $error'));
|
||||
print("Done processing API requests");
|
||||
return;
|
||||
|
|
|
@ -3,6 +3,8 @@ import 'dart:io';
|
|||
|
||||
import 'package:result_monad/result_monad.dart';
|
||||
|
||||
import 'models.dart';
|
||||
|
||||
class FriendicaClient {
|
||||
final String username;
|
||||
final String password;
|
||||
|
@ -19,15 +21,12 @@ class FriendicaClient {
|
|||
_authHeader = "Basic $encodedAuthString";
|
||||
}
|
||||
|
||||
FutureResult<List<dynamic>, String> getTimeline(
|
||||
FutureResult<List<FriendicaEntry>, String> getTimeline(
|
||||
String userId, int page, int count) async {
|
||||
final request = Uri.parse(
|
||||
'https://$serverName/api/statuses/user_timelineuser_id=$userId&count=$count&page=$page');
|
||||
return await _getApiRequest(request);
|
||||
}
|
||||
|
||||
FutureResult<List<dynamic>, String> getPostComments(int postId) async {
|
||||
return Result.error("Not Implemented");
|
||||
return (await _getApiRequest(request)).mapValue((postsJson) =>
|
||||
postsJson.map((postJson) => FriendicaEntry.fromJson(postJson)).toList());
|
||||
}
|
||||
|
||||
FutureResult<List<dynamic>, String> _getApiRequest(Uri url) async {
|
||||
|
@ -38,6 +37,7 @@ class FriendicaClient {
|
|||
ContentType('application', 'json', charset: 'utf-8');
|
||||
final response = await request.close();
|
||||
final body = await response.transform(utf8.decoder).join('');
|
||||
File('/tmp/response.json').writeAsStringSync(body);
|
||||
final bodyJson = jsonDecode(body) as List<dynamic>;
|
||||
return Result.ok(bodyJson);
|
||||
}
|
||||
|
|
48
bin/models.dart
Normal file
48
bin/models.dart
Normal file
|
@ -0,0 +1,48 @@
|
|||
class FriendicaEntry {
|
||||
final Map<String, dynamic> originalJson;
|
||||
final int id;
|
||||
final String text;
|
||||
final int commentCount;
|
||||
final List<String> images;
|
||||
|
||||
FriendicaEntry(
|
||||
{required this.originalJson,
|
||||
required this.text,
|
||||
required this.id,
|
||||
required this.commentCount,
|
||||
required this.images});
|
||||
|
||||
FriendicaEntry.fromJson(Map<String, dynamic> json)
|
||||
: originalJson = json,
|
||||
id = json['id'] ?? -1,
|
||||
text = json['text'] ?? '',
|
||||
commentCount = _commentCountFromJson(json),
|
||||
images = _imagesFromJson(json);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '''
|
||||
FriendicaPost{
|
||||
id: $id,
|
||||
text: $text,
|
||||
commentCount: $commentCount,
|
||||
images: $images,
|
||||
}
|
||||
''';
|
||||
}
|
||||
|
||||
static int _commentCountFromJson(Map<String, dynamic> json) {
|
||||
final readCount = json['friendica_comments'] ?? 0;
|
||||
final count = readCount is int ? readCount : int.tryParse(readCount) ?? 0;
|
||||
return count;
|
||||
}
|
||||
|
||||
static List<String> _imagesFromJson(Map<String, dynamic> json) {
|
||||
final List<dynamic> attachments = json['attachments'] ?? [];
|
||||
return attachments
|
||||
.where((a) => a['mimetype']?.startsWith('image') ?? false)
|
||||
.map((a) => a['url']?.toString() ?? '')
|
||||
.where((urlString) => urlString.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue