This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.php
102 lines (86 loc) · 2.72 KB
/
parser.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
<?php
require_once("vendor/autoload.php");
//$api = 'http://terbets.id.lv/tl/1.js';
//$api = 'http://terbets.id.lv/tl/2.js';
$api ="http://live.afreecatv.com/afreeca/broad_list_api.php";
$curl_log = fopen(dirname(__FILE__).'/curl_log.txt', 'a');
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $curl_log,
CURLOPT_URL => $api
));
$response = curl_exec($ch);
$output= fread($curl_log, 2048);
fclose($curl_log);
curl_close($ch);
// convert to utf-8, lose all Korean symbols in the process
// convert the js array into a valid json array
// remove js array definition
// remove trailing ;
// convert single ticks to double ticks
// remove slashes
$response = mb_convert_encoding($response, 'UTF-8', 'UTF-8');
$old = array("var oBroadListData = ", "}};", "'", "\\");
$new = array("", "}}", '"', "");
$response = str_replace($old, $new, $response);
$json = json_decode($response, true);
// log error
// echo json_last_error();
echo "<pre>";
//print_r($json);
echo "</pre>";
$aa = [];
$AfreecaLiveStreamList = arraySearch('LIVE', $json['CHANNEL']['REAL_BROAD'],$aa);
echo "<pre>";
print_r($AfreecaLiveStreamList);
echo "</pre>";
// check if we retrieved stream list. If we did not, abandon.
// error log
// remove all existing streams in DB
// add new streams in DB
try {
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, DB_USER, DB_PASS, $opt);
if ($pdo->connect_error){
error_log("Connection failed " . $conn->connect_error . "\n", 3, __DIR__ . "/error_log.txt");
die;
}
$sql = "DELETE FROM afreecatv_streams WHERE 1";
$pdo->query($sql);
$sql = 'INSERT INTO afreecatv_streams (name) VALUES ';
$insertQuery = array();
$insertData = array();
foreach ($AfreecaLiveStreamList as $row) {
$insertQuery[] = '(?)';
$insertData[] = $row;
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $pdo->prepare($sql);
$stmt->execute($insertData);
}
echo "New records added successfully to DB.";
} catch (PDOException $e) {
error_log("Database exception: " . $e->getMessage() . "\n", 3, __DIR__ . "/error_log.txt");
die;
}
$pdo = null;
// add all user ids into array
function arraySearch($searchCriteria, $array, $resultArray) {
foreach ($array as $key => $val) {
//if ($val['content_type'] === $searchCriteria) {
//if ($val['total_view_cnt'] > 0) {
array_push($resultArray, $val['user_id']);
//array_push($resultArray, $val['total_view_cnt']);
//}
}
return $resultArray;
}
?>