diff options
author | luxagraf <sng@luxagraf.net> | 2016-12-31 14:20:08 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2016-12-31 14:20:08 -0500 |
commit | 2370d5eb25362b5f382b0ea07c433dc176bab74e (patch) | |
tree | 085fa81c5a94b385fd293e8f5b46a8586db7325f /app/ebay/models.py | |
parent | c8800af7f64fff44909cdf8f88fca91a080c2526 (diff) |
wrote my own ebay tracker
Diffstat (limited to 'app/ebay/models.py')
-rw-r--r-- | app/ebay/models.py | 64 |
1 files changed, 64 insertions, 0 deletions
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 + ) |