This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_loader.php
114 lines (92 loc) · 8.81 KB
/
file_loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// header("Content-Type: text/html; charset=SHIFT-JIS");
ini_set("max_execution_time",3600);//ファイル実行可能時間を長くしておく
//PHP Simple HTML DOM Parser Manualの読み込み
require_once 'lib/simple_html_dom.php';
define("URL_PRE", "http://www.oricon.co.jp/rank/obb/w/");
define("URL_MID", "/p/");
define("URL_SUF", "/");
define("FILE_NAME", encodeToSJIS("週間 文庫ランキング.csv"));
//データ取得する週(何日付けのデータか?を記す)
$dateList = array("2014-12-01", "2014-11-24", "2014-11-17", "2014-11-10", "2014-11-03", "2014-10-27", "2014-10-20", "2014-10-13", "2014-10-06", "2014-09-29", "2014-09-22", "2014-09-15", "2014-09-08", "2014-09-01", "2014-08-25", "2014-08-18", "2014-08-11", "2014-08-04", "2014-07-28", "2014-07-21", "2014-07-14", "2014-07-07", "2014-06-30", "2014-06-23", "2014-06-16", "2014-06-09", "2014-06-02", "2014-05-26", "2014-05-19", "2014-05-12", "2014-05-05", "2014-04-28", "2014-04-21", "2014-04-14", "2014-04-07", "2014-03-31", "2014-03-24", "2014-03-17", "2014-03-10", "2014-03-03", "2014-02-24", "2014-02-17", "2014-02-10", "2014-02-03", "2014-01-27", "2014-01-20", "2014-01-13", "2014-01-06", "2013-12-30", "2013-12-30", "2013-12-23", "2013-12-16", "2013-12-09", "2013-12-02", "2013-11-25", "2013-11-18", "2013-11-11", "2013-11-04", "2013-10-28", "2013-10-21", "2013-10-14", "2013-10-07", "2013-09-30", "2013-09-23", "2013-09-16", "2013-09-09", "2013-09-02", "2013-08-26", "2013-08-19", "2013-08-12", "2013-08-05", "2013-07-29", "2013-07-22", "2013-07-15", "2013-07-08", "2013-07-01", "2013-06-24", "2013-06-17", "2013-06-10", "2013-06-03", "2013-05-27", "2013-05-20", "2013-05-13", "2013-05-06", "2013-04-29", "2013-04-22", "2013-04-15", "2013-04-08", "2013-04-01", "2013-03-25", "2013-03-18", "2013-03-11", "2013-03-04", "2013-02-25", "2013-02-18", "2013-02-11", "2013-02-04", "2013-01-28", "2013-01-21", "2013-01-14", "2013-01-07", "2012-12-31", "2012-12-24", "2012-12-17", "2012-12-10", "2012-12-03", "2012-11-26", "2012-11-19", "2012-11-12", "2012-11-05", "2012-10-29", "2012-10-22", "2012-10-15", "2012-10-08", "2012-10-01", "2012-09-24", "2012-09-17", "2012-09-10", "2012-09-03", "2012-08-27", "2012-08-20", "2012-08-13", "2012-08-06", "2012-07-30", "2012-07-23", "2012-07-16", "2012-07-09", "2012-07-02", "2012-06-25", "2012-06-18", "2012-06-11", "2012-06-04", "2012-05-28", "2012-05-21", "2012-05-14", "2012-05-07", "2012-04-30", "2012-04-23", "2012-04-16", "2012-04-09", "2012-04-02", "2012-03-26", "2012-03-19", "2012-03-12", "2012-03-05", "2012-02-27", "2012-02-20", "2012-02-13", "2012-02-06", "2012-01-30", "2012-01-23", "2012-01-16", "2012-01-09", "2012-01-02", "2011-12-26", "2011-12-19", "2011-12-12", "2011-12-05", "2011-11-28", "2011-11-21", "2011-11-14", "2011-11-07", "2011-10-31", "2011-10-24", "2011-10-17", "2011-10-10", "2011-10-03", "2011-09-26", "2011-09-19", "2011-09-12", "2011-09-05", "2011-08-29", "2011-08-22", "2011-08-15", "2011-08-08", "2011-08-01", "2011-07-25", "2011-07-18", "2011-07-11", "2011-07-04", "2011-06-27", "2011-06-20", "2011-06-13", "2011-06-06", "2011-05-30", "2011-05-23", "2011-05-16", "2011-05-09", "2011-05-02", "2011-04-25", "2011-04-18", "2011-04-11", "2011-04-04", "2011-03-28", "2011-03-21", "2011-03-14", "2011-03-07", "2011-02-28", "2011-02-21", "2011-02-14", "2011-02-07", "2011-01-31", "2011-01-24", "2011-01-17", "2011-01-10", "2011-01-03", "2010-12-27", "2010-12-20", "2010-12-13", "2010-12-06", "2010-11-29", "2010-11-22", "2010-11-15", "2010-11-08", "2010-11-01", "2010-10-25", "2010-10-18", "2010-10-11", "2010-10-04", "2010-09-27", "2010-09-20", "2010-09-13", "2010-09-06", "2010-08-30", "2010-08-23", "2010-08-16", "2010-08-09", "2010-08-02", "2010-07-26", "2010-07-19", "2010-07-12", "2010-07-05", "2010-06-28", "2010-06-21", "2010-06-14", "2010-06-07", "2010-05-31", "2010-05-24", "2010-05-17", "2010-05-10", "2010-05-03", "2010-04-26", "2010-04-19", "2010-04-12", "2010-04-05", "2010-03-29", "2010-03-22", "2010-03-15", "2010-03-08", "2010-03-01", "2010-02-22", "2010-02-15", "2010-02-08", "2010-02-01", "2010-01-25", "2010-01-18", "2010-01-04", "2009-12-28", "2009-12-21", "2009-12-14", "2009-12-07", "2009-11-30", "2009-11-23", "2009-11-16", "2009-11-09", "2009-11-02", "2009-10-26", "2009-10-19", "2009-10-12", "2009-10-05", "2009-09-28", "2009-09-21", "2009-09-14", "2009-09-07", "2009-08-31", "2009-08-24", "2009-08-17", "2009-08-10", "2009-08-03", "2009-07-27", "2009-07-20", "2009-07-13", "2009-07-06", "2009-06-29", "2009-06-22", "2009-06-15", "2009-06-08", "2009-06-01", "2009-05-25", "2009-05-18", "2009-05-11", "2009-05-04", "2009-04-27", "2009-04-20", "2009-04-13", "2009-04-06", "2009-03-30", "2009-03-23", "2009-03-16", "2009-03-09", "2009-03-02", "2009-02-23", "2009-02-16", "2009-02-09", "2009-02-02", "2009-01-26", "2009-01-19", "2009-01-12", "2008-12-29", "2008-12-22", "2008-12-15", "2008-12-08", "2008-12-01", "2008-11-24", "2008-11-17", "2008-11-10", "2008-11-03", "2008-10-27", "2008-10-20", "2008-10-13", "2008-10-06", "2008-09-29", "2008-09-22", "2008-09-15", "2008-09-08", "2008-09-01", "2008-08-25", "2008-08-18", "2008-08-11", "2008-08-04", "2008-07-28", "2008-07-21", "2008-07-14", "2008-07-07", "2008-06-30", "2008-06-23", "2008-06-16", "2008-06-09", "2008-06-02", "2008-05-26", "2008-05-19", "2008-05-12", "2008-05-05", "2008-04-28", "2008-04-21", "2008-04-14", "2008-04-07");
//最初にCSVファイルに入った以前のデータを空にしておく
$file = fopen(FILE_NAME, 'w');
ftruncate($file, 0);
fclose($file);
//再び追記モードでファイルを開く
$file = fopen(FILE_NAME, 'a');
fwrite($file, encodeToSJIS("ランキング週,ランキング,タイトル,著者,出版社,発売日,価格(税込み),推定売上部数\n"));//1行目に項目行を出力
foreach ($dateList as $date) {
//DOMをパースし必要なデータを変数に代入
$WeeklyBookDatas = getWeeklyBookDatas($date);
//CSVデータ出力&結果メッセージ出力
outputCsvData($file, $WeeklyBookDatas);
}
//終了処理
fclose($file);
echo "******** Success!! **********";
///////////////////////////
///// 以下クラス・メソッド群 /////
///////////////////////////
//引数指定された日にち付けのURLの売上TOP30の週次データを取得する
function getWeeklyBookDatas($date){
$WeeklyBookDatas = array();
for ($pageNum=1; $pageNum<=3 ; $pageNum++) { //1ページにつき10タイトルしか載っていないため、3ページ見る必要がある
$html = file_get_html(URL_PRE.$date.URL_MID.$pageNum.URL_SUF);
if($html != ""){
$rank = $pageNum * 10 - 9;
foreach($html->find('div[class=inner] div[class=wrap-text]') as $book){
$title = $book->find('h2', 0)->plaintext;
$author = $book->find('p', 0)->plaintext;
$elems = array();
foreach($book->find('ul', 0)->find('li') as $elem){
$elems[] = $elem->plaintext;
}
array_push($WeeklyBookDatas ,new WeeklyBookData(
$date, $rank++, $title, $author, $elems[0], $elems[1], $elems[2], $elems[3]));
}
}else {
throw new Exception("**********Failure!! Can't get file!!***********");
}
}
return $WeeklyBookDatas;
}
//週次データ群をCSVファイルとして出力する
function outputCsvData($file, $WeeklyBookDatas){
//1WeeklyBookDataデータあたり1行で出力
foreach ($WeeklyBookDatas as $WeeklyBookData) {
fwrite($file, $WeeklyBookData->getCsvFormattedData());
}
}
//UTF-8 ⇒ SHIFT-JIS に文字コード変換
//*本PHPファイルはUTF-8だが、Excelに出力する値などはShift-JISにする必要があるのでその際使用する
function encodeToSJIS($str){
return mb_convert_encoding($str, "SJIS", "UTF-8");
}
/**
* 1つの週次本データを表すオブジェクト
*/
class WeeklyBookData{
private $date;//ランキング週
private $rank;//ランキング
private $title;//タイトル
private $author;//著者
private $company;//出版社
private $publishDate;//発売日
private $price;//価格(税込み)
private $publishNum;//推定売上部数
public function __construct($date, $rank, $title, $author, $company, $publishDate, $price, $publishNum){
$this->date = $date;
$this->rank = $rank;
$this->title = str_replace(encodeToSJIS(","), "", $title);
$this->author = str_replace(encodeToSJIS(","), "", $author);
$this->company = str_replace(array(encodeToSJIS("出版社:"), encodeToSJIS(",")), "", $company);
$this->publishDate = str_replace(encodeToSJIS("発売日:"), "", $publishDate);
$this->price = str_replace(array(encodeToSJIS("価格:"), encodeToSJIS("円(税込)")), "", $price);
$this->publishNum = str_replace(array(encodeToSJIS("推定売上部数:"), encodeToSJIS(","), encodeToSJIS("部")), "", $publishNum);
}
//全データをCSV形式の1行にして出力する
public function getCsvFormattedData(){
return $this->date.",".$this->rank.",".$this->title.",".$this->author.",".$this->company.",".$this->publishDate.",".$this->price.",".$this->publishNum."\n";
}
}