-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
92 lines (69 loc) · 2.46 KB
/
sql.py
File metadata and controls
92 lines (69 loc) · 2.46 KB
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
import uuid
import requests
import sqlalchemy as db
from sqlalchemy import Column, Integer, String, insert, delete
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
def generate_uuid():
return str(uuid.uuid4())
# declare the structure
Base = declarative_base()
connectLocally = True
class ListingsTable(Base):
__tablename__ = "listings"
uuid = Column(String)
timeForVolunteering = Column(String)
placeForVolunteering = Column(String)
targetAudience = Column(String)
skills = Column(String)
createdDate = Column(Integer)
requirements = Column(String)
opportunityDesc = Column(String)
opportunityCategory = Column(String)
opportunityTitle = Column(String)
numOfvolunteers = Column(Integer)
minHoursPerWeek = Column(Integer)
maxHoursPerWeek = Column(Integer)
id = Column(Integer, primary_key=True)
duration = Column(String)
charityId = Column(Integer)
scrapedCharityName = Column(String)
# pictureName = Column(String)
# open
with open("./sqlPassword.txt", "r") as f:
password = f.read()
connectionString = "mysql+pymysql://serverQueryManager:%s@localhost:3306/cybervolunteers" % password
engine = db.create_engine(connectionString)
Session = sessionmaker(bind=engine)
session = Session()
requestSession = requests.Session()
# request
def recordInDb(listing, data, credentials):
cookie, path = credentials
statement = (
insert(ListingsTable).
values(**data)
)
statement = statement.compile(engine, compile_kwargs={"literal_binds": True})
try:
session.add(listing)
session.commit()
except:
pass
# print("Not able to commit to local db")
response = requestSession.post("https://cybervolunteers.org.uk/" + path, cookies={"sessionId": cookie},
data={"sql": statement})
def deleteFromSite(url, credentials):
cookie, path = credentials
statement = (
delete(ListingsTable).
where(ListingsTable.duration.like('<a class="moreDetails" href="{}%'.format(url)))
)
statement = statement.compile(engine, compile_kwargs={"literal_binds": True})
try:
engine.execute(statement)
except:
pass
# print("Not able to commit to local db")
response = requestSession.post("https://cybervolunteers.org.uk/" + path, cookies={"sessionId": cookie},
data={"sql": str(statement)})