mastodon_vk_reposter/bin/server.dart

143 lines
3.8 KiB
Dart
Raw Normal View History

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-06-18 14:29:09 +00:00
Future<void> _postOnMastodon(String postText, bool isHorny) async {
2022-05-05 19:37:22 +00:00
await dio.post('$instanceUrl/api/v1/statuses',
2022-06-18 14:29:09 +00:00
data: {
'status': postText,
if (isHorny) 'sensitive': true,
if (isHorny) 'spoiler_text': 'NSFW'
},
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
2022-06-18 14:29:09 +00:00
///
2022-05-05 19:37:22 +00:00
/// 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
2022-06-18 14:29:09 +00:00
Future<void> _importPosts(String fileUri, String outputFile) async {
2022-05-05 19:37:22 +00:00
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-05-05 19:51:02 +00:00
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-06-18 14:29:09 +00:00
final postsJson = json.decode(await File('/data/$outputFile').readAsString());
2022-05-05 19:37:22 +00:00
postsJson.addAll(posts);
// Write posts.json
2022-06-18 14:29:09 +00:00
await File('/data/$outputFile').writeAsString(json.encode(postsJson));
2022-05-05 19:37:22 +00:00
// Delete file
await file.delete();
return;
}
/// Posts a random string from posts.json to mastodon
2022-06-18 14:29:09 +00:00
///
2022-05-05 19:37:22 +00:00
/// 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 {
2022-05-05 19:54:18 +00:00
if (!File('/data/posts.json').existsSync()) {
2022-05-05 19:37:22 +00:00
return;
2022-04-21 07:12:15 +00:00
}
2022-06-18 16:25:46 +00:00
if (!File('/data/horny.json').existsSync()) {
return;
}
2022-04-21 06:37:29 +00:00
2022-06-18 14:29:09 +00:00
bool isHorny = false;
2022-05-05 19:54:18 +00:00
final postsJson = json.decode(await File('/data/posts.json').readAsString());
2022-06-18 14:29:09 +00:00
final hornyPostsJson =
json.decode(await File('/data/horny.json').readAsString());
2022-05-05 19:37:22 +00:00
2022-06-18 14:29:09 +00:00
if (postsJson.isEmpty && hornyPostsJson.isEmpty) {
2022-05-05 19:37:22 +00:00
return;
2022-04-21 07:23:51 +00:00
}
2022-06-18 14:29:09 +00:00
if (postsJson.isEmpty) {
isHorny = true;
} else if (hornyPostsJson.isEmpty) {
isHorny = false;
} else {
// 30% chance of being horny
isHorny = Random().nextInt(3) == 0;
}
2022-04-21 07:23:51 +00:00
2022-06-18 14:29:09 +00:00
final post = isHorny
? hornyPostsJson.removeAt(Random().nextInt(hornyPostsJson.length))
: postsJson.removeAt(Random().nextInt(postsJson.length));
2022-04-21 06:37:29 +00:00
2022-06-18 14:29:09 +00:00
if (isHorny) {
await File('/data/horny.json').writeAsString(json.encode(hornyPostsJson));
} else {
await File('/data/posts.json').writeAsString(json.encode(postsJson));
}
2022-04-21 06:37:29 +00:00
2022-06-18 14:29:09 +00:00
await _postOnMastodon(post, isHorny);
2022-05-05 19:37:22 +00:00
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
2022-05-05 19:54:18 +00:00
if (!File('/data/posts.json').existsSync()) {
await File('/data/posts.json').writeAsString(json.encode([]));
2022-05-05 19:37:22 +00:00
}
2022-06-18 16:25:46 +00:00
if (!File('/data/horny.json').existsSync()) {
await File('/data/horny.json').writeAsString(json.encode([]));
}
2022-05-05 19:37:22 +00:00
// Import posts from files
2022-06-18 14:29:09 +00:00
await _importPosts('/data/posts.txt', 'posts.json');
await _importPosts('/data/horny.txt', 'horny.json');
2022-04-21 05:59:37 +00:00
2022-05-05 19:37:22 +00:00
// Post a random post
2022-05-05 19:53:14 +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:53:14 +00:00
while (true) {
2022-06-18 14:29:09 +00:00
await Future.delayed(Duration(minutes: Random().nextInt(60 * 5) + 10));
await _importPosts('/data/posts.txt', 'posts.json');
await _importPosts('/data/horny.txt', 'horny.json');
2022-05-05 19:53:14 +00:00
await _postRandomPost();
}
2022-04-21 05:59:37 +00:00
}