Skip to content
Snippets Groups Projects
Commit d9dffc3b authored by jooola's avatar jooola
Browse files

refactor: fix linting errors

Part-of: <!58>
parent cb8a09ed
No related branches found
No related tags found
1 merge request!58test: add flake linter
...@@ -134,7 +134,7 @@ async def crawl(domain, use_public, detail, passes, sort): ...@@ -134,7 +134,7 @@ async def crawl(domain, use_public, detail, passes, sort):
""" """
Crawl the network starting from the given domain(s). Crawl the network starting from the given domain(s).
""" """
from . import crawler, settings from . import crawler
kwargs = crawler.get_session_kwargs() kwargs = crawler.get_session_kwargs()
async with aiohttp.ClientSession(**kwargs) as session: async with aiohttp.ClientSession(**kwargs) as session:
......
import asyncio import asyncio
import logging
import sys import sys
import aiohttp import aiohttp
import aiopg import aiopg
import psycopg2 import psycopg2
from funkwhale_network import exceptions, schemas, serializers, settings from funkwhale_network import exceptions, schemas, settings
from funkwhale_network.db import DB from funkwhale_network.db import DB
logger = logging.getLogger(__name__)
def get_session_kwargs(): def get_session_kwargs():
headers = {"User-Agent": settings.CRAWLER_USER_AGENT} headers = {"User-Agent": settings.CRAWLER_USER_AGENT}
...@@ -67,12 +70,10 @@ async def crawl_all(session, *domains, stdout, max_passes): ...@@ -67,12 +70,10 @@ async def crawl_all(session, *domains, stdout, max_passes):
def print_pass(): def print_pass():
stdout( stdout(
"[Pass {pass_number}] {pending_domains} new domains to crawl, {handled_domains} checked, {valid_domains} valid".format( f"[Pass {data['pass_number']}] "
pass_number=data["pass_number"], f"{len(data['pending_domains'])} new domains to crawl, "
pending_domains=len(data["pending_domains"]), f"{len(data['handled_domains'])} checked, "
handled_domains=len(data["handled_domains"]), f"{len(data['valid_domains'])} valid"
valid_domains=len(data["valid_domains"]),
)
) )
while data["pending_domains"] and data["pass_number"] < max_passes: while data["pending_domains"] and data["pass_number"] < max_passes:
...@@ -94,8 +95,9 @@ async def crawl_single(session, domain, data): ...@@ -94,8 +95,9 @@ async def crawl_single(session, domain, data):
cleaned_data = clean_check( cleaned_data = clean_check(
{"domain": domain, "up": True}, clean_nodeinfo(nodeinfo_data) {"domain": domain, "up": True}, clean_nodeinfo(nodeinfo_data)
) )
except Exception as e: except Exception as exception:
data["invalid_domains"].add(domain) data["invalid_domains"].add(domain)
logger.debug(exception)
return return
finally: finally:
data["pending_domains"].remove(domain) data["pending_domains"].remove(domain)
...@@ -105,8 +107,8 @@ async def crawl_single(session, domain, data): ...@@ -105,8 +107,8 @@ async def crawl_single(session, domain, data):
if nodes_url: if nodes_url:
try: try:
await gather_known_nodes(session, nodes_url, data) await gather_known_nodes(session, nodes_url, data)
except: except Exception as exception:
pass logger.debug(exception)
data["valid_domains"].add(domain) data["valid_domains"].add(domain)
data["results"][domain] = cleaned_data data["results"][domain] = cleaned_data
...@@ -135,18 +137,18 @@ def recursive_getattr(obj, key, permissive=True): ...@@ -135,18 +137,18 @@ def recursive_getattr(obj, key, permissive=True):
If the value is not present, returns None If the value is not present, returns None
""" """
v = obj value = obj
for k in key.split("."): for k in key.split("."):
try: try:
v = v.get(k) value = value.get(k)
except (TypeError, AttributeError): except (TypeError, AttributeError):
if not permissive: if not permissive:
raise raise
return return
if v is None: if value is None:
return return
return v return value
def clean_check(check_data, nodeinfo_data): def clean_check(check_data, nodeinfo_data):
...@@ -217,7 +219,7 @@ async def save_check(data): ...@@ -217,7 +219,7 @@ async def save_check(data):
", ".join(fields), ", ".join(["%s" for _ in values]) ", ".join(fields), ", ".join(["%s" for _ in values])
) )
await cursor.execute(sql, values) await cursor.execute(sql, values)
check = await cursor.fetchone() obj = await cursor.fetchone()
if data.get("private") is True: if data.get("private") is True:
# let's clean previous checks # let's clean previous checks
...@@ -230,4 +232,4 @@ async def save_check(data): ...@@ -230,4 +232,4 @@ async def save_check(data):
[node_name, data["domain"]], [node_name, data["domain"]],
) )
return check return obj
...@@ -92,7 +92,11 @@ class DB: ...@@ -92,7 +92,11 @@ class DB:
async def get_latest_check_by_domain(self): async def get_latest_check_by_domain(self):
sql = """ sql = """
SELECT DISTINCT on (domain) domain, * FROM checks INNER JOIN domains ON checks.domain = domains.name WHERE private = %s AND domains.blocked = false ORDER BY domain, time DESC SELECT DISTINCT on (domain) domain, *
FROM checks
INNER JOIN domains ON checks.domain = domains.name
WHERE private = %s AND domains.blocked = false
ORDER BY domain, time DESC
""" """
with ( with (
await self.pool.cursor(cursor_factory=psycopg2.extras.RealDictCursor) await self.pool.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
...@@ -151,7 +155,13 @@ class DB: ...@@ -151,7 +155,13 @@ class DB:
return data return data
def get_domain_query(self, **kwargs): def get_domain_query(self, **kwargs):
base_query = "SELECT DISTINCT on (domain) domain, * FROM checks INNER JOIN domains ON checks.domain = domains.name WHERE domains.blocked = false ORDER BY domain, time DESC" base_query = """
SELECT DISTINCT on (domain) domain, *
FROM checks
INNER JOIN domains ON checks.domain = domains.name
WHERE domains.blocked = false
ORDER BY domain, time DESC
"""
return base_query.format(where_clause=""), [] return base_query.format(where_clause=""), []
async def get_all_domains(self): async def get_all_domains(self):
......
import json import json
import logging
import os import os
import urllib.parse import urllib.parse
...@@ -11,6 +12,9 @@ from funkwhale_network.db import DB ...@@ -11,6 +12,9 @@ from funkwhale_network.db import DB
from . import crawler, exceptions, serializers, settings from . import crawler, exceptions, serializers, settings
logger = logging.getLogger()
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_DIR = os.path.join(BASE_DIR, "static") STATIC_DIR = os.path.join(BASE_DIR, "static")
...@@ -92,7 +96,8 @@ async def domains(request): ...@@ -92,7 +96,8 @@ async def domains(request):
except ( except (
aiohttp.client_exceptions.ClientError, aiohttp.client_exceptions.ClientError,
exceptions.CrawlerError, exceptions.CrawlerError,
) as e: ) as exception:
logger.debug(exception)
return web.json_response( return web.json_response(
{"error": f"Invalid domain name {payload['name']}"}, status=400 {"error": f"Invalid domain name {payload['name']}"}, status=400
) )
......
...@@ -71,7 +71,8 @@ def serialize_domain_from_check(data): ...@@ -71,7 +71,8 @@ def serialize_domain_from_check(data):
def serialize_rss_feed_from_checks(checks): def serialize_rss_feed_from_checks(checks):
feed = [ feed = [
'<?xml version="1.0" encoding="UTF-8" ?>', '<?xml version="1.0" encoding="UTF-8" ?>',
'<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">', '<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" '
'xmlns:atom="http://www.w3.org/2005/Atom">',
" <channel>", " <channel>",
" <title>Latest Funkwhale pods</title>", " <title>Latest Funkwhale pods</title>",
" <link>https://network.funkwhale.audio/dashboards/</link>", " <link>https://network.funkwhale.audio/dashboards/</link>",
......
...@@ -23,7 +23,7 @@ async def on_shutdown(app): ...@@ -23,7 +23,7 @@ async def on_shutdown(app):
def initialize_sentry(): def initialize_sentry():
dsn = environ.Env()("FUNKWHALE_NETWORK_DSN", default=None) dsn = environ.Env()("FUNKWHALE_NETWORK_DSN", default=None)
if dsn == None: if dsn is None:
print("Sentry is not configured, skipping") print("Sentry is not configured, skipping")
return return
sentry_sdk.init( sentry_sdk.init(
......
import logging import logging
import traceback
from aiohttp import ClientSession from aiohttp import ClientSession
from arq.cron import cron from arq.cron import cron
...@@ -7,6 +6,8 @@ from arq.cron import cron ...@@ -7,6 +6,8 @@ from arq.cron import cron
from funkwhale_network import crawler, settings from funkwhale_network import crawler, settings
from funkwhale_network.db import DB from funkwhale_network.db import DB
logger = logging.getLogger(__name__)
async def poll(ctx, domain): async def poll(ctx, domain):
session: ClientSession = ctx["session"] session: ClientSession = ctx["session"]
...@@ -22,9 +23,8 @@ async def update_all(ctx): ...@@ -22,9 +23,8 @@ async def update_all(ctx):
print(f"Checking domain {domain}") print(f"Checking domain {domain}")
try: try:
await poll(ctx, domain) await poll(ctx, domain)
except Exception as e: except Exception as exception:
print("... couldn't load all information") logger.exception(f"couldn't load all information: {exception}")
logging.error(traceback.format_exc())
async def startup(ctx): async def startup(ctx):
......
import os # import os
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
import aiohttp import aiohttp
import aiopg
# import aiopg
import psycopg2 import psycopg2
import pytest import pytest
from aioresponses import aioresponses from aioresponses import aioresponses
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment