From 07722e276a0a1a3d17d44128b1f9441b4cb47c5c Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 21 Aug 2023 02:54:32 +0300 Subject: [PATCH] push the govno code --- .gitignore | 4 ++ main.py | 98 +++++++++++++++++++++++++++++++++++++++++++ mastoapi_get_token.sh | 26 ++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100755 main.py create mode 100755 mastoapi_get_token.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73115c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__ +*.pyc +secret.py +*.json diff --git a/main.py b/main.py new file mode 100755 index 0000000..56ac2fe --- /dev/null +++ b/main.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python + +import json +import os +import requests as r +import secret +from time import time +import traceback + +API_CALL = 'https://suya.place/api/v1/statuses' +API_HEADERS = { 'Authorization': 'Bearer %s' % (secret.TOKEN) } # API key here! +EXCEPTION_MESSAGE_FORMAT = '@a1ba\n%s' # send an exception text to my direct messages +KOTCRAB_API = 'https://kotcrab.com/progress/proxy/ccc?x=%d' + +def get_stats(): + data = r.get(KOTCRAB_API % int(time() * 1000)).json() + return data[0]['units'], data[1]['units'] + +def post(visibility, status): + resp = r.post(API_CALL, headers = API_HEADERS, json = { + 'visibility': visibility, + 'status' : status + }) + return resp.status_code == 200 + +def parse_stats(stats, ref): + data = {} + + # this stuff isn't documented by Kotcrab so let's be careful here + if stats.keys() != ref.keys(): + raise Exception + + for k in stats.keys(): + if ref[k]['name'] != stats[k]['name']: + raise Exception + all_pages = len(ref[k]['data']) + edited_pages = int(float(stats[k]['edited']) * 0.01 * all_pages) + + data[stats[k]['name']] = { 'edited': edited_pages, 'all': all_pages } + return data + +def dump_and_compare_data(new): + s = json.dumps(new, indent = '\t') + with open('last.json.new', 'w') as fd: + fd.write(s) + + try: + with open('last.json', 'r') as fd: + s = fd.read() + old = json.loads(s) + + for k in new: + if k not in old: + new[k]['update'] = True + continue + + new[k]['update'] = old[k] != new[k] + except OSError: + for k in new: + new[k]['update'] = False + + os.rename('last.json.new', 'last.json') + +def compose_text(data): + text = 'Today\'s CCC Editing Progress: \n' + + for k in data: + d = data[k] + mark = '' + if d['update']: + if d['edited'] == d['all']: + mark = '🎉' # my font can't render this emoji lul + else: + mark = '⚠️' + + percent = d['edited'] / float(d['all']) * 100.0 + + text += '%s: %.2f%% (%d/%d) %s\n' % (k, percent, d['edited'], d['all'], mark) + return text + +def main(): + stats, ref = get_stats() + stats = parse_stats(stats, ref) + dump_and_compare_data(stats) + + text = compose_text(stats) + print(text) + + # bots are forced to be unlisted on my instance anyway + post('unlisted', text) + +if __name__ == "__main__": + try: + main() + except Exception as e: + t = traceback.format_exc() + print(t) + post('direct', EXCEPTION_MESSAGE_FORMAT % t) diff --git a/mastoapi_get_token.sh b/mastoapi_get_token.sh new file mode 100755 index 0000000..6259767 --- /dev/null +++ b/mastoapi_get_token.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +SCOPES='read write follow' + +if [ "$#" = "1" ]; then + RESPONSE_APP=$(curl -XPOST -F 'client_name=DoYouSuckDicks' -F "redirect_uris=urn:ietf:wg:oauth:2.0:oob" -F "scopes=$SCOPES" -F 'website=https://example.org' https://$1/api/v1/apps) + + CLIENT_ID=$(echo $RESPONSE_APP | jq -r .client_id) + CLIENT_SECRET=$(echo $RESPONSE_APP | jq -r .client_secret) + + echo "Client id: $CLIENT_ID" + echo "Client secret: $CLIENT_SECRET" + echo "Now open this in browser https://$1/oauth/authorize?client_id=$CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=$(echo $SCOPES | sed s/\ /+/g)" + + echo "After you get the token, re-run this script: " + echo "$0 $1 $CLIENT_ID $CLIENT_SECRET " +elif [ "$#" = "4" ]; then + FINAL_RESPONSE=$(curl -X POST -F "client_id=$2" -F "client_secret=$3" -F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' -F "code=$4" -F 'grant_type=authorization_code' -F "scope=$SCOPES" https://$1/oauth/token) + + echo $FINAL_RESPONSE + + echo "Your token is $(echo $FINAL_RESPONSE | jq -r .access_token)" +else + echo "Usage: $0 " + exit 1 +fi