Skip to content

Instantly share code, notes, and snippets.

@nyomo
Last active April 29, 2022 11:01
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 nyomo/92c6fb8dad040be49257e08d4b5d0a6a to your computer and use it in GitHub Desktop.
Save nyomo/92c6fb8dad040be49257e08d4b5d0a6a to your computer and use it in GitHub Desktop.
暮らしのマーケットの請求データをTSVにするやつ
<?php
//https://curama.jp/shop/bill/ にアクセスしたときのcookieの値をコピペしておく
$cookie = 'isLoggedInAsUser=false; visitorId=なんたらかんたら';
$url='https://curama.jp';
//各月のURLを取得する
$html = get_html_file($url.'/shop/bill/','',$cookie);
$lines = explode("\n",$html);
$url_list = get_bill_id($lines);
$datas = array();
//最初のページのデータ
$datas = array_merge($datas,get_bill_data($lines));
//取得したURLからデータを得る
foreach($url_list as $opt){
$html = get_html_file($url.'/shop/bill/?'.$opt,'',$cookie);
$lines = explode("\n",$html);
$datas = array_merge($datas,get_bill_data($lines));
}
//何も考えずに表計算にコピペできるTSVにする
$fp = fopen("data.csv","w");
$line = "区分\t日付\tid\t会計金額\t手数料\tStripe手数料\t備考\n";
fputs($fp,$line);
foreach($datas as $data){
$line = $data['kubun']."\t".$data['date']."\t".$data['id']."\t".$data['kaikei_kingaku']."\t".$data['tesuryo']."\t".$data['stripe_tesuryo']."\t".$data['bikou']."\t"."\n";
fputs($fp,$line);
}
exit();
//おわり****************************************************************
//データを取得する
function get_bill_data($lines){
$flg=0;
$result = array();
$kubun = 'nplist';
$data = array('kubun'=>$kubun,'date'=>'','id'=>'','kaikei_kingaku'=>'','tesuryo'=>'','stripe_tesuryo'=>'','bikou'=>'',);
$count = 0;
for($i = 0;$i<count($lines);$i++){
$line = $lines[$i];
//echo $line."\n";
//この後からにデータが入っている
if(preg_match('/.*ご請求明細.*/',$line,$match)){
$flg = 1;
}
//ここまで
if(preg_match('/.\<\/main\>.*/',$line,$match)){
$flg = 2;
}
//各日データ
if($flg == 1){
if(preg_match('/.\<div id="stripelist" class="col s12"\>.*/',$line,$match)){
$kubun = 'stripelist';
$data['kubun'] = $kubun;
}
if(preg_match('/.\<div class="col s2 col_bla08"\>\<span class="text-sm"\>(.*)\<\/span\>\<\/div\>.*/',$line,$match)){
echo $match[1]."\n";
$data['date'] = $match[1];
do{
$line = $lines[++$i];
if(preg_match('/.*\<a href="\/shop\/reservations\/detail\/(.*)\/" target="_blank" rel="noopener noreferrer"\>.*\<\/a\>.*/',$line,$match)){
$data['id'] = $match[1];
}
//Stripeデータ
if(strcmp($kubun,'stripelist')==0){
if($sfl == 0 && preg_match('/.*\<div class="col s4 right-align"\>(.*)円\<\/div\>.*/',$line,$match)){
$sfl = 1;
}else if($sfl == 1 && preg_match('/.*\<span class="text-sm"\>お会計金額(税込)\<\/span\>*/',$line,$match)){
$line = $lines[++$i];
preg_match('/.*\<div class="col s4 right-align"\>(.*)円.*/',$line,$match);
$data['kaikei_kingaku'] = $match[1];
$sfl = 2;
}else if($sfl == 2 && preg_match('/.*\<div class="col s4 right-align"\>(.*)円\<\/div\>.*/',$line,$match)){
$data['tesuryo'] = $match[1];
$sfl = 3;
}else if($sfl == 3 && preg_match('/.*\<div class="col s4 right-align"\>(.*)円\<\/div\>.*/',$line,$match)){
$data['stripe_tesuryo'] = $match[1];
$sfl = 0;
}
}else{
//NetProtectionデータ
if(preg_match('/.*\<div class="col s3 right-align"\>\<h2 class="mg-offset-0"\>(.*)円\<\/h2\>\<\/div\>.*/',$line,$match)){
$data['tesuryo'] = $match[1];
}
if(preg_match('/.*\<div class="col s8"\>\<span class="text-sm"\>お会計金額(税込)\<\/span\>\<\/div\>\<div class="col s4 right-align"\>(.*)円\<\/div\>.*/',$line,$match)){
$data['kaikei_kingaku'] = $match[1];
}
if(preg_match('/.*\<div class="col s4"\>(店舗の当日キャンセル) :(.*)((税抜))\<\/div\>.*/',$line,$match)){
$data['id'] = $match[2];
$data['bikou'] = $match[1].$match[3];
}
if(preg_match('/.*\<div class="col s4"\>(リピーターからの受注(税抜))\<\/div\>.*/',$line,$match)){
$data['bikou'] = $match[1];
}
}
}while(!preg_match('/.\<\/li\>.*/',$line,$match));
$result[] = $data;
$sfl = 0;
$data = array('kubun'=>$kubun,'date'=>'','id'=>'','kaikei_kingaku'=>'','tesuryo'=>'','stripe_tesuryo'=>'','bikou'=>'',);
}
}
}
return $result;
}
//各月のURLを取得する
function get_bill_id($lines){
$flg=0;
$result = array();
$count = 0;
foreach($lines as $line){
//この後からにデータが入っている
if(preg_match('/.*select id="npBill" class="browser-default".*/',$line,$match)){
$flg = 1;
}
//ここまで
if(preg_match('/.\<\/select\>.*/',$line,$match)){
$flg = 2;
}
if($flg == 1 && preg_match('/.\<option value="\/shop\/bill\/\?(bill_id=.*&month=[0-9]{4}-[0-9]{2})" \>.*/',$line,$match)){
$result[] = $match[1];
}
if($flg == 1){
}
}
return $result;
}
// HTMLを取得します
function get_html_file($url,$getopt,$cookie){
$context = array(
'http' => array(
'method' => 'GET',
'header' => "accept-language: ja,en;q=0.9,en-GB;q=0.8,en-US;q=0.7\r\n"
. "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n"
. "referer: https://curama.jp/shop/bill/\r\n"
. "cookie: $cookie\r\n"
. "sec-ch-ua-mobile: ?0\r\n"
. 'sec-ch-ua-ua: " Not;A Brand";v="99", "Microsoft Edge";v="97", "Chromium";v="97"'."\r\n"
. 'sec-ch-ua-platform: "Windows"'."\r\n"
. "sec-fetch-mode: navigate\r\n"
. "sec-ch-ua-user: ?1\r\n"
. "upgrade-insecure-requests: 1\r\n"
. "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62\r\n"
)
);
$html = file_get_contents($url . $getopt , false, stream_context_create($context));
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment