From 80da74f0e1020ee5ed16712ced69d7ba3ba7aecd Mon Sep 17 00:00:00 2001 From: Inex Code Date: Sat, 18 Jun 2022 14:29:09 +0000 Subject: [PATCH] Add horny mode --- bin/server.dart | 60 +++++++++++++++++++++++++++++++++---------------- pubspec.lock | 16 +------------ pubspec.yaml | 6 ++--- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/bin/server.dart b/bin/server.dart index 20ba2a5..f85dcb0 100644 --- a/bin/server.dart +++ b/bin/server.dart @@ -11,19 +11,23 @@ final instanceUrl = Platform.environment['INSTANCE_URL']; // Load mastodon auth token from env final authToken = Platform.environment['MASTODON_TOKEN']; -Future _postOnMastodon(postText) async { +Future _postOnMastodon(String postText, bool isHorny) async { await dio.post('$instanceUrl/api/v1/statuses', - data: {'status': postText}, - options: - dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})); + data: { + 'status': postText, + if (isHorny) 'sensitive': true, + if (isHorny) 'spoiler_text': 'NSFW' + }, + 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 _importPosts(fileUri) async { +Future _importPosts(String fileUri, String outputFile) async { if (!File(fileUri).existsSync()) { return; } @@ -50,21 +54,20 @@ Future _importPosts(fileUri) async { } // Read posts.json and add posts to it - final postsJson = json.decode(await File('/data/posts.json').readAsString()); + final postsJson = json.decode(await File('/data/$outputFile').readAsString()); postsJson.addAll(posts); // Write posts.json - await File('/data/posts.json').writeAsString(json.encode(postsJson)); + await File('/data/$outputFile').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. @@ -74,17 +77,35 @@ Future _postRandomPost() async { return; } - final postsJson = json.decode(await File('/data/posts.json').readAsString()); + bool isHorny = false; - if (postsJson.isEmpty) { + final postsJson = json.decode(await File('/data/posts.json').readAsString()); + final hornyPostsJson = + json.decode(await File('/data/horny.json').readAsString()); + + if (postsJson.isEmpty && hornyPostsJson.isEmpty) { return; } + if (postsJson.isEmpty) { + isHorny = true; + } else if (hornyPostsJson.isEmpty) { + isHorny = false; + } else { + // 30% chance of being horny + isHorny = Random().nextInt(3) == 0; + } - final post = postsJson.removeAt(Random().nextInt(postsJson.length)); + final post = isHorny + ? hornyPostsJson.removeAt(Random().nextInt(hornyPostsJson.length)) + : postsJson.removeAt(Random().nextInt(postsJson.length)); - await File('/data/posts.json').writeAsString(json.encode(postsJson)); + if (isHorny) { + await File('/data/horny.json').writeAsString(json.encode(hornyPostsJson)); + } else { + await File('/data/posts.json').writeAsString(json.encode(postsJson)); + } - await _postOnMastodon(post); + await _postOnMastodon(post, isHorny); return; } @@ -96,7 +117,8 @@ void main(List args) async { } // Import posts from files - await _importPosts('/data/posts.txt'); + await _importPosts('/data/posts.txt', 'posts.json'); + await _importPosts('/data/horny.txt', 'horny.json'); // Post a random post await _postRandomPost(); @@ -105,9 +127,9 @@ void main(List args) async { // Random time is between 1 and 8 hours // Random time is choosen after every post while (true) { - await Future.delayed(Duration(minutes: Random().nextInt(60*5) + 10)); - await _importPosts('/data/posts.txt'); + await Future.delayed(Duration(minutes: Random().nextInt(60 * 5) + 10)); + await _importPosts('/data/posts.txt', 'posts.json'); + await _importPosts('/data/horny.txt', 'horny.json'); await _postRandomPost(); } - } diff --git a/pubspec.lock b/pubspec.lock index 03943a0..305b033 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -106,13 +106,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.13.4" - http_methods: - dependency: transitive - description: - name: http_methods - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" http_multi_server: dependency: transitive description: @@ -212,7 +205,7 @@ packages: source: hosted version: "2.1.1" shelf: - dependency: "direct main" + dependency: transitive description: name: shelf url: "https://pub.dartlang.org" @@ -225,13 +218,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.0" - shelf_router: - dependency: "direct main" - description: - name: shelf_router - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.2" shelf_static: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 225f442..0ccbe3f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ -name: mastodon_vk_reposter -description: A server app using the shelf package and Docker. -version: 1.0.0 +name: neural_inex +description: Neural Inex poster +version: 1.1.0 # homepage: https://www.example.com environment: