Skip to content

Commit 79fb0da

Browse files
authored
Merge pull request #436 from HebaruSan/fix/bot-downloads
Downloads: Don't count from bots, send HTML to Discord
2 parents 0755ae0 + 380bf52 commit 79fb0da

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

KerbalStuff/blueprints/mods.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import dns.resolver
1313
import requests
1414
import werkzeug.wrappers
15+
import user_agents
1516

1617
from flask import Blueprint, render_template, send_file, make_response, url_for, abort, session, \
1718
redirect, request
@@ -564,31 +565,33 @@ def download(mod_id: int, mod_name: Optional[str], version: Optional[str]) -> Op
564565
else next(filter(lambda v: v.friendly_version == version, mod.versions), None)
565566
if not mod_version:
566567
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))
592595

593596
protocol = _cfg("protocol")
594597
cdn_domain = _cfg("cdn-domain")

requirements-backend.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ requests-oauthlib
2828
SQLAlchemy>=1.4,<2 # 1.4 shows deprecation warnings for 2.0, 2.0 would break our backend as of now
2929
SQLAlchemy-Utils
3030
urllib3
31+
user_agents
3132
Werkzeug

0 commit comments

Comments
 (0)