Created
September 27, 2021 17:18
-
-
Save BLKKKBVSIK/35c551c104d46ca47cd6e66fb4a0337e to your computer and use it in GitHub Desktop.
🚧 WIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:convert'; | |
import 'dart:typed_data'; | |
const kDISCORD_WEBHOOK = "YOUR_KEY_HERE"; | |
const kIMDB_API_KEY = "YOUR_KEY_HERE"; | |
void main() async { | |
var server = await HttpServer.bind('127.0.0.1', 9000); | |
print("[Debug] Http server opened on http://127.0.0.1:9000/"); | |
await for (HttpRequest r in server) { | |
if (r.method == 'POST') { | |
final currentEvent = | |
PlexPayload.fromJson(json.decode(await utf8.decodeStream(r))); | |
print(currentEvent.title); | |
r.response.close(); | |
HttpClient httpClient = HttpClient(); | |
HttpClientRequest request = | |
await httpClient.postUrl(Uri.tryParse(kDISCORD_WEBHOOK)!); | |
request.headers.contentType = | |
new ContentType("application", "json", charset: "utf-8"); | |
request.write( | |
createJsonForDiscordWebhook( | |
audianceRating: currentEvent.audienceRating, | |
title: currentEvent.title, | |
summary: currentEvent.summary, | |
), | |
); | |
request.close(); | |
} else { | |
r.response | |
..headers.set('Content-Type', 'text/html') | |
..write(''' | |
<!doctype html> | |
<html> | |
<body> | |
<p>Hello! Nothing to see there</p> | |
</body> | |
</html> | |
''') | |
..close(); | |
} | |
} | |
} | |
class PlexPayload { | |
final String? event; | |
final String? title; | |
final String? grandparentTitle; | |
final String? parentTitle; | |
final String? summary; | |
final double audienceRating; | |
const PlexPayload({ | |
this.event, | |
this.title, | |
this.grandparentTitle, | |
this.parentTitle, | |
this.summary, | |
required this.audienceRating, | |
}); | |
factory PlexPayload.fromJson(Map<String, dynamic> json) => | |
_$PlexPayloadFromJson(json); | |
@override | |
List<Object?> get props => | |
[event, title, grandparentTitle, parentTitle, summary, audienceRating]; | |
} | |
PlexPayload _$PlexPayloadFromJson(Map<String, dynamic> json) { | |
return PlexPayload( | |
event: json['event'] as String?, | |
title: json['Metadata']['title'] as String?, | |
grandparentTitle: json['Metadata']['grandparentTitle'] as String?, | |
parentTitle: json['Metadata']['parentTitle'] as String?, | |
summary: json['Metadata']['summary'] as String?, | |
audienceRating: json['Metadata']['audienceRating'] ?? 0.0, | |
); | |
} | |
String createJsonForDiscordWebhook( | |
{String? title, String? summary, required double audianceRating}) { | |
return ''' | |
{ | |
"username": "", | |
"avatar_url": "", | |
"content": "", | |
"embeds": [ | |
{ | |
"title": "There is a new X on the PleX", | |
"color": 0, | |
"description": "$summary", | |
"timestamp": "", | |
"author": {}, | |
"image": {}, | |
"thumbnail": {}, | |
"footer": { | |
"text": "Audience rating: ${audianceRating.toString()}/10" | |
}, | |
"fields": [ | |
{ | |
"name": "Title", | |
"value": "$title" | |
}, | |
{ | |
"name": "Serie Title", | |
"value": "Ooooo" | |
} | |
] | |
} | |
], | |
"components": [] | |
} | |
'''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment