-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-goodreads.py
59 lines (45 loc) · 1.73 KB
/
read-goodreads.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
import pandas as pd
import re
from bs4 import BeautifulSoup
import os.path
def main():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
#filepath = input('txt file: ')
filepath = 'goodreads.txt'
html_path = os.path.join(BASE_DIR, filepath)
with open(html_path, encoding='utf-8') as namafile:
soup = BeautifulSoup(namafile.read(), features="html.parser")
judul = soup.findAll('td',{'class':'field title'})
penulis = soup.findAll('td',{'class':'field author'})
tanggal = soup.findAll('td',{'class':'field date_read'})
title = []
author = []
date = []
for baris in judul:
title.append(baris.find('a').get_text().strip())
for baris in penulis:
full = re.split(',',baris.find('a').get_text().strip())
try: fullname = full[1].strip()+' '+full[0]
except: fullname = baris.find('a').get_text().strip()
author.append(fullname)
for baris in tanggal:
date.append(baris.find('span').get_text())
tabel = {'Title':title, 'Author':author, 'Status':'Finished', 'Progress':'', 'Book type':'', 'Highlight':'','Year':date}
df = pd.DataFrame(tabel)
date2 = []
for row in df.Year:
print(row)
try: row = datetime.strptime(row, "%b %d, %Y").strftime('%m/%d/%y')
except:
try: row = datetime.strptime(row, "%b %Y").strftime('%m/01/%y')
except: row = '0'
date2.append(row)
df['Year'] = date2
csv_path = os.path.join(BASE_DIR, 'Goodreads.csv')
if os.path.exists(csv_path):
os.remove(csv_path)
df.to_csv(csv_path, index=False)
print('fin')
return
if __name__ == '__main__':
main()