my eye

__init__.py

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
"""Analyze personal websites."""

import collections
import hashlib
import logging
import os
import pathlib
import subprocess
import time

import PIL
import requests
import web
import webagt
import webint_data
import webint_guests
import webint_jobs
import webint_owner
import webint_system
import whois
from reportlab.graphics import renderPM
from svglib.svglib import svg2rlg
from web import tx

from .utils import silos

logging.basicConfig(level=logging.DEBUG, filename="crawl.log", filemode="w", force=True)

app = web.application(
    __name__,
    args={
        "site": r"[a-z\d.-]+\.[a-z]+",
        "page": r".*",
    },
    model={
        "redirects": {
            "incoming": "TEXT UNIQUE NOT NULL",
            "outgoing": "TEXT NOT NULL",
        },
        "resources": {
            "url": "TEXT UNIQUE NOT NULL",
            "crawled": "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP",
            "details": "JSON NOT NULL",
        },
    },
    mounts=[
        webint_data.app,
        webint_guests.app,
        webint_owner.app,
        webint_jobs.app,
        webint_system.app,
    ],
)
sites_path = pathlib.Path("sites")
sites_path.mkdir(exist_ok=True)

agent = webagt.Agent("IndieWebRocksBot")
blocklist = ["accounts.google.com"]
ignored_rels = [
    "author",
    "bookmark",
    "canonical",
    "category",
    "contents",
    "home",
    "nofollow",
    "noreferrer",
    "noopener",
    "pingback",
    "profile",
    "shortcut",
    "shortlink",
    "syndication",
    "tag",
    "ugc",
]
social_network_rels = ["acquaintance", "colleague", "friend", "met"]


def refresh_page(url):
    try:
        response = agent.get(domain)
    except (requests.ConnectionError, requests.Timeout) as err:
        return {"status": "not responding", "error": str(err)}
    try:
        tx.db.insert(
            "resources",
            url=url,
            details={
                "metaverse": hashlib.sha256(domain.encode("utf-8")).hexdigest().upper(),
                "domain": {
                    "name": domain,
                    "suffix": domain_details.suffix,
                    "hsts": domain_details.in_hsts,
                },
            },
        )
        web.enqueue(query_whois, domain)
    except tx.db.IntegrityError:
        pass
    return


