Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip
Browse files Browse the repository at this point in the history
kwanok committed Mar 15, 2024
1 parent d980db3 commit db0ec15
Showing 10 changed files with 258 additions and 43 deletions.
125 changes: 97 additions & 28 deletions web/league_entry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import datetime
import os
import asyncpg
import aiohttp
@@ -14,32 +15,116 @@ def __init__(self):
self.api_key = os.environ.get("RIOT_API_KEY")
self.tiers = ["IRON", "BRONZE", "SILVER", "GOLD", "PLATINUM", "DIAMOND"]
self.divisions = ["I", "II", "III", "IV"]

self.db_user = os.environ.get("POSTGRES_USER")
self.db_password = os.environ.get("POSTGRES_PASSWORD")
self.db_host = "localhost"
self.db_port = os.environ.get("POSTGRES_PORT")
self.db_name = os.environ.get("POSTGRES_WEB_DB")

self.pool = None

async def create_db_pool(self):
self.pool = await asyncpg.create_pool(
user=self.db_user,
password=self.db_password,
host=self.db_host,
port=self.db_port,
database=self.db_name,
)

async def run(self):
# for tier in self.tiers:
# for division in self.divisions:
# await self.get_league_entries(tier, division)
await self.create_db_pool()

await self.get_league_entries(self.tiers[0], self.divisions[0])
for tier in self.tiers:
for division in self.divisions:
await self.get_league_entries(tier, division)

await self.session.close()
await self.pool.close()

async def get_league_entries(self, tier: str, division: str):
url = f"{self.base_url}/{tier}/{division}?api_key={self.api_key}"
page = 1

league_entries = await self._fetch(url + f"&page={page}")

summoners = []

while league_entries:
summoners.extend(league_entries)
print(f"Fetching page {page}")
print(f"Fetching {tier} {division} page {page}... entries: {len(league_entries)}")
summoners = [
(
league_entry["summonerId"],
league_entry["summonerName"],
datetime.datetime.now(),
datetime.datetime.now(),
)
for league_entry
in league_entries
]

async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.executemany(
"INSERT INTO app_summoner (id, name, created_at, updated_at)"
"VALUES ($1, $2, $3, $4)"
"ON CONFLICT (id)"
"""
DO UPDATE SET
name = EXCLUDED.name,
updated_at = EXCLUDED.updated_at
""",
summoners,
)

entries = [
(
league_entry["tier"],
league_entry["rank"],
league_entry["leaguePoints"],
league_entry["wins"],
league_entry["losses"],
league_entry["veteran"],
league_entry["inactive"],
league_entry["freshBlood"],
league_entry["hotStreak"],
league_entry["queueType"],
league_entry["leagueId"],
league_entry["summonerId"],
datetime.datetime.now(),
datetime.datetime.now(),
)
for league_entry
in league_entries
]
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.executemany(
"INSERT INTO app_leagueentry (tier, rank, league_points, wins, losses, veteran, inactive, fresh_blood, hot_streak, queue_type, league_id, summoner_id, created_at, updated_at)"
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)"
"ON CONFLICT (summoner_id, queue_type)"
"""
DO UPDATE SET
tier = EXCLUDED.tier,
rank = EXCLUDED.rank,
league_points = EXCLUDED.league_points,
wins = EXCLUDED.wins,
losses = EXCLUDED.losses,
veteran = EXCLUDED.veteran,
inactive = EXCLUDED.inactive,
fresh_blood = EXCLUDED.fresh_blood,
hot_streak = EXCLUDED.hot_streak,
queue_type = EXCLUDED.queue_type,
league_id = EXCLUDED.league_id,
summoner_id = EXCLUDED.summoner_id,
updated_at = EXCLUDED.updated_at
""",
entries,
)

page += 1
league_entries = await self._fetch(url + f"&page={page}")

print(summoners)
return []

async def _fetch(self, url: str) -> list:
async with self.session.get(url) as response:
@@ -56,24 +141,8 @@ async def _fetch(self, url: str) -> list:


async def main():
# crawler = LeagueEntryCrawler()
# await crawler.run()

db_user = os.environ.get("POSTGRES_USER")
db_password = os.environ.get("POSTGRES_PASSWORD")
db_host = "localhost"
db_port = os.environ.get("POSTGRES_PORT")
db_name = os.environ.get("POSTGRES_WEB_DB")

db_url = f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"

print(f"Connecting to database: {db_url}")

pool = await asyncpg.create_pool(db_url)

print("Connected to database")

await pool.close()
crawler = LeagueEntryCrawler()
await crawler.run()


loop = asyncio.get_event_loop()
45 changes: 43 additions & 2 deletions web/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions web/project/app/migrations/0005_leagueentry_unique_league_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.2 on 2024-03-14 07:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("app", "0004_leagueentry"),
]

