-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UTEvents #36 #56
Merged
Merged
UTEvents #36 #56
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6604978
Issue #36: Events Scrapper - init
85adbff
Base file for the Events scraper
0a87bcc
Import BeautifulSoup - HTML/XML parser and Timezone module
25b3810
Add UTEvents to module
40f6e7c
Add Campuses references
d4b0235
First layer, get the links to all the events
797c05d
Events parsing and json dumps
2cbfff5
Add UTEvents to readme.md
39c656e
Additional striping of irregular texts
17a054c
Change naming convention from UTEvents to Events
a73a68c
Use Scraper built-in functions
b3fc5d0
Change address to location and set campus to empty string on Off Camp…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from ..utils import Scraper | ||
from bs4 import BeautifulSoup, NavigableString | ||
from datetime import datetime, date | ||
from collections import OrderedDict | ||
import urllib.parse as urlparse | ||
from urllib.parse import urlencode | ||
import re | ||
|
||
class Events: | ||
"""A scraper for Events at the University of Toronto.""" | ||
host = 'https://www.events.utoronto.ca/' | ||
|
||
@staticmethod | ||
def scrape(location='.'): | ||
Scraper.logger.info('Events initialized.') | ||
Scraper.ensure_location(location) | ||
|
||
for event_link in Events.get_events_links(): | ||
doc = Events.get_event_doc(event_link) | ||
Scraper.save_json(doc, location, doc['id']) | ||
|
||
Scraper.logger.info('Events completed.') | ||
|
||
@staticmethod | ||
def get_events_links(): | ||
page_index_url = Events.host + 'index.php' | ||
url_parts = list(urlparse.urlparse(page_index_url)) | ||
events_links = [] | ||
paging_index = 1 | ||
events_count = 10 | ||
while(events_count == 10): | ||
params = { | ||
'p': paging_index | ||
} | ||
url_parts[4] = urlencode(params) | ||
paging_index += 1 | ||
html = Scraper.get(urlparse.urlunparse(url_parts)) | ||
soup = BeautifulSoup(html, 'html.parser') | ||
events_dom_arr = soup.select('#results')[0].find_all('li') | ||
events_count = len(events_dom_arr) | ||
events_links += list(map(lambda e: e.a['href'], events_dom_arr)) | ||
return(events_links) | ||
|
||
@staticmethod | ||
def get_event_doc(url_tail): | ||
event_url = Events.host + url_tail | ||
html = Scraper.get(event_url) | ||
url_parts = list(urlparse.urlparse(event_url)) | ||
query = dict(urlparse.parse_qsl(url_parts[4])) | ||
soup = BeautifulSoup(html, 'html.parser') | ||
|
||
event_id = query['eventid'] | ||
event_title = soup.select('.eventTitle')[0].text.strip() | ||
raw_time = soup.select('.date')[0].text.split(',') | ||
|
||
date_arr = raw_time[0].split(' - ') | ||
time_arr = re.split(' - | ', raw_time[1].strip()) | ||
|
||
# Some of the strings are misformed and gives an extra empty space | ||
time_arr = list(filter(None, time_arr)) | ||
event_start_date = datetime.strptime(date_arr[0], | ||
'%b %d').replace(year=date.today().year).date().isoformat() | ||
event_end_date = datetime.strptime(date_arr[-1], | ||
'%b %d').replace(year=date.today().year).date().isoformat() | ||
|
||
# Note: Some events span across several days e.g. 8350, thus specifying dates makes no sense | ||
event_meridiem = time_arr[2] | ||
event_start_time = time_arr[0] + ' ' + event_meridiem | ||
event_end_time = time_arr[1] + ' ' + event_meridiem | ||
|
||
evt_bar = soup.select('#evt_bar')[0] | ||
event_url = evt_bar.select('dd')[1].a['href'] | ||
event_price = evt_bar.select('dl')[1].dd.text | ||
|
||
event_campus = '' | ||
if evt_bar.select('dd')[0].b != None: | ||
event_campus = evt_bar.select('dd')[0].b.text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In the case of no campus / off campus, we can just leave |
||
|
||
event_address = '' | ||
address_block = evt_bar.select('dd')[0] | ||
if address_block.a != None: | ||
address_block = address_block.a | ||
for content in address_block.contents: | ||
text = content if type(content) == NavigableString else content.text | ||
event_address += text.strip().replace('\r', '') + ' ' | ||
event_address = event_address.strip() | ||
|
||
event_audiences = list(map(lambda a: a.text, | ||
evt_bar.select('dl')[1].select('dd')[1].select('a'))) | ||
|
||
soup.select('.eventTitle')[0].extract() | ||
soup.select('.date')[0].extract() | ||
evt_bar.extract() | ||
soup.select('#cal_bar')[0].extract() | ||
event_description = '' | ||
for content in soup.select('#content')[0].contents: | ||
text = content if type(content) == NavigableString else content.text | ||
event_description += text.strip().replace('\r', '') + ' ' | ||
event_description = event_description.strip() | ||
|
||
doc = OrderedDict([ | ||
('id', event_id), | ||
('title', event_title), | ||
('start_date', event_start_date), | ||
('end_date', event_end_date), | ||
('start_time', event_start_time), | ||
('end_time', event_end_time), | ||
('url', event_url), | ||
('description', event_description), | ||
('admission_price', event_price), | ||
('campus', event_campus), | ||
('location', event_address), | ||
('audiences', event_audiences), | ||
]) | ||
return doc |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed.