-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from lryanle/10/setup-db
10/setup db
- Loading branch information
Showing
5 changed files
with
99 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PASSWORD=PASSWORD | ||
USERNAME=USERNAME | ||
DB_URI=CONNECTION_LINK_USING_${USERNAME}_AND_${PASSWORD} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# seniordesign | ||
Senior Design Repository for the Statefarm Automotive Fraud Project | ||
|
||
## Database Access | ||
Make a copy of the ``.env.example`` file and make the following changes. | ||
1. remove ``.example`` from the extension | ||
2. Paste the username and password provided in MongoDB Atlas (if you should have access but do not, please contact @waseem-polus) | ||
3. Paste the connection URL provided provided in MongoDB Atlas. Include the password and username fields using ``${VARIABLE}`` syntax to embed the value of the variable |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from dotenv import load_dotenv | ||
import pymongo | ||
import os | ||
from datetime import date | ||
|
||
def get_conn(db): | ||
# load environment variable containing db uri (which includes username and password) | ||
load_dotenv() | ||
db_uri = os.getenv("DB_URI") | ||
|
||
# create a mongodb connection | ||
try: | ||
client = pymongo.MongoClient(db_uri) | ||
|
||
# return a friendly error if a URI error is thrown | ||
except pymongo.errors.ConfigurationError: | ||
print("An Invalid URI host error was received. Is your Atlas host name correct in your connection string (found the .env)?") | ||
return {"success" : False, "db": 0} | ||
|
||
# use a database named "Test" | ||
return {"success" : True, "db": client.get_database(db)} | ||
|
||
def post_raw(source, title, price, location, miles, link, images = None, postBody = None, longitude = None, latitude = None, attributes = None): | ||
car = { | ||
"title": title, | ||
"price": price, | ||
"location": location, | ||
"odometer": miles, | ||
"link": link, | ||
"source": source, | ||
"scrapeDate": str(date.today()) | ||
} | ||
|
||
if (images is not None): | ||
car["images"] = images | ||
|
||
if (postBody is not None): | ||
car["postBody"] = postBody | ||
|
||
if (longitude is not None): | ||
car["longitude"] = longitude | ||
|
||
if (latitude is not None): | ||
car["latitude"] = latitude | ||
|
||
if (attributes is not None): | ||
for attr in attributes: | ||
car[attr["label"]] = attr["value"] | ||
|
||
# Insert into collection called "scrape_test" | ||
conn = get_conn("scrape") | ||
|
||
if (conn["success"]): | ||
result = conn["db"]["scraped_raw"].insert_one(car) | ||
return result.acknowledged | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
faac772
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
seniordesign – ./
seniordesign-git-main-lryanle.vercel.app
seniordesign-lryanle.vercel.app
smare.vercel.app
smare.lryanle.com