def refresh_domain(domain):
    """Fetch `domain` and store site details and related media."""
    if domain in blocklist or not webagt.uri(domain).suffix:
        logging.debug(f"skipping {domain}")
        return
    # TODO logging.debug("getting previous details..")  # for etag
    start = time.time()
    logging.debug("downloading HTML..")
    try:
        response = agent.get(domain)
    except (requests.ConnectionError, requests.Timeout) as err:
        return {"status": "not responding", "error": str(err)}
    if domain != response.url.host:
        try:
            tx.db.insert("redirects", incoming=domain, outgoing=response.url.host)
        except tx.db.IntegrityError:
            tx.db.update(
                "redirects",
                outgoing=response.url.host,
                where="incoming = ?",
                vals=[domain],
            )
        refresh_domain(response.url.host)
        return
    domain_details = webagt.uri(domain)
    try:
        tx.db.insert(
            "resources",
            url=domain,
            details={
                "metaverse": hashlib.sha256(domain.encode("utf-8")).hexdigest().upper(),
                "domain": {
                    "name": domain,
                    "suffix": domain_details.suffix,
                    "hsts": domain_details.in_hsts,
                },
            },
        )
        web.enqueue(query_whois, domain)
    except tx.db.IntegrityError:
        pass
    site_path = sites_path / domain
    site_path.mkdir(parents=True, exist_ok=True)

    web.enqueue(run_lighthouse, domain)
    web.enqueue(run_pa11y, domain)

    update_details = get_updater(domain)
    update_details(
        accessed=web.now().to_iso8601_string(),
        response={
            "status": response.status,
            "time": time.time() - start,
            "headers": dict(response.headers),
            "length": round(len(response.text) / 1000),
        },
    )
    logging.debug("parsing Microformats..")
    mf2json = response.mf2json
    rels = dict(mf2json["rels"])

    if authorization_endpoint := rels.pop("authorization_endpoint", None):
        indieauth_details = {"authorization_endpoint": authorization_endpoint}
        if token_endpoint := rels.pop("token_endpoint", None):
            indieauth_details["token_endpoint"] = token_endpoint
        update_details(indieauth=indieauth_details)
    if indieauth_metadata_endpoint := rels.pop("indieauth-metadata", None):
        web.enqueue(get_indieauth_metadata, domain, indieauth_metadata_endpoint[0])

    if search := rels.pop("search", None):
        web.enqueue(get_search_description, domain, search[0])

    if manifest := rels.pop("manifest", None):
        web.enqueue(get_manifest, domain, manifest[0])

    if hub_endpoint := rels.pop("hub", None):
        web.enqueue(
            get_websub_hub, domain, hub_endpoint[0], rels.pop("self", [domain])[0]
        )

    web.enqueue(get_activitypub, domain)

    card = response.card
    update_details(mf2json=mf2json, card=card, rels=rels)
    photo_url = rels.pop("apple-touch-icon", None)
    card_type = None
    if card:
        card_type = "person"
        if card_org := card.get("org"):
            if card["name"][0] == card_org[0]:
                card_type = "organization"
        if emails := card.get("email"):
            gravatars = {}
            for email in emails:
                email = email.removeprefix("mailto:")
                gravatars[email] = hashlib.md5(
                    email.strip().lower().encode("utf-8")
                ).hexdigest()
            # TODO SET `gravatars`
        if photo_urls := card.get("photo"):  # TODO move to on-demand like icon?
            if isinstance(photo_urls[0], dict):
                photo_url = photo_urls[0]["value"]
            else:
                photo_url = photo_urls[0]
    try:
        icon_url = rels.pop("icon")[0]
    except KeyError:
        icon_url = f"{domain}/favicon.ico"
    web.enqueue(get_media, domain, photo_url, icon_url)

    scripts = []
    for script in response.dom.select("script"):
        script_details = dict(script.element.attrib)
        script_details["content_length"] = len(script.text)
        script_details["text"] = script.text
        scripts.append(script_details)
    stylesheets = rels.pop("stylesheet", [])
    for stylesheet in response.dom.select("style"):
        stylesheets.append(
            {
                "content_length": len(stylesheet.text),
                "text": stylesheet.text,
            }
        )
    whostyle = rels.pop("whostyle", None)
    try:
        title = response.dom.select("title")[0].text
    except IndexError:
        title = ""
    update_details(
        scripts=scripts, stylesheets=stylesheets, whostyle=whostyle, title=title
    )

    for ignored_rel in ignored_rels:
        rels.pop(ignored_rel, None)
    social_network = {}
    for social_network_rel in social_network_rels:
        if people_rels := rels.pop(social_network_rel, None):
            social_network[social_network_rel] = people_rels
    logging.debug("determining reciprocal rel=me..")
    reciprocals = set()
    rel_me_silos = []
    for silo, silo_details in silos.items():
        if len(silo_details) == 3:
            rel_me_silos.append(silo_details[0])
    rel_mes = rels.pop("me", [])
    url = webagt.uri(domain)  # TODO XXX
    for me_url in rel_mes:
        if not me_url.startswith(("http", "https")):
            continue
        me_url = webagt.uri(me_url)
        logging.debug(f"  rel=me {me_url}")
        if (me_url.domain, me_url.suffix) == ("twitter", "com"):
            if "/" in me_url.path:
                continue
            twitter_id = me_url.path.split("/")[0]
            twitter_bearer = os.getenv("TWITTER")
            twitter_profile = agent.get(
                f"https://api.twitter.com/2/users"
                f"/by/username/{twitter_id}?user.fields=url",
                headers={"Authorization": f"Bearer {twitter_bearer}"},
            ).json["data"]
            if twitter_profile_url := twitter_profile.get("url", None):
                try:
                    recip_url = agent.get(twitter_profile_url).url
                except requests.Timeout:
                    continue
                if recip_url == url:
                    reciprocals.add(me_url.minimized)
        if (me_url.subdomain, me_url.domain, me_url.suffix) == (
            "en",
            "wikipedia",
            "org",
        ):
            wp_props = agent.get(me_url).mf2json["items"][0]["properties"]
            if wp_url := wp_props.get("url"):
                if wp_url[0] == url:
                    reciprocals.add(me_url.minimized)
        if me_url.host not in rel_me_silos:
            continue
        try:
            reverse_rel_mes = agent.get(me_url).mf2json["rels"]["me"]
        except KeyError:
            continue
        for reverse_rel_me in reverse_rel_mes:
            if webagt.uri(reverse_rel_me).minimized == url.minimized:
                reciprocals.add(me_url.minimized)
    update_details(
        social_network=social_network, reciprocals=list(reciprocals), rel_me=rel_mes
    )

    feed = response.feed
    alt_feed_urls = set()
    if not feed["items"]:
        try:
            alt_feed_urls = set(rels["home"]) & set(rels["alternate"])
        except KeyError:
            pass
    alternate_reprs = rels.pop("alternate", [])
    alternate_feeds = rels.pop("feed", [])
    if not feed["items"]:
        for alt_feed_url in alternate_reprs + alternate_feeds:
            try:
                feed = agent.get(alt_feed_url).feed
            except ValueError:  # XML feed
                pass
            finally:
                print("using", alt_feed_url)
    # rels.pop("alternate", None)
    for entry in feed["items"]:
        try:
            published = entry["published"]
            permalink = entry["url"]
            entry.pop("published-str")
        except KeyError:
            continue
        entry.pop("uid", None)
        # TODO refresh_page(permalink)
    update_details(feed=feed)

    # logging.debug("archiving to WARC..")
    # warc_file = site_path / "warc_output"
    # subprocess.run(
    #     [
    #         "wget",
    #         "-EHkpq",
    #         site,
    #         f"--warc-file={warc_file}",
    #         "--no-warc-compression",
    #         "--delete-after",
    #     ]
    # )

    logging.debug("calculating IndieMark score..")
    scores = [
        [(3, None)] * 10,
        [(3, None)] * 10,
        [(3, None)] * 10,
        [(3, None)] * 10,
        [(3, None)] * 10,
    ]

    # L1 Identity
    if card:
        if "icon" in rels:
            scores[0][0] = (0, "contact info and icon on home page")
        else:
            scores[0][0] = (1, "contact info but no icon on home page")
    else:
        scores[0][0] = (2, "no contact info on home page")

    # L1 Authentication
    if rel_mes:
        scores[0][1] = (
            1,
            "<code>rel=me</code>s found but none for GitHub or Twitter",
        )
        for rel_me in rel_mes:
            if rel_me.startswith(("https://github.com", "https://twitter.com/")):
                scores[0][1] = (
                    0,
                    "<code>rel=me</code>s found for GitHub and/or Twitter",
                )
                break
    else:
        scores[0][1] = (2, "no <code>rel=me</code>s found")

    # L1 Posts
    if feed["items"]:
        if len(feed["items"]) > 1:
            scores[0][2] = (0, "more than one post")
        else:
            scores[0][2] = (1, "only one post")
    else:
        scores[0][2] = (2, "no posts")

    # L1 Search
    # XXX if details["ddg"]:
    # XXX     scores[0][6] = (0, "your content was found on DuckDuckgo")
    # XXX else:
    # XXX     scores[0][6] = (
    # XXX         1,
    # XXX         "your content was <strong>not</strong> found on DuckDuckgo",
    # XXX     )

    # L1 Interactivity
    scores[0][8] = (0, "content is accessible (select/copy text/permalinks)")

    # L2 Identity
    scores[1][0] = (0, "you've linked to silo profiles")

    # L3 'h-card contact info and icon on homepage'
    # L3 'multiple post types'
    # L3 'POSSE'
    # L3 'Posting UI'
    # L3 'Next/Previus Navigation between posts'
    # L3 'Search box on your site'
    # L3 'Embeds/aggregation'
    # L3 'Web Actions'

    # L4 'Send Webmentions'
    # L4 'PubSubHubbub support'
    # L4 'Display Search Results on your site'
    # L4 'Display Reply Context'

    # L5 'Automatic Webmentions'
    # L5 'Handle Webmentions'
    # L5 'Display full content rich reply-contexts'
    # L5 'Search on your own search backend'
    # L5 'Multiple Reply Types'
    # L5 'Display Backfeed of Comments'

    update_details(scores=scores)
    # logging.debug("dumping details..")
    # details["stored"] = web.now().to_iso8601_string()
    web.dump(scores, path=site_path / "scores.json")
    logging.debug("generating scoreboard..")
    subprocess.run(["node", "../index.js", domain])


