[boardgames] Add lichess importing
This commit is contained in:
119
vrobbler/apps/boardgames/sources/lichess.py
Normal file
119
vrobbler/apps/boardgames/sources/lichess.py
Normal file
@ -0,0 +1,119 @@
|
||||
import berserk
|
||||
from django.conf import settings
|
||||
|
||||
from boardgames.models import BoardGame
|
||||
from scrobbles.models import Scrobble
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def import_chess_games_for_all_users():
|
||||
client = berserk.Client(
|
||||
session=berserk.TokenSession(settings.LICHESS_API_KEY)
|
||||
)
|
||||
|
||||
scrobbles_to_create = []
|
||||
for user in User.objects.filter(profile__lichess_username__isnull=False):
|
||||
games = client.games.export_by_player(user.profile.lichess_username)
|
||||
for game_dict in games:
|
||||
chess, created = BoardGame.objects.get_or_create(title="Chess")
|
||||
if created:
|
||||
chess.run_time_seconds = 1800
|
||||
chess.bggeek_id = 171
|
||||
chess.save(update_fields=["run_time_seconds", "bggeek_id"])
|
||||
scrobble = Scrobble.objects.filter(
|
||||
user_id=user.id,
|
||||
timestamp=game_dict.get("createdAt"),
|
||||
board_game_id=chess.id,
|
||||
).first()
|
||||
|
||||
if scrobble:
|
||||
continue
|
||||
|
||||
log_data = {
|
||||
"variant": game_dict.get("variant"),
|
||||
"lichess_id": game_dict.get("id"),
|
||||
"rated": game_dict.get("rated"),
|
||||
"speed": game_dict.get("speed"),
|
||||
"moves": game_dict.get("moves"),
|
||||
"players": [],
|
||||
}
|
||||
|
||||
chess_status = game_dict.get("status")
|
||||
chess_source = game_dict.get("source")
|
||||
|
||||
winner = game_dict.get("winner")
|
||||
black_player = game_dict.get("players", {}).get("black", {})
|
||||
white_player = game_dict.get("players", {}).get("white", {})
|
||||
|
||||
user_player = {
|
||||
"user_id": user.profile.lichess_username,
|
||||
"color": "",
|
||||
"win": False,
|
||||
}
|
||||
other_player = {"name_str": "", "color": "", "win": False}
|
||||
|
||||
if (
|
||||
black_player.get("user", {}).get("name", "")
|
||||
== user.profile.lichess_username
|
||||
):
|
||||
user_player["color"] = "black"
|
||||
if "aiLevel" in white_player.keys():
|
||||
other_player["name_str"] = "aiLevel_" + str(
|
||||
white_player.get("aiLevel", "")
|
||||
)
|
||||
else:
|
||||
other_player["name_str"] = white_player.get(
|
||||
"user", {}
|
||||
).get("name", "")
|
||||
|
||||
other_player["color"] = "white"
|
||||
if winner == "black":
|
||||
user_player["win"] = True
|
||||
else:
|
||||
other_player["win"] = True
|
||||
if (
|
||||
white_player.get("user", {}).get("name", "")
|
||||
== user.profile.lichess_username
|
||||
):
|
||||
user_player["color"] = "white"
|
||||
if "aiLevel" in black_player.keys():
|
||||
other_player["name_str"] = "aiLevel_" + str(
|
||||
black_player.get("aiLevel", "")
|
||||
)
|
||||
else:
|
||||
other_player["name_str"] = white_player.get(
|
||||
"user", {}
|
||||
).get("name", "")
|
||||
other_player["color"] = "black"
|
||||
if winner == "white":
|
||||
user_player["win"] = True
|
||||
else:
|
||||
other_player["win"] = True
|
||||
|
||||
log_data["players"].append(user_player)
|
||||
log_data["players"].append(other_player)
|
||||
|
||||
scrobble_dict = {
|
||||
"user_id": user.id,
|
||||
"timestamp": game_dict.get("createdAt"),
|
||||
"stop_timestamp": game_dict.get("lastMoveAt"),
|
||||
"board_game_id": chess.id,
|
||||
"log": log_data,
|
||||
}
|
||||
scrobbles_to_create.append(Scrobble(**scrobble_dict))
|
||||
|
||||
if scrobbles_to_create:
|
||||
Scrobble.objects.bulk_create(scrobbles_to_create)
|
||||
return scrobbles_to_create
|
||||
|
||||
|
||||
# 'players': {
|
||||
# 'white': {'aiLevel': 1},
|
||||
# 'black': {'user': {'name': 'secstate', 'id': 'secstate'},
|
||||
# 'rating': 1500,
|
||||
# 'provisional': True}
|
||||
# },
|
||||
# 'fullId': '4T8CinfXdI95',
|
||||
# 'winner': 'black',
|
||||
@ -0,0 +1,21 @@
|
||||
# Generated by Django 4.2.18 on 2025-01-29 04:50
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
(
|
||||
"profiles",
|
||||
"0020_userprofile_ntfy_enabled_userprofile_ntfy_url_and_more",
|
||||
),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="userprofile",
|
||||
name="lichess_username",
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
]
|
||||
@ -30,6 +30,7 @@ class UserProfile(TimeStampedModel):
|
||||
archivebox_url = models.CharField(max_length=255, **BNULL)
|
||||
|
||||
bgg_username = models.CharField(max_length=255, **BNULL)
|
||||
lichess_username = models.CharField(max_length=255, **BNULL)
|
||||
|
||||
todoist_auth_key = EncryptedField(**BNULL)
|
||||
todoist_state = EncryptedField(**BNULL)
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from vrobbler.apps.boardgames.sources.lichess import (
|
||||
import_chess_games_for_all_users,
|
||||
)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
count = len(import_chess_games_for_all_users())
|
||||
print(f"Imported {count} Lichess games")
|
||||
Reference in New Issue
Block a user