2022-04-21 05:59:37 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
2022-05-05 19:37:22 +00:00
|
|
|
import 'dart:math';
|
2022-04-21 05:59:37 +00:00
|
|
|
|
2022-04-21 06:37:29 +00:00
|
|
|
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'];
|
2022-04-21 05:59:37 +00:00
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
Future<void> _postOnMastodon(postText) async {
|
|
|
|
await dio.post('$instanceUrl/api/v1/statuses',
|
|
|
|
data: {'status': postText},
|
|
|
|
options:
|
|
|
|
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
2022-04-21 05:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
/// 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);
|
2022-05-05 19:49:41 +00:00
|
|
|
print('Added post: $post');
|
2022-04-21 05:59:37 +00:00
|
|
|
} else {
|
2022-05-05 19:37:22 +00:00
|
|
|
post += '$line\n';
|
2022-04-21 05:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
if (post != '') {
|
|
|
|
posts.add(post);
|
2022-05-05 19:49:41 +00:00
|
|
|
print('Added post: $post');
|
2022-04-21 07:21:05 +00:00
|
|
|
}
|
2022-04-21 06:37:29 +00:00
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
// Read posts.json and add posts to it
|
2022-05-05 19:44:43 +00:00
|
|
|
final postsJson = json.decode(await File('posts.json').readAsString());
|
2022-05-05 19:37:22 +00:00
|
|
|
postsJson.addAll(posts);
|
|
|
|
|
|
|
|
// Write posts.json
|
|
|
|
await File('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('posts.json').existsSync()) {
|
|
|
|
return;
|
2022-04-21 07:12:15 +00:00
|
|
|
}
|
2022-04-21 06:37:29 +00:00
|
|
|
|
2022-05-05 19:44:43 +00:00
|
|
|
final postsJson = json.decode(await File('posts.json').readAsString());
|
2022-05-05 19:37:22 +00:00
|
|
|
|
|
|
|
if (postsJson.isEmpty) {
|
|
|
|
return;
|
2022-04-21 07:23:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
final post = postsJson.removeAt(Random().nextInt(postsJson.length));
|
2022-04-21 06:37:29 +00:00
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
await File('posts.json').writeAsString(json.encode(postsJson));
|
2022-04-21 06:37:29 +00:00
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
await _postOnMastodon(post);
|
|
|
|
|
|
|
|
return;
|
2022-04-21 06:37:29 +00:00
|
|
|
}
|
2022-04-21 05:59:37 +00:00
|
|
|
|
|
|
|
void main(List<String> args) async {
|
2022-05-05 19:37:22 +00:00
|
|
|
// If posts.json does not exist, create it and write an empty array
|
|
|
|
if (!File('posts.json').existsSync()) {
|
|
|
|
await File('posts.json').writeAsString(json.encode([]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import posts from files
|
2022-05-05 19:44:43 +00:00
|
|
|
await _importPosts('/data/posts.txt');
|
2022-04-21 05:59:37 +00:00
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
// Post a random post
|
2022-05-05 19:49:41 +00:00
|
|
|
// await _postRandomPost();
|
2022-04-21 05:59:37 +00:00
|
|
|
|
2022-05-05 19:37:22 +00:00
|
|
|
// Post a random post at random time
|
|
|
|
// Random time is between 1 and 8 hours
|
|
|
|
// Random time is choosen after every post
|
2022-05-05 19:49:41 +00:00
|
|
|
// while (true) {
|
|
|
|
// await Future.delayed(Duration(hours: Random().nextInt(8) + 1));
|
|
|
|
// await _postRandomPost();
|
|
|
|
// }
|
2022-05-05 19:37:22 +00:00
|
|
|
|
2022-04-21 05:59:37 +00:00
|
|
|
}
|