-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (93 loc) · 3.18 KB
/
main.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
import re
import requests
from flask import Flask
from urllib.parse import urljoin
from bs4 import BeautifulSoup
app = Flask(__name__)
RLC = "rescue-good-food-becoming-waste"
COALITION = "staff-mobile-soup-kitchen"
BASE_URL = "https://www.newyorkcares.org"
LOGIN_URL = "/user"
# Create the payload
payload = {
"name": "",
"pass": "",
"form_id": "user_login",
"op": "Log in",
}
def login():
"""Login New York Cares and return the current session"""
with requests.Session() as s:
# Get the page first for https
s.get(urljoin(BASE_URL, LOGIN_URL))
# Post the payload to the site to log in
s.post(urljoin(BASE_URL, LOGIN_URL), data=payload)
return s
def signup_project(session, project):
"""Sign up the project with the correct regex matching"""
listing = session.get(
"https://www.newyorkcares.org/search/projects/results?location=496"
)
soup = BeautifulSoup(listing.text, "html.parser")
# Go to the last page
last_page_url = soup.find("a", class_="last")["href"]
last_page = session.get(urljoin(BASE_URL, last_page_url))
soup = BeautifulSoup(last_page.content, "html.parser")
project_links = soup.findAll("a", href=re.compile(r".*" + project + "*"))
# Find the correct project
for link in project_links:
if link.find_parent("div", class_="project").find(
string=re.compile(".*Union Square.*")
):
project_link = link
break
while not project_link and soup.find("a", class_="prev"):
prev = soup.find("a", class_="prev")["href"]
prev_page = s.get(urljoin(base_url, prev))
soup = BeautifulSoup(prev_page.content, "html.parser")
# Find Coalition
if is_coalition:
project_link = soup.find(
"a", href=re.compile(r".*" + coalition_regex + "*")
)
# Find RLC
elif is_rlc:
project_link = soup.findAll("a", href=re.compile(r".*" + rlc_regex + "*"))
# Find the Usq project
for link in project_link:
if link.find_parent("div", class_="project").find(
string=re.compile(".*Union Square.*")
):
project_link = link
break
else:
pass
if not project_link:
return
# sign up
proj = project_link.find_parent("div", class_="project")
proj_signup_link = proj.find("div", class_="sign-up").find("a")["href"]
success = s.get(urljoin(base_url, proj_signup_link))
print(success)
@app.route("/projects/coalition", methods=["GET"])
def get_coalition():
"""Sign up Coalition for the homeless"""
s = login()
signup_project(s, COALITION)
return ("Signed up Coalition", 200)
@app.route("/projects/rlc", methods=["GET"])
def get_rlc():
"""Sign up Rescue Leftover Cuisine"""
s = login()
signup_project(s, RLC)
return ("Signed up RLC", 200)
@app.route("/", methods=["GET"])
def hello_world():
"""Test route for the API"""
return ("Hello World!", 200)
def main():
s = login()
signup_project(s, COALITION)
if __name__ == "__main__":
# app.run(debug=True)
main()