Skip to content
Permalink
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 pricealert.src.models.stores.store import Store
__author__ = 'csuttles'
class Item(object):
def __init__(self, name, url, price=None, _id=None):
self.name = 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.name, self.url)
def load_price(self):
# https://www.redbubble.com/people/immortalloom/works/22929408-official-big-o-cheat-sheet-poster?p=poster&finish=semi_gloss&size=large
# <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 = re.search(pattern, string_price)
self.price = float(match.group())
return self.price
def save_to_mongo(self):
Database.update(ItemConstants.COLLECTION, {"_id": self._id}, self.json())
def json(self):
return {
"_id": self._id,
"name": self.name,
"url": self.url,
"price": self.price
}
@classmethod
def get_by_id(cls, id):
return cls(**Database.find_one(ItemConstants.COLLECTION, {"_id": id}))
You can’t perform that action at this time.