0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel lolalytics api を実装したい!①

Posted at

lolalytics api(非公式) を実装したいと思っていますが、その前に取ってきた統計を新しいタブをクリックした後のページに表示するようにしたい

スクリーンショット 2025-08-12 172834.png
(こんなかんじ)

lolalytics-api ドキュメントまとめ

概要

lolalytics-api は LoLalytics の統計データを取得するための 非公式 Python ライブラリ です。
LoLalytics サイトのスクレイピングを行うため、サイト構造変更により動作が不安定になる可能性があります。
使用の際は LoLalytics の利用規約(スクレイピング関連)を確認してください。


インストール

pip install lolalytics-api==0.0.6

主な関数と取得できる情報

get_tierlist(n: int = 10, lane: str = '', rank: str = '')

  • 概要: チャンピオンのティアリスト(勝率順など)を取得
  • 引数:
    • n: 取得件数(デフォルト10)
    • lane: レーン名(例: "mid", "top", "adc", "support", "jungle"
    • rank: ランク帯(例: "emerald_plus", "gold_plus"
  • 返り値: リスト形式(チャンピオン名、勝率、ピック率など)

get_counters(n: int = 10, champion: str = '', rank: str = '')

  • 概要: 指定チャンピオンのカウンター情報を取得
  • 引数:
    • n: 取得件数
    • champion: チャンピオン名(例: "Ahri")
    • rank: ランク帯
  • 返り値: カウンターリスト(相手チャンプ名、勝率など)

get_champion_data(champion: str, lane: str = '', rank: str = '')

  • 概要: 指定チャンピオンの詳細統計を取得
  • 引数:
    • champion: チャンピオン名
    • lane: レーン名
    • rank: ランク帯
  • 返り値: 勝率・ピック率・Ban率・ビルド傾向などの辞書

matchup(champion1: str, champion2: str, lane: str = '', rank: str = '')

  • 概要: 2チャンピオン間の対面勝率を取得
  • 引数:
    • champion1, champion2: チャンピオン名
    • lane: レーン名
    • rank: ランク帯
  • 返り値: 勝率、試合数など

patch_notes(rank: str = '')

  • 概要: 最新パッチでのバフ・ナーフされたチャンピオン情報を取得
  • 引数:
    • rank: ランク帯
  • 返り値: チャンピオン名と統計変化(勝率変化、ピック率変化など)

使用例

from lolalytics_api import (
    get_tierlist,
    get_counters,
    get_champion_data,
    matchup,
    patch_notes
)

# 1. ミッドレーン Emerald+ のティアリスト上位5件
tiers = get_tierlist(n=5, lane="mid", rank="emerald_plus")
print(tiers)

# 2. Ahri のカウンター上位5件
ahri_counters = get_counters(n=5, champion="Ahri", rank="emerald_plus")
print(ahri_counters)

# 3. Ahri の詳細データ
ahri_data = get_champion_data(champion="Ahri", lane="mid", rank="emerald_plus")
print(ahri_data)

# 4. Ahri 対 Zed の対面データ
ahri_vs_zed = matchup("Ahri", "Zed", lane="mid", rank="emerald_plus")
print(ahri_vs_zed)

# 5. 最新パッチのバフ・ナーフ情報
latest_patch_changes = patch_notes(rank="emerald_plus")
print(latest_patch_changes)

注意事項

  • 非公式ライブラリのため、LoLalytics 側の仕様変更で突然動かなくなる可能性があります
  • 実運用する場合は例外処理キャッシュを実装することを推奨します
  • 利用規約やスクレイピングに関するルールに必ず従ってください

Laravel Breeze/Blade に「統計」ページを追加してナビに表示、logout エラーも修正する手順

1. コントローラを作成

ターミナルで実行:

php artisan make:controller StatsController

2. コントローラコード

app/Http/Controllers/StatsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StatsController extends Controller
{
    public function index()
    {
        // 後でAPIやスクレイピング処理に置き換え可能
        $statsData = [
            'winRate' => '52.3%',
            'pickRate' => '14.1%',
            'topChampion' => 'Ahri',
        ];

        return view('stats.index', ['stats' => $statsData]);
    }
}

3. ルートを追加

routes/web.php に追記:

use App\Http\Controllers\StatsController;

Route::get('/stats', [StatsController::class, 'index'])
    ->middleware(['auth']) // ログイン不要なら削除
    ->name('stats.index');

4. ビューを作成

resources/views/stats/index.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            統計
        </h2>
    </x-slot>

    <div class="py-6">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white p-6 shadow-sm sm:rounded-lg">
                <h3 class="text-lg font-bold mb-4">サンプル統計データ</h3>
                <ul class="list-disc pl-6">
                    <li>勝率: {{ $stats['winRate'] }}</li>
                    <li>ピック率: {{ $stats['pickRate'] }}</li>
                    <li>トップチャンピオン: {{ $stats['topChampion'] }}</li>
                </ul>
            </div>
        </div>
    </div>
</x-app-layout>

5. ナビゲーションにリンクを追加

resources/views/layouts/navigation.blade.php を編集。

デスクトップ用

<x-nav-link :href="route('stats.index')" :active="request()->routeIs('stats.*')">
    統計
</x-nav-link>

モバイル用

<x-responsive-nav-link :href="route('stats.index')" :active="request()->routeIs('stats.*')">
    統計
</x-responsive-nav-link>

6. logout エラーの修正

Breeze/Jetstream のログアウトルートが読み込まれていない場合、routes/web.php の末尾に追加:

require __DIR__.'/auth.php';

7. キャッシュクリア

php artisan optimize:clear
php artisan route:clear
php artisan view:clear
composer dump-autoload

8. 動作確認

  • /stats にアクセス → StatsController@index が実行され、Bladeにデータが渡る
  • ナビの「統計」タブをクリック → /stats へ遷移
  • ハンバーガーメニュー(スマホ)でも「統計」が表示される
  • logout リンクから正常にログアウトできる

9. 補足

  • 今後、StatsController@index 内にスクレイピングや API 呼び出し処理を追加すれば、統計ページを動的化できる
  • ナビリンクの request()->routeIs('stats.*') は、stats.index 以外の stats.show 等でもアクティブ表示可能
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?