This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrap.php
156 lines (125 loc) · 4.34 KB
/
scrap.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
const BASE_URL = 'https://del.icio.us';
$return = [];
class ScrapDeliciousPage
{
public $username;
public $page;
public $total_pages;
public $errors = [];
public $bookmarks = [];
public $source_url = [];
public function __construct()
{
include('simple_html_dom.php');
}
public function init()
{
if (isset($_GET['username'])) {
$this->username = strtolower($_GET['username']);
}
if (isset($_GET['page'])) {
$this->page = $_GET['page'];
}
$args = '';
if ($this->page && $this->page != 'all') {
$args = [];
$args[] = 'page=' . $this->page;
}
if (!empty($args)) {
$args = '/?' . implode('&', $args);
}
$this->source_url = BASE_URL . '/' . $this->username . $args;
if ($this->username) {
$this->fetchData();
} else {
$this->errors[] = 'Username missing.';
return;
}
}
public function fetchData()
{
if ($this->page == 1) {
// test target website
$headers_site = @get_headers(BASE_URL);
if (strpos($headers_site[0],'200') === false) {
$this->errors[] = 'Something went wrong with the Delicious website (' . BASE_URL .').';
return;
}
// test target page
$headers_page = @get_headers($this->source_url);
if (strpos($headers_page[0],'200') === false) {
$this->errors[] = 'The user <b>' . $this->username . '</b> doesn\'t exists or has been deleted.';
return;
}
}
$this->bookmarks = array_merge($this->bookmarks, $this->getPageData($this->source_url));
}
private function getPageData($url)
{
$page_data = [];
$source = file_get_html($url);
$items = $source->find('.articleThumbBlockOuter');
$this->total_pages = $source->find('.pagination', 0)->children((count($source->find('.pagination li')) - 2))->plaintext;
foreach ($items as $item) {
$description = null;
if ($item->find('.thumbTBriefTxt', 0)->children(1)) {
$description = $item->find('.thumbTBriefTxt', 0)->children(1)->plaintext;
}
$date = $item->find('.articleInfoPan', 0)->children(2)->plaintext;
$date = str_replace('This link recently saved by ' . $this->username . ' on ', '', $date);
$date = new DateTime($date);
$date = $date->getTimestamp();
$tags = [];
foreach ($item->find('.tagName li') as $tag) {
$tags[] = $tag->plaintext;
}
$page_data[] = [
'title' => $item->find('h3', 0)->plaintext,
'url' => $item->find('.articleInfoPan', 0)->children(0)->find('a', 0)->href,
'domain' => $item->find('.articleInfoPan', 0)->children(1)->plaintext,
'date' => $date,
'description' => $description,
'tags' => $tags,
];
}
$this->saveBookmarks($page_data);
return $page_data;
}
private function saveBookmarks($page_data)
{
session_start();
if ($this->page == 1) {
session_unset();
}
$scrappedBookmarks = isset($_SESSION["scrappedBookmarks"]) && is_array($_SESSION["scrappedBookmarks"]) ? $_SESSION["scrappedBookmarks"] : [];
$_SESSION["scrappedBookmarks"] = array_merge($scrappedBookmarks, $page_data);
}
}
$scrap = new ScrapDeliciousPage();
$scrap->init();
$errors = $scrap->errors;
if (!empty($errors)) {
$return['message'] = 'error';
$return['errors'] = $errors;
echo json_encode($return);
return;
}
$return['bookmarks'] = [];
$return['page'] = $scrap->page;
if ($scrap->page == 1) {
$return['total_pages'] = $scrap->total_pages;
}
foreach ($scrap->bookmarks as $bookmark) {
$return['bookmarks'][] = [
'title' => $bookmark['title'],
'url' => $bookmark['url'],
'domain' => $bookmark['domain'],
'date' => $bookmark['date'],
'description' => $bookmark['description'],
'tags' => $bookmark['tags'],
];
}
$return['message'] = 'ok';
echo json_encode($return);
?>