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
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',
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<void> _importPosts(fileUri) async {
Future<void> _importPosts(String fileUri, String outputFile) async {
if (!File(fileUri).existsSync()) {
return;
}
@ -50,21 +54,20 @@ Future<void> _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<void> _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<String> 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<String> 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();
}
}

View File

@ -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:

View File

@ -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: