From 2370d5eb25362b5f382b0ea07c433dc176bab74e Mon Sep 17 00:00:00 2001 From: luxagraf Date: Sat, 31 Dec 2016 14:20:08 -0500 Subject: wrote my own ebay tracker --- app/ebay/models.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 app/ebay/models.py (limited to 'app/ebay/models.py') diff --git a/app/ebay/models.py b/app/ebay/models.py new file mode 100644 index 0000000..4338197 --- /dev/null +++ b/app/ebay/models.py @@ -0,0 +1,64 @@ +import datetime +import re +from django.contrib.gis.db import models +from bs4 import BeautifulSoup +import requests + +class Item(models.Model): + name = models.CharField(max_length=200) + purchase_price = models.IntegerField() + url = models.CharField(max_length=200) + currently_own = models.BooleanField(blank=True, default=False) + sold = models.BooleanField(blank=True, default=False) + sale_price = models.IntegerField() + + def save(self, **kwargs): + super(Item, self).save() + + def __str__(self): + return self.name + + +class TrackedItem(models.Model): + url = models.CharField(max_length=200) + title = models.CharField(max_length=200) + + def save(self, **kwargs): + super(TrackedItem, self).save() + + @property + def get_latest_price(self): + return self.price_set.latest().price + + def __str__(self): + return self.title + + +class Price(models.Model): + date = models.DateTimeField(blank=True) + price = models.DecimalField(max_digits=6, decimal_places=2) + item = models.ForeignKey(TrackedItem) + + class Meta: + ordering = ('-date',) + get_latest_by = 'date' + + def __str__(self): + return str(self.date) + + def save(self, **kwargs): + if not self.pk: + self.date = datetime.datetime.now() + super(Price, self).save() + + +def update_tracked_item_price(item): + r = requests.get(item.url) + soup = BeautifulSoup(r.text, "lxml") + numbers = re.compile('\d+(?:\.\d+)?') + price = soup.find(itemprop="price").get_text() + price = numbers.findall(price)[0] + l, created = Price.objects.get_or_create( + price=price, + item=item + ) -- cgit v1.2.3-70-g09d2