import sys
import tempfile
import zc.lockfile
+
from fba import commands
from fba import fba
}
try:
- raw = fba.fetch_url("https://raw.githubusercontent.com/chaossocial/meta/master/federation.md", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text
+ raw = fba.fetch_url("https://raw.githubusercontent.com/chaossocial/meta/master/federation.md", network.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text
# DEBUG: print(f"DEBUG: raw()={len(raw)}[]={type(raw)}")
doc = bs4.BeautifulSoup(markdown.markdown(raw, extensions=extensions), features='html.parser')
try:
print(f"INFO: Fetch FBA-specific RSS args.feed='{args.feed}' ...")
- response = fba.fetch_url(args.feed, fba.headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = fba.fetch_url(args.feed, network.headers, (config.get("connection_timeout"), config.get("read_timeout")))
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',response.text()={len(response.text)}")
if response.ok and response.status_code < 300 and len(response.text) > 0:
domains = list()
try:
print(f"INFO: Fetching ATOM feed='{feed}' from FBA bot account ...")
- response = fba.fetch_url(feed, fba.headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = fba.fetch_url(feed, network.headers, (config.get("connection_timeout"), config.get("read_timeout")))
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',response.text()={len(response.text)}")
if response.ok and response.status_code < 300 and len(response.text) > 0:
boot.acquire_lock()
# Fetch this URL
- response = fba.fetch_url("https://github.com/federater/blocks_recommended/raw/main/federater.csv", fba.headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = fba.fetch_url("https://github.com/federater/blocks_recommended/raw/main/federater.csv", network.headers, (config.get("connection_timeout"), config.get("read_timeout")))
# DEBUG: print(f"DEBUG: response[]='{type(response)}'")
if response.ok and response.content != "":
# DEBUG: print(f"DEBUG: Fetched {len(response.content)} Bytes, parsing CSV ...")
"http://nodeinfo.diaspora.software/ns/schema/1.0",
]
-# HTTP headers for non-API requests
-headers = {
- "User-Agent": config.get("useragent"),
-}
-
-# HTTP headers for API requests
-api_headers = {
- "User-Agent": config.get("useragent"),
- "Content-Type": "application/json",
-}
-
# Connect to database
connection = sqlite3.connect("blocks.db")
cursor = connection.cursor()
# DEBUG: print(f"DEBUG: Fetching peers from '{domain}',software='{software}' ...")
peers = list()
try:
- response = network.fetch_response(domain, "/api/v1/instance/peers", api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = network.fetch_response(domain, "/api/v1/instance/peers", network.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
data = json_from_response(response)
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},data[]='{type(data)}'")
if not response.ok or response.status_code >= 400:
# DEBUG: print(f"DEBUG: Was not able to fetch peers, trying alternative ...")
- response = network.fetch_response(domain, "/api/v3/site", api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = network.fetch_response(domain, "/api/v3/site", network.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
data = json_from_response(response)
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},data[]='{type(data)}'")
try:
# DEBUG: print(f"DEBUG: Fetching request='{request}' from domain='{domain}' ...")
- response = network.fetch_response(domain, request, api_headers, (config.get("nodeinfo_connection_timeout"), config.get("nodeinfo_read_timeout")))
+ response = network.fetch_response(domain, request, network.api_headers, (config.get("nodeinfo_connection_timeout"), config.get("nodeinfo_read_timeout")))
data = json_from_response(response)
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},data[]='{type(data)}'")
data = {}
try:
- response = network.fetch_response(domain, "/.well-known/nodeinfo", api_headers, (config.get("nodeinfo_connection_timeout"), config.get("nodeinfo_read_timeout")))
+ response = network.fetch_response(domain, "/.well-known/nodeinfo", network.api_headers, (config.get("nodeinfo_connection_timeout"), config.get("nodeinfo_read_timeout")))
data = json_from_response(response)
# DEBUG: print("DEBUG: domain,response.ok,data[]:", domain, response.ok, type(data))
# DEBUG: print("DEBUG: rel,href:", link["rel"], link["href"])
if link["rel"] in nodeinfo_identifier:
# DEBUG: print("DEBUG: Fetching nodeinfo from:", link["href"])
- response = fetch_url(link["href"], api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = fetch_url(link["href"], network.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
data = json_from_response(response)
# DEBUG: print("DEBUG: href,response.ok,response.status_code:", link["href"], response.ok, response.status_code)
try:
# DEBUG: print(f"DEBUG: Fetching path='{path}' from '{domain}' ...")
- response = network.fetch_response(domain, path, headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = network.fetch_response(domain, path, network.headers, (config.get("connection_timeout"), config.get("read_timeout")))
# DEBUG: print("DEBUG: domain,response.ok,response.status_code,response.text[]:", domain, response.ok, response.status_code, type(response.text))
if response.ok and response.status_code < 300 and len(response.text) > 0:
peers = list()
try:
# DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
- response = network.fetch_response(domain, "/api/v3/site", fba.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = network.fetch_response(domain, "/api/v3/site", network.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
data = fba.json_from_response(response)
try:
doc = bs4.BeautifulSoup(
- network.fetch_response(domain, "/about/more", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
+ network.fetch_response(domain, "/about/more", network.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
"html.parser",
)
except BaseException as exception:
# handling CSRF, I've saw at least one server requiring it to access the endpoint
# DEBUG: print("DEBUG: Fetching meta:", domain)
meta = bs4.BeautifulSoup(
- network.fetch_response(domain, "/", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
+ network.fetch_response(domain, "/", network.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
"html.parser",
)
try:
csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
# DEBUG: print("DEBUG: Adding CSRF token:", domain, csrf)
- reqheaders = {**fba.api_headers, **{"X-CSRF-Token": csrf}}
+ reqheaders = {**network.api_headers, **{"X-CSRF-Token": csrf}}
except BaseException as exception:
# DEBUG: print("DEBUG: No CSRF token found, using normal headers:", domain, exception)
- reqheaders = fba.api_headers
+ reqheaders = network.api_headers
# DEBUG: print("DEBUG: Querying API domain_blocks:", domain)
blocklist = network.fetch_response(domain, "/api/v1/instance/domain_blocks", reqheaders, (config.get("connection_timeout"), config.get("read_timeout"))).json()
# DEBUG: print(f"DEBUG: domain='{domain}',mode='{mode}'")
while True:
try:
- response = network.fetch_response(domain, "/api/v1/server/{mode}?start={start}&count=100", headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = network.fetch_response(domain, "/api/v1/server/{mode}?start={start}&count=100", network.headers, (config.get("connection_timeout"), config.get("read_timeout")))
data = fba.json_from_response(response)
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',data[]='{type(data)}'")
import requests
from fba import config
+from fba import fba
from fba import instances
+# HTTP headers for non-API requests
+headers = {
+ "User-Agent": config.get("useragent"),
+}
+
+# HTTP headers for API requests
+api_headers = {
+ "User-Agent" : config.get("useragent"),
+ "Content-Type": "application/json",
+}
+
def post_json_api(domain: str, path: str, parameter: str, extra_headers: dict = {}) -> dict:
# DEBUG: print(f"DEBUG: domain='{domain}',path='{path}',parameter='{parameter}',extra_headers()={len(extra_headers)} - CALLED!")
if not isinstance(domain, str):
timeout=(config.get("connection_timeout"), config.get("read_timeout"))
)
- data = json_from_response(response)
+ data = fba.json_from_response(response)
# DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},data[]='{type(data)}'")
if not response.ok or response.status_code >= 400:
print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},response.status_code='{response.status_code}',data[]='{type(data)}'")