-
Notifications
You must be signed in to change notification settings - Fork 1
/
putusan-ma.py
291 lines (251 loc) · 8.27 KB
/
putusan-ma.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
author: Okky Mabruri
maintainer: Okky Mabruri <[email protected]>
"""
import argparse
import io
import os
import re
import time
import urllib
from concurrent.futures import ThreadPoolExecutor, wait
from datetime import date
import pandas as pd
import requests
from bs4 import BeautifulSoup
from pdfminer import high_level
import utils
def get_args(argv=None):
parser = argparse.ArgumentParser(
description="Putusan Mahkamah Agung Scraper", add_help=True
)
parser.add_argument(
"-k",
"--keyword",
required=False,
dest="keyword",
help="keyword for the Supreme Court",
)
parser.add_argument(
"-u",
"--url",
required=False,
dest="url",
help=" specify url for the Supreme Court\nexample: https://putusan3.mahkamahagung.go.id/search.html?cat=98821d8a4bc63aff3a81f66c37934f56",
)
parser.add_argument(
"-sd",
"--sortdate",
dest="sort_date",
required=False,
default=False,
action="store_true",
help="(optional) scraping from newest putusan. Default False",
)
parser.add_argument(
"-dp",
"--downloadpdf",
dest="download_pdf",
required=False,
default=False,
action="store_true",
help="(optional) download pdf. Default False",
)
return parser.parse_args(argv)
def open_page(link):
count = 0
while count < 3:
try:
return BeautifulSoup(requests.get(link).text, "lxml")
except:
count += 1
time.sleep(5)
def get_detail(soup, keyword):
try:
text = (
soup.find(lambda tag: tag.name == "td" and keyword in tag.text)
.find_next()
.get_text()
.strip()
)
return text
except:
return ""
def get_pdf(url, path_pdf, download_pdf):
file = urllib.request.urlopen(url)
file_name = file.info().get_filename().replace("/", " ")
# with file:
# file = file.read()
file = file.read()
if download_pdf:
with open(f"{path_pdf}/{file_name}", "wb") as out_file:
out_file.write(file)
return io.BytesIO(file), file_name
def clean_text(text):
text = text.replace("M a h ka m a h A g u n g R e p u blik In d o n esia\n", "")
text = text.replace("Disclaimer\n", "")
text = text.replace(
"Kepaniteraan Mahkamah Agung Republik Indonesia berusaha untuk selalu mencantumkan informasi paling kini dan akurat sebagai bentuk komitmen Mahkamah Agung untuk pelayanan publik, transparansi dan akuntabilitas\n",
"",
)
text = text.replace(
"pelaksanaan fungsi peradilan. Namun dalam hal-hal tertentu masih dimungkinkan terjadi permasalahan teknis terkait dengan akurasi dan keterkinian informasi yang kami sajikan, hal mana akan terus kami perbaiki dari waktu kewaktu.\n",
"",
)
text = text.replace(
"Dalam hal Anda menemukan inakurasi informasi yang termuat pada situs ini atau informasi yang seharusnya ada, namun belum tersedia, maka harap segera hubungi Kepaniteraan Mahkamah Agung RI melalui :\n",
"",
)
text = text.replace(
"Email : [email protected] Telp : 021-384 3348 (ext.318)\n",
"",
)
return text
def extract_data(link, keyword_url):
global today
global path_output
global path_pdf
global download_pdf
soup = open_page(link)
table = soup.find("table", {"class": "table"})
judul = table.find("h2").text
soup.find("table", {"class": "table"}).find("h2").decompose()
nomor = get_detail(table, "Nomor")
tingkat_proses = get_detail(table, "Tingkat Proses")
klasifikasi = get_detail(table, "Klasifikasi")
kata_kunci = get_detail(table, "Kata Kunci")
tahun = get_detail(table, "Tahun")
tanggal_register = get_detail(table, "Tanggal Register")
lembaga_peradilan = get_detail(table, "Lembaga Peradilan")
jenis_lembaga_peradilan = get_detail(table, "Jenis Lembaga Peradilan")
hakim_ketua = get_detail(table, "Hakim Ketua")
hakim_anggota = get_detail(table, "Hakim Anggota")
panitera = get_detail(table, "Panitera")
amar = get_detail(table, "Amar")
amar_lainnya = get_detail(table, "Amar Lainnya")
catatan_amar = get_detail(table, "Catatan Amar")
tanggal_musyawarah = get_detail(table, "Tanggal Musyawarah")
tanggal_dibacakan = get_detail(table, "Tanggal Dibacakan")
kaidah = get_detail(table, "Kaidah")
abstrak = get_detail(table, "Abstrak")
try:
link_pdf = soup.find("a", href=re.compile(r"/pdf/"))["href"]
file_pdf, file_name_pdf = get_pdf(link_pdf, path_pdf, download_pdf)
text_pdf = high_level.extract_text(file_pdf)
text_pdf = clean_text(text_pdf)
except:
link_pdf = ""
text_pdf = ""
file_name_pdf = ""
data = [
judul,
nomor,
tingkat_proses,
klasifikasi,
kata_kunci,
tahun,
tanggal_register,
lembaga_peradilan,
jenis_lembaga_peradilan,
hakim_ketua,
hakim_anggota,
panitera,
amar,
amar_lainnya,
catatan_amar,
tanggal_musyawarah,
tanggal_dibacakan,
kaidah,
abstrak,
link,
link_pdf,
file_name_pdf,
text_pdf,
]
result = pd.DataFrame(
[data],
columns=[
"judul",
"nomor",
"tingkat_proses",
"klasifikasi",
"kata_kunci",
"tahun",
"tanggal_register",
"lembaga_peradilan",
"jenis_lembaga_peradilan",
"hakim_ketua",
"hakim_anggota",
"panitera",
"amar",
"amar_lainnya",
"catatan_amar",
"tanggal_musyawarah",
"tanggal_dibacakan",
"kaidah",
"abstrak",
"link",
"link_pdf",
"file_name_pdf",
"text_pdf",
],
)
keyword_url = keyword_url.replace("/", " ")
if keyword_url.startswith("https"):
keyword_url = ""
destination = f"{path_output}/putusan_ma_{keyword_url}_{today}"
print(destination)
if not os.path.isfile(f"{destination}.csv"):
result.to_csv(f"{destination}.csv", header=True, index=False)
else:
result.to_csv(f"{destination}.csv", mode="a", header=False, index=False)
def run_process(keyword_url, page, sort_page):
if keyword_url.startswith("https"):
link = f"{keyword_url}&page={page}"
else:
link = f"https://putusan3.mahkamahagung.go.id/search.html?q={keyword_url}&page={page}"
if sort_page:
link = f"{link}&obf=TANGGAL_PUTUS&obm=desc"
print(link)
soup = open_page(link)
links = soup.find_all("a", {"href": re.compile("/direktori/putusan")})
for link in links:
extract_data(link["href"], keyword_url)
if __name__ == "__main__":
args = get_args()
keyword = args.keyword
url = args.url
sort_date = args.sort_date
download_pdf = args.download_pdf
if not keyword and not url:
exit("Please provide keyword or URL")
# keyword = "Pdt.Sus-BPSK"
path_output = utils.create_path("putusan")
path_pdf = utils.create_path("pdf-putusan")
today = date.today().strftime("%Y-%m-%d")
link = f"https://putusan3.mahkamahagung.go.id/search.html?q={keyword}&page=1"
if url:
link = url
soup = open_page(link)
last_page = int(
soup.find_all("a", {"class": "page-link"})[-1].get("data-ci-pagination-page")
)
if url:
print(f"Scraping with url: {url} - {20 * last_page} data - {last_page} page")
else:
print(
f"Scraping with keyword: {keyword} - {20 * last_page} data - {last_page} page"
)
# url = "https://putusan3.mahkamahagung.go.id/search.html?q=&jenis_doc=&cat=98821d8a4bc63aff3a81f66c37934f56"
# url = "https://putusan3.mahkamahagung.go.id/search.html?cat=98821d8a4bc63aff3a81f66c37934f56&page=2"
if url:
keyword_url = url
else:
keyword_url = keyword
futures = []
with ThreadPoolExecutor(max_workers=4) as executor:
for page in range(last_page):
futures.append(
executor.submit(run_process, keyword_url, page + 1, sort_date)
)
wait(futures)