aboutsummaryrefslogtreecommitdiffstats
path: root/fetch-status.py
blob: 89abc1e395ffe273c1bf48e7041fce38715807b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import re

from bs4 import BeautifulSoup
import requests

import os
import sqlite3
import time

print("Access-Control-Allow-Origin: *") # CORS shit, needed for styling iframes apparently

profile_id = os.environ['QUERY_STRING'].strip()

if not profile_id.isdigit():
	print("Status: 400 Bad Request")
	print("Content-Type: text/plain")
	print()

	current_url = "https://" if os.environ.get("HTTPS") else "http://"
	current_url += os.environ.get("HTTP_HOST") + os.environ.get("REQUEST_URI")

	if current_url.endswith("?"+profile_id):
		current_url = current_url[:-len(profile_id)-1]
	print(f"you have to put your user id as a query, like {current_url}?6498")
	quit()

con = sqlite3.connect(os.environ.get("AGORA_STATUS_DB"))
cur = con.cursor()

cur.execute("""
	CREATE TABLE IF NOT EXISTS statuses(
		profile_id INTEGER PRIMARY KEY UNIQUE,
		content TEXT,
		last_checked REAL,
		check_status TEXT
	)
""")
con.commit()

CACHE_TIMEOUT = 1800

cached_result = cur.execute("""SELECT content, check_status FROM statuses
	WHERE profile_id=? AND last_checked > ?""",
	(profile_id, time.time() - CACHE_TIMEOUT)
).fetchone()


if cached_result == None:
	headers = {
		'User-Agent': f'agora status fetcher - contact forum user "garret 427738" or https://427738.xyz/hate-mail.html - requested by {os.environ.get("REMOTE_ADDR")}',
	}

	profile_html = requests.get("https://forum.agoraroad.com/index.php?members/" + profile_id, headers=headers).text
	soup = BeautifulSoup(profile_html, 'lxml')

	posts = soup.find_all("div", class_="message-content js-messageContent")

	post_body = None

	data_lb_id_regex = re.compile(r"profile\-post\-\d+")

	for post in posts:
		posted_by_user = post.find("a", attrs={
			"class": 'username',
			"data-user-id": profile_id,
		}) != None
		if posted_by_user:
			post_body = str(post.find("div", attrs={
				"class": "lbContainer js-lbContainer",
				"data-lb-id": data_lb_id_regex,
			}))
			break

	is_loginwall = soup.find("html", class_='has-no-js template-login') != None
	is_cloudflare_block = soup.find("title").text == "Just a moment..."
else:
	post_body = cached_result[0]
	is_loginwall = cached_result[1] == "schizo"
	is_cloudflare_block = cached_result[1] == "cloudflare"

if post_body is None:
	print("Content-Type: text/plain; charset=utf-8")
	if is_loginwall:
		print("Status: 403")
		print()
		print("🚨🚨🚨 schizo alert! 🚨🚨🚨")
		print("it won't work if you don't expose your profile to the public, sorry")
	elif is_cloudflare_block:
		print("status: 403")
		print()
		print("blocked by cloudflare :(")
		print("i've found it to work from my home ip, probably this is hosted on a datacentre one...")
	else:
		print("Status: 500")
		print()
		print("it didn't work for some reason, possibly you've not made any posts on your profile")
else:
	print("Content-Type: text/html; charset=utf-8")
	print()
	print(post_body)

check_status = "success"
if is_loginwall:
	check_status = "schizo"
elif is_cloudflare_block:
	check_status = "cloudflare"
elif post_body is None:
	check_status = "mystery"

if not cached_result:
	cur.execute("INSERT OR REPLACE INTO statuses VALUES(?,?,?,?)", (profile_id, post_body, time.time(), check_status))
con.commit()