Skip to content

Commit

Permalink
feat(events): added events application
Browse files Browse the repository at this point in the history
  • Loading branch information
iarp committed Apr 7, 2022
1 parent cde740f commit 4b32436
Show file tree
Hide file tree
Showing 63 changed files with 3,233 additions and 3 deletions.
Empty file added browser.py
Empty file.
28 changes: 28 additions & 0 deletions core/utils.py
@@ -0,0 +1,28 @@
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.urls.exceptions import NoReverseMatch


def redirect_next(request, default):
"""Redirects to a given page, except if the session has a next value given.
Args:
request: WSGIRequest object
default: Default location to go to if no next is given.
Returns:
HttpResponseRedirect
"""
if request:
if request.session.get("next"):
default = request.session["next"]
del request.session["next"]
elif request.GET.get("next"):
default = request.GET.get("next")
elif request.POST.get("next"):
default = request.POST.get("next")

try:
return redirect(default)
except NoReverseMatch:
return HttpResponseRedirect(default)
Empty file added events/__init__.py
Empty file.
93 changes: 93 additions & 0 deletions events/admin.py
@@ -0,0 +1,93 @@
from django.contrib import admin

from events.models import Association, Exhibition, Rink, Tournament

# from daterange_filter.filter import DateRangeFilter


@admin.register(Association)
class AssociationAdmin(admin.ModelAdmin):
list_display = [
"name",
"full_name",
"location",
"website",
"exhibition_is_in_ohf",
"inserted",
]
ordering = ["weight"]


@admin.register(Tournament)
class TournamentAdmin(admin.ModelAdmin):
# list_filter = ['association', 'verified', 'source', ('start_date', DateRangeFilter)]
list_filter = ["association", "verified", "source"]
list_display = (
"association",
"sanction_number",
"name",
"location",
"start_date",
"verified",
"inserted_by",
"permits_count",
)
search_fields = ["sanction_number", "start_date", "divisions"]
ordering = [
"verified",
"start_date",
]

raw_id_fields = ["inserted_by"]

fieldsets = (
(
None,
{
"fields": (
"association",
"name",
"sanction_number",
"location",
("start_date", "end_date"),
)
},
),
("Optional Data", {"fields": ("divisions",)}),
(
"Advanced options",
{
"fields": (
"source",
"verified",
"verified_date",
"inserted_by",
"website",
"notes",
)
},
),
)

def permits_count(self, obj):
return obj.travelpermit_set.count()

permits_count.short_description = "Permits"


@admin.register(Exhibition)
class ExhibitionAdmin(admin.ModelAdmin):
list_display = [
"other_team",
"other_team_association",
"destination",
"arena",
"start_date",
]

raw_id_fields = ["inserted_by"]


@admin.register(Rink)
class RinkAdmin(admin.ModelAdmin):
pass
8 changes: 8 additions & 0 deletions events/apps.py
@@ -0,0 +1,8 @@
from django.apps import AppConfig


class EventsConfig(AppConfig):
name = "events"

# def ready(self):
# import events.signals
78 changes: 78 additions & 0 deletions events/association_profiles/GTHL.py
@@ -0,0 +1,78 @@
import datetime
import logging

from bs4 import BeautifulSoup
from selenium.common.exceptions import WebDriverException

log = logging.getLogger("events.commands.get_tournament_listing")

ENABLED = True


def scan(browser, association):

try:
browser.wait_until(element_name="btnList")
except AttributeError:
log.exception(
f'Unable to find submit button with name="btnList", skipping {association}'
)
return None
except WebDriverException:
try:
browser.save_screenshot(sub_folder="WebDriverException")
except:
log.exception("Screenshot Failure")
return None

browser.get_element(element_name="btnList").click()

soup = BeautifulSoup(browser.browser.page_source, "html.parser")

table = soup.find("table", {"class": "tblBorder"})
table_body = table.find("tbody")

columns = [
"Sanction Number",
"Tournament Name",
"Centre",
"Start Date",
"End Date",
"Divisions",
"Details",
]
rows = []

for row_index, row in enumerate(table_body.find_all("tr")):
row_data = {}

cols = [ele.text.strip() for ele in row.find_all("td")]