operations = [
migrations.AddConstraint(
model_name="leagueentry",
constraint=models.UniqueConstraint(
fields=("summoner", "queue_type"), name="unique_league_entry"
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.2 on 2024-03-14 08:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("app", "0005_leagueentry_unique_league_entry"),
]

operations = [
migrations.AddIndex(
model_name="summoner",
index=models.Index(fields=["name"], name="app_summone_name_9a15eb_idx"),
),
]
11 changes: 11 additions & 0 deletions web/project/app/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Create your models here.

from django.contrib.postgres.search import SearchVector, SearchVectorField
from users.models import AppUser as User
from django.db import models

@@ -35,6 +36,11 @@ class Summoner(models.Model):

def __str__(self):
return self.name

class Meta:
indexes = [
models.Index(fields=['name'])
]


class LeagueEntry(models.Model):
@@ -55,3 +61,8 @@ class LeagueEntry(models.Model):

def __str__(self):
return f"{self.summoner.name} - {self.tier} {self.rank}"

class Meta:
constraints = [
models.UniqueConstraint(fields=['summoner', 'queue_type'], name='unique_league_entry')
]
8 changes: 7 additions & 1 deletion web/project/app/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from app.models import Summoner
from app.models import Summoner, LeagueEntry
from rest_framework import serializers

class SummonerSerializer(serializers.ModelSerializer):
class Meta:
model = Summoner
fields = "__all__"


class LeagueEntrySerializer(serializers.ModelSerializer):
class Meta:
model = LeagueEntry
fields = "__all__"
9 changes: 3 additions & 6 deletions web/project/app/urls.py
Original file line number Diff line number Diff line change
@@ -6,18 +6,15 @@
recommend_ai,
recommend_result,
riot_txt,
SummonerViewSet
search_summoners_by_name,
)

router = routers.DefaultRouter()
router.register(r"summoners", SummonerViewSet, basename="summoner")

urlpatterns = [
path("riot.txt", riot_txt),
path("", index, name="home"),
path("summoner/", get_account_by_summoner_name, name="summoner"),
path("recommend-ai/", recommend_ai, name="recommend-ai"),
path("recommend-result/", recommend_result, name="recommend-result"),
path("api/", include(router.urls)),
path("summoners/search/", search_summoners_by_name, name="search-summoners-by-name"),
path("__debug__/", include("debug_toolbar.urls")),
]
urlpatterns += router.urls
52 changes: 47 additions & 5 deletions web/project/app/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import os
import uuid
from django.http import JsonResponse
from django.shortcuts import redirect, render
from django.core.handlers.wsgi import WSGIRequest
import requests
from django.contrib.auth.decorators import login_required

from users.models import SummonerInfo
from rest_framework.decorators import api_view
from app.riot_client import get_client
from app.riot_assets import get_riot_assets
from rest_framework import viewsets
from app.serializers import SummonerSerializer
from app.models import Summoner
from app.serializers import SummonerSerializer, LeagueEntrySerializer
from app.models import Summoner, LeagueEntry


def riot_txt(request: WSGIRequest):
@@ -82,6 +84,46 @@ def recommend_result(request: WSGIRequest):
)


class SummonerViewSet(viewsets.ModelViewSet):
queryset = Summoner.objects.all()
serializer_class = SummonerSerializer
@api_view(["GET"])
def search_summoners_by_name(request: WSGIRequest):
name = request.GET.get("name")
count = request.GET.get("count")
if not name or not count:
return JsonResponse({"error": "name, count are required"}, status=400)

try:
count = int(count)
except ValueError:
return JsonResponse({"error": "count must be an integer"}, status=400)

if count > 10:
return JsonResponse({"error": "count must be less than or equal to 10"}, status=400)

queryset = Summoner.objects.filter(name__startswith=name)[:count]
serializer = SummonerSerializer(queryset, many=True)

summoners = serializer.data

summoner_ids = [
summoner["id"]
for summoner in serializer.data
]

queryset = LeagueEntry.objects.filter(summoner_id__in=summoner_ids)
serializer = LeagueEntrySerializer(queryset, many=True)

league_entries = serializer.data

result = [
{
"summoner": summoner,
"league_entries": [
league_entry
for league_entry in league_entries
if league_entry["summoner"] == summoner["id"]
]
}
for summoner in summoners
]

return JsonResponse(result, safe=False)
13 changes: 12 additions & 1 deletion web/project/project/settings.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@

# SECURITY WARNING: don't run with debug turned on in production!
if ENV == "production":
DEBUG = False
DEBUG = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_SECONDS = 31536000 # 1 year
SESSION_COOKIE_SECURE = True
@@ -54,6 +54,7 @@
"app",
"users",
"rest_framework",
"debug_toolbar",
]

MIDDLEWARE = [
@@ -65,6 +66,7 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"project.middleware.user_activity_logger.UserActivityLoggerMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
]

ROOT_URLCONF = "project.urls"
@@ -173,3 +175,12 @@
environ.Env.read_env(BASE_DIR / ".env")

AUTH_USER_MODEL = "users.AppUser"

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}

INTERNAL_IPS = [
"127.0.0.1",
]
2 changes: 2 additions & 0 deletions web/pyproject.toml
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ asyncpg = "^0.29.0"
aiohttp = "^3.9.3"
markdown = "^3.5.2"
django-filter = "^24.1"
djangorestframework = "^3.14.0"
django-debug-toolbar = "^4.3.0"


[build-system]

0 comments on commit db0ec15

Please sign in to comment.