Add horny mode

This commit is contained in:
Inex Code 2022-06-18 14:29:09 +00:00
parent 3a3439a588
commit 80da74f0e1
3 changed files with 45 additions and 37 deletions

View File

@ -11,19 +11,23 @@ final instanceUrl = Platform.environment['INSTANCE_URL'];
// Load mastodon auth token from env // Load mastodon auth token from env
final authToken = Platform.environment['MASTODON_TOKEN']; final authToken = Platform.environment['MASTODON_TOKEN'];
Future<void> _postOnMastodon(postText) async { Future<void> _postOnMastodon(String postText, bool isHorny) async {
await dio.post('$instanceUrl/api/v1/statuses', await dio.post('$instanceUrl/api/v1/statuses',
data: {'status': postText}, data: {
options: 'status': postText,
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})); 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 /// Reads a file [fileUri] and adds posts to posts.json
/// ///
/// If [fileUri] does not exist, do nothing. /// If [fileUri] does not exist, do nothing.
/// [fileUri] is a plain text file where the posts are separated by empty lines. /// [fileUri] is a plain text file where the posts are separated by empty lines.
/// posts.json is an array of posts /// posts.json is an array of posts
Future<void> _importPosts(fileUri) async { Future<void> _importPosts(String fileUri, String outputFile) async {
if (!File(fileUri).existsSync()) { if (!File(fileUri).existsSync()) {
return; return;
} }
@ -50,21 +54,20 @@ Future<void> _importPosts(fileUri) async {
} }
// Read posts.json and add posts to it // 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); postsJson.addAll(posts);
// Write posts.json // Write posts.json
await File('/data/posts.json').writeAsString(json.encode(postsJson)); await File('/data/$outputFile').writeAsString(json.encode(postsJson));
// Delete file // Delete file
await file.delete(); await file.delete();
return; return;
} }
/// Posts a random string from posts.json to mastodon /// Posts a random string from posts.json to mastodon
/// ///
/// If posts.json does not exist, do nothing. /// If posts.json does not exist, do nothing.
/// posts.json is an array of posts /// posts.json is an array of posts
/// If an array is empty, do nothing. /// If an array is empty, do nothing.
@ -74,17 +77,35 @@ Future<void> _postRandomPost() async {
return; 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; 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; return;
} }
@ -96,7 +117,8 @@ void main(List<String> args) async {
} }
// Import posts from files // 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 // Post a random post
await _postRandomPost(); await _postRandomPost();
@ -105,9 +127,9 @@ void main(List<String> args) async {
// Random time is between 1 and 8 hours // Random time is between 1 and 8 hours
// Random time is choosen after every post // Random time is choosen after every post
while (true) { while (true) {
await Future.delayed(Duration(minutes: Random().nextInt(60*5) + 10)); await Future.delayed(Duration(minutes: Random().nextInt(60 * 5) + 10));
await _importPosts('/data/posts.txt'); await _importPosts('/data/posts.txt', 'posts.json');
await _importPosts('/data/horny.txt', 'horny.json');
await _postRandomPost(); await _postRandomPost();
} }
} }

View File

@ -106,13 +106,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.13.4" 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: http_multi_server:
dependency: transitive dependency: transitive
description: description:
@ -212,7 +205,7 @@ packages:
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
shelf: shelf:
dependency: "direct main" dependency: transitive
description: description:
name: shelf name: shelf
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
@ -225,13 +218,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.0.0" 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: shelf_static:
dependency: transitive dependency: transitive
description: description:

View File

@ -1,6 +1,6 @@
name: mastodon_vk_reposter name: neural_inex
description: A server app using the shelf package and Docker. description: Neural Inex poster
version: 1.0.0 version: 1.1.0
# homepage: https://www.example.com # homepage: https://www.example.com
environment: environment: