Skip to content

Instantly share code, notes, and snippets.

@maruhoi1
Last active December 22, 2018 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maruhoi1/4801939e9aec5cc796fb3e6b47d73f8d to your computer and use it in GitHub Desktop.
Save maruhoi1/4801939e9aec5cc796fb3e6b47d73f8d to your computer and use it in GitHub Desktop.
Google Apps Scripts for Steam Status Crawler
function setActivity() {
//記録用スプレッドシートを取得
var sheet = SpreadsheetApp.getActiveSheet();
//Steamプロフィールページのリンク
var url = "https://steamcommunity.com/id/maruhoi1";
//プロフィールページを取得する
var content = UrlFetchApp.fetch(url).getContentText();
//ゲーム中かゲーム中ではないかを判断するためのパース用変数
var fromText = '<div class="profile_in_game persona ';
var toText = '">';
//ゲーム中でなければ「ゲームしてません」と記録して、ゲーム中であればタイトル名を記録する
if(startParse(content,fromText,toText) !== "in-game"){
sheet.getRange(sheet.getLastRow()+1,1,1,2).setValues([[getDate(),"ゲームしてません"]]);
}else{
var fromText = '<div class="profile_in_game_name">';
var toText = '</div>';
sheet.getRange(sheet.getLastRow()+1,1,1,2).setValues([[getDate(),startParse(content,fromText,toText)]]);
}
}
//外部ライブラリで引数を利用してパースする関数
function startParse(content,fromText,toText){
return Parser.data(content).from(fromText).to(toText).build();
}
//コード実行した日時を取得する関数
function getDate(){
var sheet = SpreadsheetApp.getActiveSheet();
var date = new Date();
return Utilities.formatDate(date,"JST","yyyy/MM/dd HH:mm");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment