mastodon_vk_reposter/bin/server.dart

114 lines
2.8 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:dio/dio.dart' as dio_lib;
var dio = dio_lib.Dio();
// Load mastodon instance url from env
final instanceUrl = Platform.environment['INSTANCE_URL'];
// Load mastodon auth token from env
final authToken = Platform.environment['MASTODON_TOKEN'];
Future<void> _postOnMastodon(postText) async {
await dio.post('$instanceUrl/api/v1/statuses',
data: {'status': postText},
options:
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
}
/// Reads a file [fileUri] and adds posts to posts.json
///
/// If [fileUri] does not exist, do nothing.
/// [fileUri] is a plain text file where the posts are separated by empty lines.
/// posts.json is an array of posts
Future<void> _importPosts(fileUri) async {
if (!File(fileUri).existsSync()) {
return;
}
final file = File(fileUri);
final lines = await file.readAsLines();
final List<String> posts = [];
String post = '';
for (final line in lines) {
if (line.isEmpty && post != '') {
posts.add(post);
print('Added post: $post');
post = '';
} else {
post += '$line\n';
}
}
if (post != '') {
posts.add(post);
print('Added post: $post');
}
// Read posts.json and add posts to it
final postsJson = json.decode(await File('/data/posts.json').readAsString());
postsJson.addAll(posts);
// Write posts.json
await File('/data/posts.json').writeAsString(json.encode(postsJson));
// Delete file
await file.delete();
return;
}
/// Posts a random string from posts.json to mastodon
///
/// If posts.json does not exist, do nothing.
/// posts.json is an array of posts
/// If an array is empty, do nothing.
/// Delete posted post from posts.json
Future<void> _postRandomPost() async {
if (!File('/data/posts.json').existsSync()) {
return;
}
final postsJson = json.decode(await File('/data/posts.json').readAsString());
if (postsJson.isEmpty) {
return;
}
final post = postsJson.removeAt(Random().nextInt(postsJson.length));
await File('/data/posts.json').writeAsString(json.encode(postsJson));
await _postOnMastodon(post);
return;
}
void main(List<String> args) async {
// If posts.json does not exist, create it and write an empty array
if (!File('/data/posts.json').existsSync()) {
await File('/data/posts.json').writeAsString(json.encode([]));
}
// Import posts from files
await _importPosts('/data/posts.txt');
// Post a random post
await _postRandomPost();
// Post a random post at random time
// Random time is between 1 and 8 hours
// Random time is choosen after every post
while (true) {
await Future.delayed(Duration(hours: Random().nextInt(8) + 1));
await _importPosts('/data/posts.txt');
await _postRandomPost();
}
}