-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.py
183 lines (145 loc) · 5.37 KB
/
library.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
from os.path import exists
import pandas as pd
from pandas.core.frame import DataFrame
import os
from datetime import datetime
from bs4 import BeautifulSoup
import json
import re
import selenium
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
opts = webdriver.ChromeOptions()
opts.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options=opts)
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
def check_file_exists(filename):
"""
return if file exists in the path
input: filename (str)
output: (bool)
"""
if exists(filename):
return True
return False
def modify_df(df):
"""
return a modified dataframe
input: df (dataframe)
output: df (dataframe)
"""
column = df.columns
df = df.T.reset_index(drop=True)
df['id'] = column
cols = list(df.columns)
cols = [cols[-1]] + cols[:-1]
df = df[cols]
return df
def create_dir(parent_dir, year):
"""
creates a directory in the with the path
return a path of the directory
input: parent_dir (str), year (str)
output: path (str)
"""
path = os.path.join(parent_dir,year)
os.mkdir(path)
return path
def alternative_scrape(url, month):
# driver = webdriver.Chrome('./chromedriver')
driver.get(url)
time.sleep(5)
html_source = driver.page_source
soup = BeautifulSoup(html_source,'lxml')
header = ['id','title','authorids','authors','TL;DR','abstract','pdf','software','preprint','existing_preprints','preferred_venue','consent','paperhash','reviewer/Editor_reassignment_request','reviewer/Editor_reassignment_justification','data','previous_URL','previous_PDF','response_PDF']
df = pd.DataFrame(columns=header)
find = soup.find_all("li", {"class": "note"})
count = len(find)
if count == 0:
return df
c = 1
printProgressBar(0, count, prefix = month + ' Progress:', suffix = 'Complete', length = 50)
for titles in find:
printProgressBar(c, count, prefix = month + ' Progress:', suffix = 'Complete', length = 50)
c+=1
data = dict()
id = titles.get('data-id')
if id is None:
continue
# print(id)
h4 = titles.find('h4')
links = h4.find_all('a')
title = " ".join(links[0].text.split())
forum = links[0].get('href')
pdf = links[1].get('href')
data['id'] = id
data['title'] = title
data['forum'] = forum
data['pdf'] = pdf
authors = " ".join(titles.find('div', {"class": "note-authors"}).text.split())
# print(authors)
data['author'] = authors
details = titles.find('ul', {"class": "list-unstyled note-content"})
items = [" ".join(i.text.split())[:-1] for i in details.find_all('strong', {"class": "note-content-field"})]
# print(items)
contents = [" ".join(i.text.split()) for i in details.find_all('span', {"class": "note-content-value"})]
# print(contents)
for item, content in zip(items, contents):
try:
if item != 'Previous URL' and item != 'Abstract':
t = "Download " + item
content = details.find('a', {"title": t}).get('href')
data[item] = content
else:
data[item] = content
except:
pass
# print(data)
df = df.append(data, ignore_index = True)
print()
# print(df)
return df
def get_ids_from_page(main_page, func=None):
"""
get the information needed based on which func to use
input: main_page (request), func (year, month, None)
output: list of ids on the page
"""
# beautifulSoup by using lxml to get the raw html and store in soup with different class
soup = BeautifulSoup(main_page.content, "lxml")
# find the script that contains the json data
script = soup.find("script", id="__NEXT_DATA__")
# loading the script into json format
json_object = json.loads(script.string)
# using regex for parsing the id of each paper
if func == 'year':
years = re.findall(r"id=aclweb.org/ACL/ARR/(.+?)\"", str(json_object['props']))
return years
elif func == 'month':
ids = re.findall(r"\"url\": \"(.+?)\"", str(json_object['props']))
months = [a.split('/')[-1] for a in ids]
return months
else:
ids = re.findall(r"forum\?id=(.+?)\\", str(json_object['props']))
return ids