|
12 | 12 | import dns.resolver |
13 | 13 | import requests |
14 | 14 | import werkzeug.wrappers |
| 15 | +import user_agents |
15 | 16 |
|
16 | 17 | from flask import Blueprint, render_template, send_file, make_response, url_for, abort, session, \ |
17 | 18 | redirect, request |
@@ -564,31 +565,33 @@ def download(mod_id: int, mod_name: Optional[str], version: Optional[str]) -> Op |
564 | 565 | else next(filter(lambda v: v.friendly_version == version, mod.versions), None) |
565 | 566 | if not mod_version: |
566 | 567 | abort(404, 'Unfortunately we couldn\'t find the requested mod version. Maybe it got deleted?') |
567 | | - # Events are aggregated hourly |
568 | | - an_hour_ago = datetime.now() - timedelta(hours=1) |
569 | | - download = DownloadEvent.query\ |
570 | | - .filter(DownloadEvent.version_id == mod_version.id, DownloadEvent.created > an_hour_ago)\ |
571 | | - .order_by(desc(DownloadEvent.created))\ |
572 | | - .first() |
573 | | - storage = _cfg('storage') |
574 | | - if not storage: |
575 | | - abort(404) |
576 | | - |
577 | | - if 'Range' not in request.headers: |
578 | | - if not download: |
579 | | - download = DownloadEvent() |
580 | | - download.mod = mod |
581 | | - download.version = mod_version |
582 | | - download.downloads = 1 |
583 | | - db.add(download) |
584 | | - db.flush() |
585 | | - db.commit() |
586 | | - mod.downloads.append(download) |
587 | | - else: |
588 | | - download.downloads += 1 |
589 | | - mod.download_count += 1 |
590 | | - mod_version.download_count += 1 |
591 | | - mod.score = get_mod_score(mod) |
| 568 | + ua = user_agents.parse(request.user_agent.string) |
| 569 | + # Only count download events from non-bots |
| 570 | + if not ua.is_bot: |
| 571 | + # Events are aggregated hourly |
| 572 | + an_hour_ago = datetime.now() - timedelta(hours=1) |
| 573 | + download = DownloadEvent.query\ |
| 574 | + .filter(DownloadEvent.version_id == mod_version.id, DownloadEvent.created > an_hour_ago)\ |
| 575 | + .order_by(desc(DownloadEvent.created))\ |
| 576 | + .first() |
| 577 | + if 'Range' not in request.headers: |
| 578 | + if not download: |
| 579 | + download = DownloadEvent() |
| 580 | + download.mod = mod |
| 581 | + download.version = mod_version |
| 582 | + download.downloads = 1 |
| 583 | + db.add(download) |
| 584 | + db.flush() |
| 585 | + db.commit() |
| 586 | + mod.downloads.append(download) |
| 587 | + else: |
| 588 | + download.downloads += 1 |
| 589 | + mod.download_count += 1 |
| 590 | + mod_version.download_count += 1 |
| 591 | + mod.score = get_mod_score(mod) |
| 592 | + elif 'discord'.casefold() in ua.browser.family.casefold(): |
| 593 | + # Send HTML to Discord so it can see the OpenGraph tags |
| 594 | + return redirect(url_for("mods.mod", mod_id=mod.id, mod_name=mod.name)) |
592 | 595 |
|
593 | 596 | protocol = _cfg("protocol") |
594 | 597 | cdn_domain = _cfg("cdn-domain") |
|
0 commit comments