def get_updater(url):
    """Return an update function catered to `domain`."""

    def update_details(**kwargs):
        """Atomically update the resource's details with `kwargs`."""
        keys = ", ".join([f"'$.{key}', json(?)" for key in kwargs.keys()])
        tx.db.update(
            "resources",
            what=f"details = json_set(details, {keys})",
            where="url = ?",
            vals=[web.dump(v) for v in kwargs.values()] + [url],
        )

    return update_details


def query_whois(domain):
    """Update the creation date for the domain."""
    logging.debug("querying WHOIS")
    domain_created = whois.whois(domain)["creation_date"]
    if isinstance(domain_created, list):
        domain_created = domain_created[0]
    try:
        domain_created = domain_created.isoformat()
    except AttributeError:
        pass
    get_updater(domain)(**{"domain.created": domain_created})


def get_media(domain, photo_url, icon_url):
    """Download the representative photo for the domain."""
    site_path = sites_path / domain
    if photo_url:
        logging.debug("downloading representative photo..")
        filename = photo_url.rpartition("/")[2]
        suffix = filename.rpartition(".")[2]
        if not suffix:
            suffix = "jpg"
        original = site_path / f"photo.{suffix}"
        webagt.download(photo_url, original)
        final = site_path / "photo.png"
        if suffix != "png":
            if suffix == "svg":
                drawing = svg2rlg(original)
                renderPM.drawToFile(drawing, final, fmt="PNG")
            else:
                try:
                    image = PIL.Image.open(original)
                except PIL.UnidentifiedImageError:
                    pass
                else:
                    image.save(final)
    logging.debug("downloading iconography..")
    final = site_path / "icon.png"
    filename = icon_url.rpartition("/")[2]
    suffix = filename.rpartition(".")[2]
    original = site_path / f"icon.{suffix}"
    try:
        download = webagt.download(icon_url, original)
    except web.ConnectionError:
        pass
    else:
        if download.status == 200 and suffix != "png":
            try:
                image = PIL.Image.open(original)
            except PIL.UnidentifiedImageError:
                pass
            else:
                image.save(final)


