検索する期間・カテゴリーを選択してください
現状のランキング
ALIS本体にもランキングはありますが、だいたい1~2日前に書かれた記事しか表示されていません。
それに検索(記事・タグ)してもいいねの多い順には表示してくれないし、ソート機能もありません。
てことで、期間指定出来るいいねランキングツールを作りました( ͡° ͜ʖ ͡°)
ランキング表示はこんな感じ
いいね数はもちろんのこと、作者(user_id)+マイページのリンク、タイトル+記事のリンクを表示します。
ALIS Ranking の作り方(サンプルコード)
まずはツールを作る前にデーターベースを作りますので、そちらのコードから。
<?php set_time_limit(300); $context = stream_context_create(["http" => ["ignore_errors" => true,"timeout" => 100]]); //いろいろ初期値セット $start = 0; $kanma = ","; $kigo = "\""; $number = 0; $count = 1; $gets = 0; $file = 0; //対象のURL for($j = 1; $j < 500; $j++){ $urls[] = "https://alis.to/api/articles/recent?limit=100&page=".$j; } //マルチハンドル初期化 $mh = curl_multi_init(); //後で使うため個別ハンドル保管用の配列を準備 $ch_array = array(); //URLのセット foreach($urls as $url) { $ch = curl_init(); $ch_array[] = $ch; curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 30, ]); curl_multi_add_handle($mh, $ch); } do { curl_multi_exec($mh, $running); curl_multi_select($mh); } while ($running > 0); //データー取得 foreach ($ch_array as $ch) { $file = curl_multi_getcontent($ch); usleep(50000); if(strpos($file,"article_id",$start) == false){ curl_close($ch); $gets = 1; do { $file = file_get_contents("https://alis.to/api/articles/recent?limit=100&page=".$count,false, $context); } while (strpos($http_response_header[0], "500") !== false); } // 100記事分繰り返し処理 for($i = 0; $i < 100; $i++){ if(strpos($file,"article_id",$start) == false){ break; } //記事idを取得 $start = strpos($file,"article_id",$start)+14; $end = strpos($file,$kanma,$start)-1; $article_id = substr($file, $start, $end - $start); //echo $article_id.$kanma; //ユーザーIDを取得 if(strpos($file,"user_id",$end) == false){ break; } $start = strpos($file,"user_id",$end)+11; $end = strpos($file,$kanma,$start)-1; $user_id = substr($file, $start, $end - $start); //記事作成時間取得 if(strpos($file,"created_at",$end) == false){ break; } $start = strpos($file,"created_at",$end)+13; $timestamp = substr($file, $start, 10); $date = new DateTime("@$timestamp"); $date->setTimeZone(new DateTimeZone('Asia/Tokyo')); $time = $date->format("Ymd"); //トピックを取得 if(strpos($file,"topic",$end) == false){ break; } $start = strpos($file,"topic",$start)+9; $end = strpos($file,$kanma,$start)-1; $topic = substr($file, $start, $end - $start); //タイトルを取得 if(strpos($file,"title",$end) == false){ break; } $start = strpos($file,"title",$end)+8; $end = strpos($file,$kanma,$start); $title = json_decode(substr($file, $start, $end - $start)); //状態を取得 $start = strpos($file,"status",$end)+10; $end = strpos($file,"}",$start)-1; $status = substr($file, $start, $end - $start); if(strcmp($status,"public") == 0){ $output[] = "article_id:"."$article_id,$user_id,$time,$topic,$title"."\n"; } } $start= 0; $count ++; if($gets !== 1){ curl_multi_remove_handle($mh, $ch); curl_close($ch); } $gets = 0; $file = 0; } //終了処理 curl_multi_close($mh); //保存 file_put_contents("ranking1.dat",serialize($output));
ALISには 2019/11/19 現在、46,852件の記事(公開されてるもの)があるので、余裕をみて50,000記事分の処理をしてます。
取得するデーター
- 記事ID
- ユーザーID
- 記事作成時間
- トピック(カテゴリー)
- タイトル
- ステータス(公開 or 非公開)
上記データーを取得し、データーベースを作っておきます。
次に、期間指定するフォームをHTMLで作ります。
<p><span class="ymarker-s"><b>検索する期間・カテゴリーを選択してください</b></span><br /> <form action="https://voemushroom.com/test.php" method="post"> 開始日 <input type="date" name="date1" min="2018-04-01"><br><br> 終了日 <input type="date" name="date2" min="2018-04-01"></p> <br>カテゴリー <select name="categori"> <optgroup label="カテゴリ"> <option value="nothing" selected>未選択 <option value="crypto">クリプト <option value="gourmet">グルメ <option value="business">ビジネス <option value="travel">トラベル <option value="beauty">ビューティー <option value="education-parenting">教育・子育て <option value="comic-animation">マンガ・アニメ <option value="technology">テクノロジー <option value="other">その他 </optgroup> </select><br><br> <input type="submit" value=" OK "><br> </form>
最後に上のHTMLから開始日・終了日を受け取り、データーベースから必要な部分を抽出するコードを書きます。
<?php set_time_limit(300); $date1 = $_POST["date1"]; $date2 = $_POST["date2"]; $categori = $_POST["categori"]; if($date1 == null){ exit("開始日が選択されていません"); } if($date2 == null){ exit("終了日が選択されていません"); } $date_start = date('Ymd', strtotime($date1)); $date_end = date('Ymd', strtotime($date2)); $context = stream_context_create(["http" => ["ignore_errors" => true,"timeout" => 100]]); $file = file_get_contents("ranking.dat"); //いろいろデーターをセット $end = 0; $kanma = ","; $kigo = "\""; $number = 0; $sum = substr($file, 2, 5); for($j = 0; $j < $sum; $j++){ $start = strpos($file,"article_id",$end)+11; $end = strpos($file,$kanma,$start); $id = substr($file, $start, $end - $start); $start1 = $end+1; $end = strpos($file,$kanma,$start1); $user = substr($file, $start1, $end - $start1); $start2 = $end+1; $end = strpos($file,$kanma,$start2); $time0 = substr($file, $start2, $end - $start2); $time = date('Ymd', strtotime($time0)); $start3 = $end+1; $end = strpos($file,$kanma,$start3); $topic = substr($file, $start3, $end - $start3); $start4 = $end+1; $end = strpos($file,$kigo,$start4); $title = substr($file, $start4, $end - $start4); if(strcmp($categori,"nothing") !== 0 && strcmp($categori,$topic) !== 0){ continue; } if($time >= $date_start && $time <= $date_end){ do { $like = file_get_contents("https://alis.to/api/articles/".$id."/likes",false, $context); } while (strpos($http_response_header[0], "500") !== false); //likesを取得 $end1 = strpos($like,"}"); $likes = substr($like,10, $end1 - 10); //echo "$id,$user,$time,$topic,$title,$likes"."<br>"; $arr[] = [$likes,$id,$user,$time,$topic,$title]; } } rsort($arr); $count = count($arr); ?> <html> <head> <meta charset='utf-8'> <title>ランキング結果�df44</title> </head> <body> <h2><?php echo $date_start." ~ ".$date_end." のいいねランキング結果�df44"; ?></h2> <h3>対象記事件数(<?= $count; ?>)</h3> <?php for($q = 0; $q < 10; $q++){ if($q > $count-1){ break; } $w = $q+1; echo "<h4>"."$w"."位:".$arr[$q][0]."いいね". '<a href="https://alis.to/users/'.$arr[$q][2].' target="_blank">'.$arr[$q][2]."さん</a><br>". '<a href="https://alis.to/'.$arr[$q][2]."/articles/".$arr[$q][1].' target="_blank">'.$arr[$q][5]."</a><br>". "(".$arr[$q][4].")</h4>"; } ?> </body> </html>
最後に
データーを収集などのバックエンド色々出来るようになったので、ぼちぼちフロントエンドの勉強をしていこうかと思います。お洒落なwebアプリ作りたいな🍄