Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Added Google Maps API features #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Look in `settings.py` for a full list of all the configuration options. Here's
* `CRAIGSLIST_HOUSING_SECTION` -- the subsection of Craigslist housing that you want to look in.
* `SLACK_CHANNEL` -- the Slack channel you want the bot to post in.


External Setup
--------------------

Expand All @@ -31,6 +32,7 @@ Before using this bot, you'll need a Slack team, a channel for the bot to post i
* Create a Slack team, which you can do [here](https://slack.com/create#email).
* Create a channel for the listings to be posted into. [Here's](https://get.slack.help/hc/en-us/articles/201402297-Creating-a-channel) help on this. It's suggested to use `#housing` as the name of the channel.
* Get a Slack API token, which you can do [here](https://api.slack.com/docs/oauth-test-tokens). [Here's](https://get.slack.help/hc/en-us/articles/215770388-Creating-and-regenerating-API-tokens) more information on the process.
* Get your Google Maps API token from [This link](https://developers.google.com/maps/documentation/javascript/get-api-key).

Configuration
--------------------
Expand All @@ -55,6 +57,7 @@ Configuration

* Create a file called `private.py` in this folder.
* Add a value called `SLACK_TOKEN` that contains your Slack API token.
* Add a value called `GOOGLE_TOKEN_PLACES` that contains your Google Maps API token.
* Add any other values you want to `private.py`.

Installation + Usage
Expand All @@ -68,7 +71,7 @@ Installation + Usage
* `docker run -d -e SLACK_TOKEN={YOUR_SLACK_TOKEN} dataquestio/apartment-finder`
* To run the program with your own configuration:
* `docker run -d -e SLACK_TOKEN={YOUR_SLACK_TOKEN} -v {ABSOLUTE_PATH_TO_YOUR_CONFIG_FOLDER}:/opt/wwc/apartment-finder/config dataquestio/apartment-finder`

## Manual

* Look in the `Dockerfile`, and make sure you install any of the apt packages listed there.
Expand Down
4 changes: 3 additions & 1 deletion scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Listing(Base):
cl_id = Column(Integer, unique=True)
area = Column(String)
bart_stop = Column(String)
distance_matrix_result = Column(String)

Base.metadata.create_all(engine)

Expand Down Expand Up @@ -97,7 +98,8 @@ def scrape_area(area):
cl_id=result["id"],
area=result["area"],
bart_stop=result["bart"]
)
distance_matrix_result=result["distance_matrix_result"]
)

# Save the listing so we don't grab it again.
session.add(listing)
Expand Down
33 changes: 32 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,37 @@
# Should be put in private.py, or set as an environment variable.
SLACK_TOKEN = os.getenv('SLACK_TOKEN', "")

# Google API Settings

GOOGLE_TOKEN_PLACES = os.getenv('GOOGLE_TOKEN_PLACES', "")
#You can use the distance API for time and distance informaiton
# from the apartment found to specified locations. e.g. your work.
# Information for the googlemaps distance matric api
DESTINATIONS="Starbucks"
MODE="transit"
LANG="english"
AVOID=None
UNITS="metric"
DEPART_TIME=None
dt = datetime.now()
art = dt.replace(hour=7,minute=45)
ARRIV_TIME=art
TRANS_MODE="bus"
TRANS_ROUT_PREF="fewer_transfers"
TRAFFIC_MODEL="best_guess"

# Information for the googlemaps places api_call
# For information about places in the area, eg. restaurants.
radius=1500
keyword=None
min_price=0
max_price=4
name=None
open_now=False
rank_by=None
TYPE="restaurant"


# Any private settings are imported here.
try:
from private import *
Expand All @@ -124,4 +155,4 @@
try:
from config.private import *
except Exception:
pass
pass
35 changes: 31 additions & 4 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import settings
import math
import googlemaps

def coord_distance(lat1, lon1, lat2, lon2):
"""
Expand Down Expand Up @@ -36,6 +37,15 @@ def post_listing_to_slack(sc, listing):
:param listing: A record of the listing.
"""
desc = "{0} | {1} | {2} | {3} | <{4}>".format(listing["area"], listing["price"], listing["bart_dist"], listing["name"], listing["url"])
image_url=""
if listing["geotag"] != None:
image_url = """https://maps.googleapis.com/maps/api/staticmap?size=500x400"""+\
"""&markers=color:blue%7Clabel:S%7C"""+str(settings.WORK_COORD[0])+","+str(settings.WORK_COORD[1])+\
"""&markers=color:red%7Clabel:A%7C"""+str(listing["geotag"][0])+","+str(listing["geotag"][1])+"""&key="""+settings.GOOGLE_TOKEN_MAPS
attachments = [{"title": "map",
"image_url": image_url}]
else:
attachments = None
sc.api_call(
"chat.postMessage", channel=settings.SLACK_CHANNEL, text=desc,
username='pybot', icon_emoji=':robot_face:'
Expand All @@ -55,11 +65,27 @@ def find_points_of_interest(geotag, location):
near_bart = False
bart_dist = "N/A"
bart = ""
distance_matrix_result=""
# Look to see if the listing is in any of the neighborhood boxes we defined.
for a, coords in settings.BOXES.items():
if in_box(geotag, coords):
area = a
area_found = True

if geotag is not None:
for a, coords in settings.BOXES.items():
if in_box(geotag, coords):
area = a
area_found = True
# get googlemaps data for the distance and duration to the specified destination
gmaps = googlemaps.Client(key=settings.GOOGLE_TOKEN_DISTANCE)
distance_matrix = gmaps.distance_matrix(geotag, settings.DESTINATIONS, mode=settings.MODE, language=settings.LANG, avoid=settings.AVOID, units=settings.UNITS, departure_time=settings.DEPART_TIME, arrival_time=settings.ARRIV_TIME, transit_mode=settings.TRANS_MODE, transit_routing_preference=settings.TRANS_ROUT_PREF, traffic_model=settings.TRAFFIC_MODEL)
gplaces = googlemaps.Client(key=settings.GOOGLE_TOKEN_PLACES)
places_list = gplaces.places_nearby(geotag, settings.radius, settings.keyword, settings.LANG, settings.min_price, settings.max_price, settings.name, settings.open_now, settings.rank_by, settings.TYPE, page_token=None)
if distance_matrix['rows'][0]['elements'][0]['status'] != 'ZERO_RESULTS':
time = distance_matrix['rows'][0]['elements'][0]['duration']['text']
distance = distance_matrix['rows'][0]['elements'][0]['distance']['text']
distance_matrix_result = "{0} in {1}".format(distance, time)
else:
distance_matrix_result = "Google Error."
else:
distance_matrix_result= "Not enough location information available."

# Check to see if the listing is near any transit stations.
for station, coords in settings.TRANSIT_STATIONS.items():
Expand All @@ -84,4 +110,5 @@ def find_points_of_interest(geotag, location):
"near_bart": near_bart,
"bart_dist": bart_dist,
"bart": bart
"distance_matrix_result": distance_matrix_result
}