if not row_index:
continue

for index, col in enumerate(columns):
row_data[col] = cols[index]

try:
row_data["Start Date"] = datetime.datetime.strptime(
row_data["Start Date"], "%d-%b-%Y"
).date()
except ValueError:
log.exception(
"{}: bad Start/End dates, expecting dd-Mth-YYYY, received {} and {}".format(
association.name, row_data["Start Date"], row_data["End Date"]
)
)
continue

try:
row_data["End Date"] = datetime.datetime.strptime(
row_data["End Date"], "%d-%b-%Y"
).date()
except ValueError:
row_data["End Date"] = row_data["Start Date"]

rows.append(row_data)

return rows
120 changes: 120 additions & 0 deletions events/association_profiles/HEO.py
@@ -0,0 +1,120 @@
"""
I've left this script unfinished. HEO does not publish the sanction number
on the main search table, you must visit each events page to get that number.
I do not want to be hitting their site once a week and then hitting 100+ event
pages to check and see if we already have the sanction number in our system.
I let Nathan know HEO is not possible for the time being.
Why not match on title? What happens if the same title was used last year?
Why not match on date and title? What happens when either of those values
change, even in the slightest?
It scans the first page (i did not add the system to follow to the next page) and
finds the events sanction number on the events page.
"""
import datetime
import logging
import traceback

from bs4 import BeautifulSoup

from browser import SKBrowserBase

# from selenium.common.exceptions import WebDriverException

log = logging.getLogger("events.commands.get_tournament_listing")

ENABLED = False


def scan(browser: SKBrowserBase, association):

raise ValueError("HEO is disabled at this time.")

# try:
# browser.wait_until(element_id='edit-submit-tournament-listing')
# except AttributeError:
# log.debug(f'Unable to find Apply button with id="edit-submit-tournament-listing", skipping site: {association.name} {association.tournament_listing_url}')
# return None
# except WebDriverException:
# try:
# browser.save_screenshot(sub_folder='WebDriverException')
# except:
# log.debug(traceback.format_exc())
# return None

# with open('cache/heo-listing.html', 'w') as fw:
# fw.write(browser.browser.page_source)
with open("cache/heo-listing.html", "r") as fo:
source = fo.read()

# soup = BeautifulSoup(browser.browser.page_source, "html.parser")
soup = BeautifulSoup(source, "html.parser")

table = soup.find("table", {"class": "views-table"})
table_body = table.find("tbody")

columns = ["Date", "Tournament Name", "HLComp", "BodyChk", "Divisions"]

rows = []

for row_index, row in enumerate(table_body.find_all("tr")):
row_data = {}

cols = [ele.text.strip() for ele in row.find_all("td")]

if not row_index:
continue

for index, col in enumerate(columns):
row_data[col] = cols[index]

link_to_event = row.find("td", {"class": "views-field-title"}).find(
"a", href=True
)["href"]
link_to_event = f"http://www.heominor.ca{link_to_event}"

try:
start_raw, end_raw = row_data["Date"].split(" to ", 1)

row_data["Start Date"] = datetime.datetime.strptime(
start_raw, "%b %d %Y"
).date()

try:
row_data["End Date"] = datetime.datetime.strptime(
end_raw, "%b %d %Y"
).date()
except ValueError:
row_data["End Date"] = row_data["Start Date"]

except ValueError:
log.debug(traceback.format_exc())
log.debug(
"{}: bad Start/End dates, expecting dd-Mth-YYYY, received {} and {}".format(
association.name, row_data["Start Date"], row_data["End Date"]
)
)
continue

print(row)
print(row_data)

browser.load_url(link_to_event)
with open("cache/heo-listing-event.html", "w") as fw:
fw.write(browser.browser.page_source)
# with open('cache/heo-listing-event.html', 'r') as fo:
# source = fo.read()

soup = BeautifulSoup(browser.browser.page_source, "html.parser")
# soup = BeautifulSoup(source, 'html.parser')

sanction_number = (
soup.find("div", {"class": "field-name-field-sanction-number"})
.find("div", {"class": "field-items"})
.text
)

row_data["Sanction Number"] = sanction_number

return

0 comments on commit 4b32436

Please sign in to comment.