Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (46 sloc) 1.79 KB
import re
import uuid
import requests
from bs4 import BeautifulSoup
from pricealert.src.common.database import Database
import pricealert.src.models.items.constants as ItemConstants
from import Store
__author__ = 'csuttles'
class Item(object):
def __init__(self, name, url, price=None, _id=None): = name
self.url = url
store = Store.find_by_url(url)
self.tag_name = store.tag_name
self.query = store.query
self.price = None if price is None else price
self._id = uuid.uuid4().hex if _id is None else _id
def __repr__(self):
return "<Item {} with URL {}>".format(, self.url)
def load_price(self):
# <meta itemprop="price" content="32.66"/>
# tag_name = meta
# query = { "itemprop": "price" }
request = requests.get(self.url)
content = request.content
soup = BeautifulSoup(content, 'html.parser')
element = soup.find(self.tag_name, self.query)
#string_price = element.text.strip()
string_price = str(element)
pattern = re.compile(r'(\d+\.\d+)')
match =, string_price)
self.price = float(
return self.price
def save_to_mongo(self):
Database.update(ItemConstants.COLLECTION, {"_id": self._id}, self.json())
def json(self):
return {
"_id": self._id,
"url": self.url,
"price": self.price
def get_by_id(cls, id):
return cls(**Database.find_one(ItemConstants.COLLECTION, {"_id": id}))
You can’t perform that action at this time.