-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
53 lines (39 loc) · 1.46 KB
/
filter.py
File metadata and controls
53 lines (39 loc) · 1.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
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, Float, Boolean
from sqlalchemy.orm import sessionmaker
from dateutil.parser import parse
from slackclient import SlackClient
import time
engine = create_engine('sqlite:///listings.db', echo=False)
Base = declarative_base()
class Listing(Base):
"""
A table to store data on craigslist listings.
"""
__tablename__ = 'listings'
id = Column(Integer, primary_key=True)
title = Column(String)
link = Column(String, unique=True)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
def do_filter(results):
filteredresult = []
for result in results:
listing = session.query(Listing).filter_by(link=result["link"]).first()
# Don't store the listing if it already exists.
if listing is None:
if result["htmlTitle"] is None or result["link"] is None:
# If there is no string identifying which neighborhood the result is from, skip it.
continue
# Create the listing object.
listing = Listing(
title=result["htmlTitle"],
link=result["link"]
)
# Save the listing so we don't grab it again.
session.add(listing)
session.commit()
filteredresult.append(result)
return filteredresult