Compare commits
9 Commits
master
...
neural-ine
Author | SHA1 | Date |
---|---|---|
Inex Code | b398d7761a | |
Inex Code | 80da74f0e1 | |
Inex Code | 3a3439a588 | |
Inex Code | 4ffb74614e | |
Inex Code | 6d5669c009 | |
Inex Code | 532b4eb6f1 | |
Inex Code | 94ac7cfcb6 | |
Inex Code | c81dfdf5c9 | |
Inex Code | d3750df342 |
272
bin/server.dart
272
bin/server.dart
|
@ -1,198 +1,142 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:math';
|
||||||
import 'package:shelf/shelf.dart';
|
|
||||||
import 'package:shelf/shelf_io.dart';
|
|
||||||
import 'package:shelf_router/shelf_router.dart';
|
|
||||||
|
|
||||||
import 'package:dio/dio.dart' as dio_lib;
|
import 'package:dio/dio.dart' as dio_lib;
|
||||||
|
|
||||||
var dio = dio_lib.Dio();
|
var dio = dio_lib.Dio();
|
||||||
|
|
||||||
// Load confirmation token from env
|
|
||||||
final confirmationToken = Platform.environment['CONFIRMATION_TOKEN'];
|
|
||||||
// Load secret key from env
|
|
||||||
final secretKey = Platform.environment['SECRET_KEY'];
|
|
||||||
// Load mastodon instance url from env
|
// Load mastodon instance url from env
|
||||||
final instanceUrl = Platform.environment['INSTANCE_URL'];
|
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'];
|
||||||
|
|
||||||
// Configure routes.
|
Future<void> _postOnMastodon(String postText, bool isHorny) async {
|
||||||
final _router = Router()
|
await dio.post('$instanceUrl/api/v1/statuses',
|
||||||
..get('/', _rootHandler)
|
data: {
|
||||||
..post('/vk', _vkHandler);
|
'status': postText,
|
||||||
|
if (isHorny) 'sensitive': true,
|
||||||
Response _rootHandler(Request req) {
|
if (isHorny) 'spoiler_text': 'NSFW'
|
||||||
return Response.ok('Hello, World!\n');
|
},
|
||||||
|
options:
|
||||||
|
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Response> _vkHandler(Request req) async {
|
/// Reads a file [fileUri] and adds posts to posts.json
|
||||||
// POST request should contain a JSON body.
|
///
|
||||||
// JSON must contain a 'type' field.
|
/// If [fileUri] does not exist, do nothing.
|
||||||
// The value of the 'type' field must be one of the following:
|
/// [fileUri] is a plain text file where the posts are separated by empty lines.
|
||||||
// 'confirmation'
|
/// posts.json is an array of posts
|
||||||
// 'wall_post_new'
|
Future<void> _importPosts(String fileUri, String outputFile) async {
|
||||||
// If we recieve a 'confirmation' request, we should respond with a confirmationToken.
|
if (!File(fileUri).existsSync()) {
|
||||||
// If we recieve a 'wall_post_new' request, we should validate if 'secret' field equals
|
return;
|
||||||
// secretKey and respond with an 'ok' string, printing request in the console.
|
}
|
||||||
|
|
||||||
final requestBody = await req.readAsString();
|
final file = File(fileUri);
|
||||||
final requestJson = json.decode(requestBody);
|
final lines = await file.readAsLines();
|
||||||
final requestType = requestJson['type'];
|
|
||||||
|
|
||||||
if (requestType == 'confirmation') {
|
final List<String> posts = [];
|
||||||
return Response.ok(confirmationToken);
|
String post = '';
|
||||||
} else if (requestType == 'wall_post_new') {
|
|
||||||
if (requestJson['secret'] == secretKey) {
|
for (final line in lines) {
|
||||||
print(requestBody);
|
if (line.isEmpty && post != '') {
|
||||||
_postOnMastodon(requestJson);
|
posts.add(post);
|
||||||
return Response.ok('ok');
|
print('Added post: $post');
|
||||||
|
post = '';
|
||||||
} else {
|
} else {
|
||||||
return Response.ok('invalid secret');
|
post += '$line\n';
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return Response.ok('invalid request type');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (post != '') {
|
||||||
|
posts.add(post);
|
||||||
|
print('Added post: $post');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read posts.json and add posts to it
|
||||||
|
final postsJson = json.decode(await File('/data/$outputFile').readAsString());
|
||||||
|
postsJson.addAll(posts);
|
||||||
|
|
||||||
|
// Write posts.json
|
||||||
|
await File('/data/$outputFile').writeAsString(json.encode(postsJson));
|
||||||
|
|
||||||
|
// Delete file
|
||||||
|
await file.delete();
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _postOnMastodon(reqJson) async {
|
/// Posts a random string from posts.json to mastodon
|
||||||
// Called to repost the post on Mastodon on vk.
|
///
|
||||||
// reqJson contains the 'object' field of the VK post.
|
/// If posts.json does not exist, do nothing.
|
||||||
// This object has the 'text' field with the post text.
|
/// posts.json is an array of posts
|
||||||
// Also it contains the 'attachments' field with the attachments.
|
/// If an array is empty, do nothing.
|
||||||
// The attachments field is an array of objects.
|
/// Delete posted post from posts.json
|
||||||
// Each object has the 'type' field with the attachment type.
|
Future<void> _postRandomPost() async {
|
||||||
// We only want to post photos.
|
if (!File('/data/posts.json').existsSync()) {
|
||||||
// The 'photo' type is 'photo'.
|
return;
|
||||||
// The 'photo' type has the 'sizes' field with an array of objects.
|
}
|
||||||
// Each object has the 'url', 'height' and 'width' fields.
|
if (!File('/data/horny.json').existsSync()) {
|
||||||
// We want to post the largest photo.
|
return;
|
||||||
// The 'url' field is the url of the photo.
|
|
||||||
// The 'height' and 'width' fields are the height and width of the photo.
|
|
||||||
// We download all pictures with dio into a temporary directory.
|
|
||||||
|
|
||||||
// Make sure post text is 500 characters or less.
|
|
||||||
String postText = reqJson['object']['text'];
|
|
||||||
if (postText.length > 500) {
|
|
||||||
postText.substring(0, 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final attachments = reqJson['object']['attachments'] ?? [];
|
bool isHorny = false;
|
||||||
|
|
||||||
var photos = attachments.where((attachment) => attachment['type'] == 'photo');
|
final postsJson = json.decode(await File('/data/posts.json').readAsString());
|
||||||
|
final hornyPostsJson =
|
||||||
|
json.decode(await File('/data/horny.json').readAsString());
|
||||||
|
|
||||||
List<String> photoUrls = [];
|
if (postsJson.isEmpty && hornyPostsJson.isEmpty) {
|
||||||
|
return;
|
||||||
for (final photo in photos) {
|
|
||||||
// Find the url of the largest photo by comparing the height and width.
|
|
||||||
var largestPhoto = photo['photo']['sizes'].reduce((a, b) {
|
|
||||||
if (a['height'] * a['width'] > b['height'] * b['width']) {
|
|
||||||
return a;
|
|
||||||
} else {
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
final photoUrl = largestPhoto['url'];
|
|
||||||
final photoFilename =
|
|
||||||
'${DateTime.now().millisecondsSinceEpoch}-${photoUrl.split('/').last.split('?').first.substring(0, 10)}.jpg';
|
|
||||||
final photoPath = '${Directory.systemTemp.path}/$photoFilename';
|
|
||||||
await dio.download(photoUrl, photoPath);
|
|
||||||
photoUrls.add(photoPath);
|
|
||||||
}
|
}
|
||||||
|
if (postsJson.isEmpty) {
|
||||||
// If there are more than four photos, move other photos to another array to be posted in the thread.
|
isHorny = true;
|
||||||
List<String> otherPhotos = [];
|
} else if (hornyPostsJson.isEmpty) {
|
||||||
if (photoUrls.length > 4) {
|
isHorny = false;
|
||||||
otherPhotos = photoUrls.sublist(4);
|
|
||||||
photoUrls = photoUrls.sublist(0, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
print('photoUrls: $photoUrls');
|
|
||||||
print('otherPhotos: $otherPhotos');
|
|
||||||
|
|
||||||
// Post the post on Mastodon.
|
|
||||||
// First we upload all photos with dio.
|
|
||||||
// Then we post the post with the uploaded photos and the text.
|
|
||||||
// We delete the photos after posting.
|
|
||||||
// Photos are uploaded to POST <instanceUrl>/api/v1/media in the 'file' form data field.
|
|
||||||
// We take the 'id' field of the response and add it to the post's 'media_ids' field.
|
|
||||||
// The 'media_ids' field is an array of media ids.
|
|
||||||
// We post the post with the media ids and the text in the 'status' field.
|
|
||||||
// Endpoint: POST <instanceUrl>/api/v1/statuses
|
|
||||||
// Headers: Authorization: Bearer <authToken>
|
|
||||||
// We use auth on all requests.
|
|
||||||
|
|
||||||
// if there are no photos, we don't fetch them
|
|
||||||
if (photos.isEmpty) {
|
|
||||||
await dio.post('$instanceUrl/api/v1/statuses',
|
|
||||||
data: {'status': postText},
|
|
||||||
options:
|
|
||||||
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
|
||||||
} else {
|
} else {
|
||||||
var mediaIds = [];
|
// 30% chance of being horny
|
||||||
|
isHorny = Random().nextInt(3) == 0;
|
||||||
for (final photoUrl in photoUrls) {
|
|
||||||
final resp = await dio.post('$instanceUrl/api/v1/media',
|
|
||||||
data: dio_lib.FormData.fromMap(
|
|
||||||
{'file': dio_lib.MultipartFile.fromFileSync(photoUrl)}),
|
|
||||||
options:
|
|
||||||
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
|
||||||
mediaIds.add(resp.data['id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
var postResponse = await dio.post('$instanceUrl/api/v1/statuses',
|
|
||||||
data: {'status': postText, 'media_ids': mediaIds},
|
|
||||||
options:
|
|
||||||
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
|
||||||
|
|
||||||
await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete()));
|
|
||||||
|
|
||||||
// If there are other photos, post them in a thread.
|
|
||||||
while (otherPhotos.isNotEmpty) {
|
|
||||||
mediaIds = [];
|
|
||||||
if (otherPhotos.length > 4) {
|
|
||||||
otherPhotos = photoUrls.sublist(4);
|
|
||||||
photoUrls = photoUrls.sublist(0, 4);
|
|
||||||
} else {
|
|
||||||
photoUrls = otherPhotos;
|
|
||||||
otherPhotos = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
print('photoUrls: $photoUrls');
|
|
||||||
print('otherPhotos: $otherPhotos');
|
|
||||||
|
|
||||||
for (final photoUrl in photoUrls) {
|
|
||||||
final resp = await dio.post('$instanceUrl/api/v1/media',
|
|
||||||
data: dio_lib.FormData.fromMap(
|
|
||||||
{'file': dio_lib.MultipartFile.fromFileSync(photoUrl)}),
|
|
||||||
options: dio_lib.Options(
|
|
||||||
headers: {'Authorization': 'Bearer $authToken'}));
|
|
||||||
mediaIds.add(resp.data['id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
postResponse = await dio.post('$instanceUrl/api/v1/statuses',
|
|
||||||
data: {
|
|
||||||
'in_reply_to_id': postResponse.data['id'],
|
|
||||||
'media_ids': mediaIds
|
|
||||||
},
|
|
||||||
options:
|
|
||||||
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
|
||||||
|
|
||||||
await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final post = isHorny
|
||||||
|
? hornyPostsJson.removeAt(Random().nextInt(hornyPostsJson.length))
|
||||||
|
: postsJson.removeAt(Random().nextInt(postsJson.length));
|
||||||
|
|
||||||
|
if (isHorny) {
|
||||||
|
await File('/data/horny.json').writeAsString(json.encode(hornyPostsJson));
|
||||||
|
} else {
|
||||||
|
await File('/data/posts.json').writeAsString(json.encode(postsJson));
|
||||||
|
}
|
||||||
|
|
||||||
|
await _postOnMastodon(post, isHorny);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void main(List<String> args) async {
|
void main(List<String> args) async {
|
||||||
// Use any available host or container IP (usually `0.0.0.0`).
|
// If posts.json does not exist, create it and write an empty array
|
||||||
final ip = InternetAddress.anyIPv4;
|
if (!File('/data/posts.json').existsSync()) {
|
||||||
|
await File('/data/posts.json').writeAsString(json.encode([]));
|
||||||
|
}
|
||||||
|
|
||||||
// Configure a pipeline that logs requests.
|
if (!File('/data/horny.json').existsSync()) {
|
||||||
final _handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);
|
await File('/data/horny.json').writeAsString(json.encode([]));
|
||||||
|
}
|
||||||
|
|
||||||
// For running in containers, we respect the PORT environment variable.
|
// Import posts from files
|
||||||
final port = int.parse(Platform.environment['PORT'] ?? '8080');
|
await _importPosts('/data/posts.txt', 'posts.json');
|
||||||
final server = await serve(_handler, ip, port);
|
await _importPosts('/data/horny.txt', 'horny.json');
|
||||||
print('Server listening on port ${server.port}');
|
|
||||||
|
// Post a random post
|
||||||
|
await _postRandomPost();
|
||||||
|
|
||||||
|
// Post a random post at random time
|
||||||
|
// 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', 'posts.json');
|
||||||
|
await _importPosts('/data/horny.txt', 'horny.json');
|
||||||
|
await _postRandomPost();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
16
pubspec.lock
16
pubspec.lock
|
@ -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:
|
||||||
|
|
|
@ -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:
|
||||||
|
@ -10,8 +10,6 @@ environment:
|
||||||
dependencies:
|
dependencies:
|
||||||
args: ^2.0.0
|
args: ^2.0.0
|
||||||
dio: ^4.0.6
|
dio: ^4.0.6
|
||||||
shelf: ^1.1.0
|
|
||||||
shelf_router: ^1.0.0
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
http: ^0.13.0
|
http: ^0.13.0
|
||||||
|
|
Loading…
Reference in New Issue