-
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.
first steps to get data from eve central (Issue #18)
- Loading branch information
Showing
4 changed files
with
97 additions
and
2 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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
from django.db import models | ||
|
||
# Create your models here. | ||
class TaskLastRun(models.Model): | ||
taskName = models.CharField(max_length=256) | ||
parameterValue = models.CharField(max_length=256) | ||
lastRun = models.DateTimeField(null=True) | ||
|
||
class Meta: | ||
unique_together = (("taskName", "parameterValue"),) |
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
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,23 @@ | ||
# =========================================================================== | ||
# = Import general python modules = | ||
# =========================================================================== | ||
from lxml import etree | ||
import httplib | ||
|
||
# ============================================================================= | ||
# = Helper functions = | ||
# ============================================================================= | ||
|
||
def getXMLFromEveCentralAPI(action, params): | ||
header = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} | ||
|
||
connection = httplib.HTTPConnection("api.eve-central.com", 80) | ||
connection.request("GET", action, params, header) | ||
|
||
xml = connection.getresponse().read() | ||
connection.close() | ||
|
||
if xml.lower() == "Can't find that type": | ||
return None | ||
else: | ||
return etree.fromstring(xml) |
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,67 @@ | ||
# =========================================================================== | ||
# = Import Models and helpers = | ||
# =========================================================================== | ||
from evedb.models import * | ||
from common.models import TaskLastRun | ||
from common.tasks import locktask | ||
from helper import getXMLFromEveCentralAPI | ||
# =========================================================================== | ||
# = Import celery modules = | ||
# =========================================================================== | ||
from celery.task import Task | ||
from celery.task import task | ||
|
||
# =========================================================================== | ||
# = Import general python modules = | ||
# =========================================================================== | ||
import urllib | ||
|
||
# =========================================================================== | ||
# = Logging = | ||
# =========================================================================== | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
# =================================== | ||
# = Update Character from EVE API = | ||
# =================================== | ||
class UpdateAllMarketOrders(Task): | ||
def run(self): | ||
allItems = invTypes.objects.all() | ||
logger.info("Got %s Items from database" % allItems.count()) | ||
|
||
for item in allItems: | ||
logger.info("Updating Item '%s'" % item.typeName) | ||
updateMarketOrder.delay(item.typeID) | ||
|
||
return True | ||
|
||
@task | ||
@locktask | ||
def updateMarketOrder(itemTypeId): | ||
lastFetch, created = TaskLastRun.objects.get_or_create(taskName='updateMarketOrder', parameterValue=itemTypeId) | ||
now = datetime.datetime.now() | ||
|
||
if lastFetch.lastRun is None: | ||
hours = 360 | ||
else: | ||
delta = now - lastFetch.lastRun | ||
deltaHours = int(round(delta.total_seconds() / 60, 0)) | ||
hours = deltaHours if deltaHours > 1 else 1 | ||
|
||
action = "/eve/CharacterInfo.xml.aspx" | ||
params = urllib.urlencode({'typeid': itemTypeId, 'sethours': hours}) | ||
xml = getXMLFromEveCentralAPI(action=action, params=params) | ||
|
||
if xml is None: | ||
logging.error("Unable to fetch itemID %s from EVE Central" % itemTypeId) | ||
else: | ||
logging.info("Got data for itemID %s" % itemTypeId) | ||
|
||
lastFetch.lastRun = now | ||
lastFetch.save() | ||
|
||
if not created: | ||
logger.warning("Delete must be implemented here") | ||
|
||
logger.error("Not implemented") |