def get_indieauth_metadata(domain, indieauth_metadata_endpoint):
    """Download IndieAuth metadata for the domain."""
    logging.debug("downloading IndieAuth metadata..")
    metadata = agent.get(indieauth_metadata_endpoint).json
    get_updater(domain)(**{"indieauth": {"metadata": metadata}})


def get_search_description(domain, search_url):
    """Download OpenSearch description document at `search_url`."""
    logging.debug("downloading OpenSearch description..")
    search_xml = agent.get(search_url).xml
    search_url = webagt.uri(search_xml.find("Url", search_xml.nsmap).attrib["template"])
    search_endpoint = f"//{search_url.host}/{search_url.path}"
    name = None
    for name, values in search_url.query.items():
        if values[0] == "{template}":
            break
    get_updater(domain)(**{"search_url": [search_endpoint, name]})


def get_manifest(domain, manifest_url):
    """Download site manifest at `manifest_url`."""
    logging.debug("downloading site manifest..")
    # if "patches" in web.get(manifest_url).headers:
    #     get_updater(domain)(**{"manifest": "hot"})
    webagt.download(manifest_url, sites_path / domain / "manifest.json")


def get_websub_hub(domain, endpoint, self):
    """Subscribe to site via WebSub `endpoint`."""
    # TODO subscribe if not already
    logging.debug("subscribing to WebSub hub..")
    get_updater(domain)(**{"hub": [endpoint, self]})


def run_lighthouse(domain):
    """Run lighthouse for the domain."""
    logging.debug("running lighthouse..")
    subprocess.Popen(
        [
            "lighthouse",
            f"https://{domain}",
            "--output=json",
            f"--output-path={sites_path}/{domain}/audits.json",
            "--only-audits=total-byte-weight",
            '--chrome-flags="--headless"',
            "--quiet",
        ],
        stdout=subprocess.PIPE,
    ).stdout.read()


def run_pa11y(domain):
    """Run pa11y for the domain."""
    site_path = sites_path / domain
    logging.debug("running pa11y..")
    web.dump(
        web.load(
            subprocess.Popen(
                [
                    "pa11y",
                    domain,
                    "--reporter",
                    "json",
                    "--screen-capture",
                    site_path / "site.png",
                ],
                stdout=subprocess.PIPE,
            ).stdout.read()
        ),
        path=site_path / "a11y.json",
    )

    found_icon = True  # TODO XXX
    logging.debug("finding most used color, generating images..")
    try:
        screenshot = PIL.Image.open(site_path / "site.png")
    except FileNotFoundError:
        pass
    else:
        screenshot.crop((0, 0, 1280, 1024)).save(site_path / "screenshot.png")
        colors = collections.Counter()
        for x in range(screenshot.width):
            for y in range(screenshot.height):
                colors[screenshot.getpixel((x, y))] += 1
        most_used_color = colors.most_common()[0][0]
        icon = PIL.Image.new("RGB", (1, 1), color=most_used_color)
        if not found_icon:
            icon.save(site_path / "icon.png")
        if not (site_path / "photo.png").exists():
            icon.save(site_path / "photo.png")


def get_activitypub(domain):
    webfinger = agent.get(f"https://{domain}/.well-known/webfinger")


@app.query
def get_categories(db):
    categories = collections.Counter()
    with db.transaction as cur:
        for post in cur.cur.execute(
            "select json_extract(resources.details, '$.category') "
            "AS categories from resources"
        ):
            if not post["categories"]:
                continue
            if post_categories := web.load(post["categories"]):
                for post_category in post_categories:
                    categories[post_category] += 1
    return categories


@app.query
def get_resources(db):
    return db.select(
        "resources",
        where="crawled > ?",
        vals=[web.now().subtract(days=7)],
        order="crawled DESC",
    )


@app.query
def get_posts(db):
    return []


@app.query
def get_people(db):
    return {
        url: details["card"]
        for url, details in tx.db.select(
            "resources", what="url, details", order="url ASC"
        )
    }


@app.query
def get_people_details(db):
    return tx.db.select("people", order="url ASC")


# @app._model.migrate(1)
# def add_redirects(db):
#     db.create("redirects", "incoming TEXT UNIQUE NOT NULL, outgoing TEXT NOT NULL")