Skip to content

Instantly share code, notes, and snippets.

@georgyo
Created June 3, 2021 18:08
Show Gist options
  • Save georgyo/b042c1fb8eb1dc6520b517f73734ca0f to your computer and use it in GitHub Desktop.
Save georgyo/b042c1fb8eb1dc6520b517f73734ca0f to your computer and use it in GitHub Desktop.
hackerspaces.org matrix channel tester
#!/usr/bin/env python3
import codecs
import csv
import json
import logging
from urllib.parse import quote, urlencode
from urllib.request import urlopen
from urllib.error import HTTPError
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
utf8_stream_decoder = codecs.getreader("utf-8")
hackerspaces_matrix_csv_url = "https://wiki.hackerspaces.org/Special:Ask/format%3Dcsv/link%3Dall/headers%3Dshow/searchlabel%3DCSV/class%3Dsortable-20wikitable-20smwtable/sort%3D/order%3Dasc/offset%3D0/limit%3D500/-5B-5BMatrix::~*-23*-5D-5D/-3FMatrix/mainlabel%3D/prettyprint%3Dtrue/unescape%3Dtrue"
ALIAS_URL = "https://matrix.org/_matrix/client/r0/directory/room/{room_alias}"
ROOM_INFO = "https://matrix.org/_matrix/client/r0/directory/list/room/{room_id}"
def process_row(row):
data_alias = None
data_channel = None
try:
data_alias = json.load(
codecs.getreader("utf-8")(
urlopen(ALIAS_URL.format(room_alias=quote(row["Matrix"])))
)
)
except HTTPError as e:
logger.error(
f"Cloud not resolved {row['Matrix']} for hackerspace {row['']}. Got HTTP error: {e}"
)
try:
data_channel = json.load(
codecs.getreader("utf-8")(
urlopen(ROOM_INFO.format(room_id=quote(data_alias["room_id"])))
)
)
except HTTPError as e:
logger.error(
f"Cloud not resolved {row['Matrix']} for hackerspace {row['']}. Got HTTP error: {e}"
)
except TypeError:
pass
print(
{
"hackerspace": row[""],
"channel": row["Matrix"],
"visibility": data_channel["visibility"] if data_channel else "ERROR",
}
)
with urlopen(hackerspaces_matrix_csv_url) as f:
for row in csv.DictReader(codecs.getreader("utf-8")(f)):
process_row(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment