Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # generate cachkey for
- def generate_cache_key(key, category=None, api=None, **kwargs):
- cache_key = "%s" % (key)
- if category:
- cache_key += "_%s" % category
- if api:
- cache_key += "_%d" % api.pk
- for key, value in kwargs.iteritems():
- cache_key += "_%s_%s" % (key, value)
- return cache_key
- class EveApiCache(Task):
- """
- set cache task. Creating new caches, and updating old ones.
- """
- ACCOUNT = "account"
- CHAR = "char"
- CORP = "corp"
- EVE = "eve"
- MAP = "map"
- SERVER = "server"
- API = "API"
- CATEGORIES = (
- (ACCOUNT, "Account"),
- (CHAR, "Char"),
- (CORP, "Corp"),
- (EVE, "Eve"),
- (MAP, "Map"),
- (SERVER, "Server"),
- (API, "API"),
- )
- UPDATES = ['WalletJournal', 'MarketOrders']
- api = models.ForeignKey('apis.Api', null=True)
- kwargs = PickledObjectField(null=True, blank=True)
- category = models.CharField(max_length=15, choices=CATEGORIES, blank=True)
- key = models.CharField(max_length=254)
- #cache_key = models.CharField(max_length=254)
- class Meta:
- unique_together = ['api', 'kwargs', 'category', 'key']
- def __unicode__(self):
- return "%s/%s" % (self.category, self.key)
- def cache_key(self):
- key = connection.generate_cache_key(
- self.key, category=self.category, api=self.api, **self.kwargs
- )
- return key
- def execute(self):
- now = datetime.now().replace(tzinfo=utc)
- # need to be created before running UPDATES
- cache_key = self.cache_key()
- result = {"created": now}
- if self.key in self.UPDATES:
- data = getattr(self, self.key.lower())()
- cache_timer = utils.connection.API_TIMERS[self.key]
- else:
- data = self.request_call()
- # default cachetimer is 48 hours
- cache_timer = 60 * 60 * 48
- result["data"] = data
- cache.set(cache_key, result, cache_timer)
- # Get the first part of the api object.
- # it will establish an authenticated connection if needed.
- # the connection will already have the first part of the API uri
- # example uri part: https://api.eveonline.com/Eve/KEY
- # Where KEY will be added before retrieving the information
- # for more information look at https://neweden-dev.com/API
- def connection(self):
- if self.api:
- api = connection.auth_connect(self.api)
- else:
- api = connection.api_connect()
- # return eveapi object
- return getattr(api, self.category)
- # uses connection to retrieve data from specific API key
- # Kwargs should contain all required and/or optional fields
- # so example kwargs for a WalletJournal key with 2500 rows would be
- # self.kwargs = {"characterID": some_character_id, "rowCount": 2500}
- def request_call(self):
- connect = self.connection()
- if self.kwargs:
- return getattr(connect, self.key)(**self.kwargs)
- else:
- return getattr(connect, self.key)()
- # update wallet journal for characters and corporation
- def walletjournal(self):
- if "characterID" in self.kwargs:
- model = apps.get_model("characters", "CharacterJournal")
- parent = apps.get_model("characters", "CharacterApi").objects.get(
- characterid=self.kwargs['characterID']
- )
- else:
- model = apps.get_model("corporations", "CorporationJournal")
- fromid = 0
- while True:
- transactions = self.request_call().transactions
- for trans in transactions:
- date = common.convert_timestamp(trans.date)
- if model.objects.filter(
- parent=parent,
- balance=trans.balance,
- date=date,
- ).exists():
- continue
- else:
- model.create_entry(parent, trans)
- if int(trans.refID) < fromid or fromid == 0:
- fromid = int(trans.refID)
- if len(transactions) < 2500:
- break
- else:
- kwargs = self.kwargs
- kwargs['fromID'] = fromid
- self.kwargs = Kwargs
- self.save()
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement