|
1
|
|
|
"""School name validation""" |
|
2
|
|
|
import os |
|
3
|
|
|
import logging |
|
4
|
|
|
from datetime import datetime |
|
5
|
|
|
import aiohttp |
|
6
|
|
|
import pandas as pd |
|
7
|
|
|
|
|
8
|
|
|
log = logging.getLogger("bot") |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
async def update_list(bot, download: bool = False) -> None: |
|
12
|
|
|
""" |
|
13
|
|
|
Update list |
|
14
|
|
|
|
|
15
|
|
|
**Asynchronous Function** |
|
16
|
|
|
|
|
17
|
|
|
Update the school list |
|
18
|
|
|
:param bot: The bot class to update the dataframe for |
|
19
|
|
|
:param download: Download the list |
|
20
|
|
|
:return: None |
|
21
|
|
|
""" |
|
22
|
|
|
if download: |
|
23
|
|
|
log.info("Downloading new list") |
|
24
|
|
|
try: |
|
25
|
|
|
os.replace("school_list.csv", "school_list.csv.bak") |
|
26
|
|
|
except OSError: |
|
27
|
|
|
log.debug("School list did not exist") |
|
28
|
|
|
csv_url = "https://raw.githubusercontent.com/Competitive-Cyber-Clubs/School-List/master/school_list.csv" # noqa: E501 pylint: disable=line-too-long |
|
29
|
|
|
with open("school_list.csv", mode="w", encoding="utf-8") as new_list: |
|
30
|
|
|
async with aiohttp.ClientSession() as session, session.get(csv_url) as resp: |
|
31
|
|
|
new_list.write("\n".join((await resp.text(encoding="utf-8")).splitlines())) |
|
32
|
|
|
bot.list_updated = datetime.utcnow() |
|
33
|
|
|
bot.school_list = pd.read_csv("school_list.csv", encoding="utf-8") |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
async def school_check(school_list: pd.DataFrame, name: str) -> bool: |
|
37
|
|
|
""" |
|
38
|
|
|
School check |
|
39
|
|
|
|
|
40
|
|
|
**Asynchronous Function** |
|
41
|
|
|
|
|
42
|
|
|
Checks to see if the name is in school_list.csv |
|
43
|
|
|
|
|
44
|
|
|
:param school_list: Dataframe of lists |
|
45
|
|
|
:type school_list: pandas.DataFrame |
|
46
|
|
|
:param name: Name of the school that the member wants to check against the list. |
|
47
|
|
|
:type name: str |
|
48
|
|
|
:return: Name is in school_list |
|
49
|
|
|
""" |
|
50
|
|
|
return name in school_list.Institution_Name.values |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
async def region_select(school_list: pd.DataFrame, name: str) -> str: |
|
54
|
|
|
""" |
|
55
|
|
|
Region select |
|
56
|
|
|
|
|
57
|
|
|
**Asynchronous Function** |
|
58
|
|
|
|
|
59
|
|
|
Maps a regions for a school, :refs:`name`. Looks school name in the 'Institution_Name' column, |
|
60
|
|
|
if it finds a match then pulls from 'Regions' column. |
|
61
|
|
|
|
|
62
|
|
|
:param school_list: Dataframe of schools |
|
63
|
|
|
:type school_list: pandas.DataFrame |
|
64
|
|
|
:param name: Name of the school that needs a region. |
|
65
|
|
|
:type name: str |
|
66
|
|
|
:return: |
|
67
|
|
|
""" |
|
68
|
|
|
return school_list.Regions.values[school_list["Institution_Name"] == name][0] |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
async def school_search(school_list: pd.DataFrame, name: str) -> list: |
|
72
|
|
|
""" |
|
73
|
|
|
School Search |
|
74
|
|
|
|
|
75
|
|
|
**Asynchronous Function** |
|
76
|
|
|
|
|
77
|
|
|
Searches for part of a school name in the 'Institution_Name' column in school_list. |
|
78
|
|
|
Turns the 'Institution_Name' into a series then gets all names using pandas.Series.str.contains. |
|
79
|
|
|
|
|
80
|
|
|
:param school_list: Dataframe of schools |
|
81
|
|
|
:type school_list: pandas.DataFrame |
|
82
|
|
|
:param name: Part of the name the member wants to search for. |
|
83
|
|
|
:type name: str |
|
84
|
|
|
:return: All possible matches |
|
85
|
|
|
:rtype: list |
|
86
|
|
|
""" |
|
87
|
|
|
return school_list.Institution_Name.values[ |
|
88
|
|
|
school_list["Institution_Name"].str.contains(name, case=False) |
|
89
|
|
|
] |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
async def state_list(school_list: pd.DataFrame, state: str) -> list: |
|
93
|
|
|
""" |
|
94
|
|
|
State List |
|
95
|
|
|
|
|
96
|
|
|
**Asynchronous Function** |
|
97
|
|
|
|
|
98
|
|
|
Get a list of all schools in state. |
|
99
|
|
|
|
|
100
|
|
|
:param school_list: Dataframe of schools |
|
101
|
|
|
:type school_list: pandas.DataFrame |
|
102
|
|
|
:param state: State to get all schools from. |
|
103
|
|
|
:return: Schools in the state |
|
104
|
|
|
:rtype: list |
|
105
|
|
|
""" |
|
106
|
|
|
return school_list.Institution_Name.values[school_list["States"] == state] |
|
107
|
|
|
|