-
Notifications
You must be signed in to change notification settings - Fork 0
/
IEEE_scraper.py
executable file
·178 lines (138 loc) · 6.12 KB
/
IEEE_scraper.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
import requests
from datetime import datetime, timezone
from bs4 import BeautifulSoup
import json
import glob
class Conference:
def __init__(self, shortName, name, start, end, deadline, city, country, link, type, addressLink, note, year):
self.shortName = shortName
self.name = name
self.start = start
self.end = end
self.deadline = deadline
self.city = city
self.country = country
self.link = link
self.type = type
self.addressLink = addressLink
self.note = note
self.year = year
def as_dict(self):
return {'shortName': self.shortName, 'name': self.name, 'start': self.start, 'end': self.end, 'deadline': self.deadline, 'city': self.city, 'country': self.country, 'link': self.link, 'type': self.type, 'addressLink': self.addressLink, 'note': self.note}
def create_conference(row):
conf_title = row.find(attrs={'class':'conf-title'})
conf_title_text = ""
link = ""
link_element=conf_title.find('a')
if (link_element) :
link = link_element['href']
conf_title_text = link_element.text
else :
conf_title_text = ''.join(conf_title.find_all(string=True, recursive=False)).strip()
#print(conf_title_text)
words = conf_title_text.split()
year = words[0]
shortName = words[-1]
name = ' '.join(words[1:-1])
conf_place = row.find(attrs={'class':'conf-place'}).text
conf_place_split = conf_place.split(',')
#print(words)
city = conf_place_split[0].strip()
if (len(conf_place_split) > 1):
country = conf_place_split[1].strip()
else:
country = ""
conf_date = row.find(attrs={'class':'conf-date'}).span['title']
conf_date_split = conf_date[1:].split('-')
start = datetime.fromtimestamp(int(conf_date_split[0]), timezone.utc).strftime('%Y-%m-%d')
end = datetime.fromtimestamp(int(conf_date_split[1]), timezone.utc).strftime('%Y-%m-%d')
#deadline
deadline = ""
abstract_date_element = row.find('div', class_='conf-sub-date')
if abstract_date_element:
title_attribute = abstract_date_element.find('span', {'title': True})
timestamp = title_attribute['title'].replace('abs', '')
deadline = datetime.fromtimestamp(int(timestamp), timezone.utc).strftime('%Y-%m-%d')
#print(name)
# print(year)
# print(shortName[1:-1])
# print(city)
# print(country)
# print(start)
# print(end)
# print(" ")
return Conference(shortName[1:-1], name, start, end, deadline, city, country, link, "Conference", "", "", year)
def main():
# Send a GET request to the webpage
url = "https://www.ieee-ras.org/conferences-workshops/upcoming-conferences"
response = requests.get(url)
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
#print(soup)
confs = soup.find_all(attrs={'class':'conf-full-item'})
#print(confs)
json_confs_dict = {}
json_file_to_update = {}
# Parse all www/data/conf***.json files
json_files = glob.glob("../www/data/conf*.json")
#print(json_files)
existent_confs_dict = {}
for file in json_files:
with open(file, 'r') as f:
year = file.split("conf")[1].split(".json")[0]
json_confs = json.load(f)
json_confs_dict[year] = json_confs
json_file_to_update[year] = False
#print(json_confs)
for json_conf in json_confs:
#print(json_conf)
#print(json_conf['shortName'])
if year in existent_confs_dict:
existent_confs_dict[year][json_conf['shortName'].upper()] = json_conf
else:
existent_confs_dict[year] = {json_conf['shortName'].upper(): json_conf}
offset_insertion = 0
for row in confs:
scraped_conf = create_conference(row)
if not (scraped_conf.shortName.upper() in existent_confs_dict[scraped_conf.year]):
print("Adding ", scraped_conf.shortName, scraped_conf.year)
json_file_to_update[scraped_conf.year] = True
index_to_insert = 0
#to keep track of the inserted one, since we can not updated existent_confs_dict inserting in "the middle"
#so this is the offset to add to the index_to_insert
for shortName, conf in existent_confs_dict[scraped_conf.year].items():
conf_date_start = datetime.strptime(conf['start'], '%Y-%m-%d')
scraped_conf_date_start = datetime.strptime(scraped_conf.start, '%Y-%m-%d')
#print(conf_date_start)
#print(scraped_conf_date_start)
if (conf_date_start < scraped_conf_date_start):
index_to_insert += 1
elif (conf_date_start == scraped_conf_date_start):
conf_date_end = datetime.strptime(conf['end'], '%Y-%m-%d')
scraped_conf_date_end = datetime.strptime(scraped_conf.end, '%Y-%m-%d')
if (conf_date_end < scraped_conf_date_end):
#must be put after, otherwise it will be put before
index_to_insert += 1
# do not consider the case when two conf has the same init date, and different end date... not so important having so detailed order
break
else:
break
json_confs_dict[scraped_conf.year].insert(index_to_insert+offset_insertion, scraped_conf.as_dict())
offset_insertion += 1
updated = False
#dump on file
for year, json_confs in json_confs_dict.items():
json_file_path = "../www/data/conf" + year + ".json"
if json_file_to_update[year]:
with open(json_file_path, 'w') as file:
json.dump(json_confs, file, indent=2)
file.close()
print("Updated file: ", json_file_path)
updated = True
else:
print("No update for file: ", json_file_path)
return updated
if __name__ == "__main__":
import sys
result = main()
sys.exit(0 if result else 1)