Initial import

This commit is contained in:
2010-02-23 10:16:25 +00:00
committed by dreddit
commit b8e5647148
26 changed files with 957 additions and 0 deletions

0
eve_api/__init__.py Normal file
View File

32
eve_api/admin.py Normal file
View File

@@ -0,0 +1,32 @@
"""
Admin interface models. Automatically detected by admin.autodiscover().
"""
from django.contrib import admin
from eve_api.models import *
class EVEAccountAdmin(admin.ModelAdmin):
list_display = ('id', 'user')
search_fields = ['id']
admin.site.register(EVEAccount, EVEAccountAdmin)
class EVEPlayerCharacterAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'corporation')
search_fields = ['id', 'name']
admin.site.register(EVEPlayerCharacter, EVEPlayerCharacterAdmin)
class EVEPlayerCorporationInline(admin.TabularInline):
model = EVEPlayerCorporation
fields = ('name', 'ticker')
extra = 0
class EVEPlayerAllianceAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'ticker', 'member_count', 'date_founded')
search_fields = ['name', 'ticker']
date_hierarchy = 'date_founded'
inlines = [EVEPlayerCorporationInline]
admin.site.register(EVEPlayerAlliance, EVEPlayerAllianceAdmin)
class EVEPlayerCorporationAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'ticker', 'member_count', 'alliance')
search_fields = ['name', 'ticker']
admin.site.register(EVEPlayerCorporation, EVEPlayerCorporationAdmin)

16
eve_api/api_exceptions.py Normal file
View File

@@ -0,0 +1,16 @@
"""
Contains exeptions used in the eve_api app.
"""
class APIAuthException(Exception):
"""
Raised when an invalid userID and/or authKey were provided.
"""
def __str__(self):
return "An authentication was encountered while querying the EVE API."
class APINoUserIDException(Exception):
"""
Raised when a userID is required, but missing.
"""
def __str__(self):
return "This query requires a valid userID, but yours is either missing or invalid."

View File

72
eve_api/api_puller/accounts.py Executable file
View File

@@ -0,0 +1,72 @@
#!/usr/bin/env python
"""
This module abstracts the pulling of account data from the EVE API.
"""
from xml.dom import minidom
from datetime import datetime
if __name__ == "__main__":
# Only mess with the environmental stuff if this is being ran directly.
from importer_path import fix_environment
fix_environment()
from django.conf import settings
from eve_proxy.models import CachedDocument
from eve_api.api_exceptions import APIAuthException, APINoUserIDException
from eve_api.models import EVEAccount, EVEPlayerCharacter, EVEPlayerCorporation
def import_eve_account(api_key, user_id):
"""
Imports an account from the API into the EVEAccount model.
"""
print user_id, ":", api_key
auth_params = {'userID': user_id, 'apiKey': api_key}
account_doc = CachedDocument.objects.api_query('/account/Characters.xml.aspx',
params=auth_params,
no_cache=False)
#print account_doc.body
dom = minidom.parseString(account_doc.body)
characters_node_children = dom.getElementsByTagName('rowset')[0].childNodes
# Create or retrieve the account last to make sure everything
# before here is good to go.
try:
account = EVEAccount.objects.get(id=user_id)
except EVEAccount.DoesNotExist:
account = EVEAccount(id=user_id)
account.api_key = api_key
account.api_user_id = user_id
account.api_last_updated = datetime.now()
account.save()
for node in characters_node_children:
try:
# Get this first, as it's safe.
corporation_id = node.getAttribute('corporationID')
corp, created = EVEPlayerCorporation.objects.get_or_create(id=corporation_id)
# Do this last, since the things we retrieved above are used
# on the EVEPlayerCharacter object's fields.
character_id = node.getAttribute('characterID')
pchar, created = EVEPlayerCharacter.objects.get_or_create(id=character_id)
name = node.getAttribute('name')
# Save these for last to keep the save count low.
pchar.name = name
pchar.corporation = corp
pchar.save()
account.characters.add(pchar)
except AttributeError:
# This must be a Text node, ignore it.
continue
return account
if __name__ == "__main__":
"""
Test import.
"""
api_key = settings.EVE_API_USER_KEY
#api_key += "1"
user_id = settings.EVE_API_USER_ID
import_eve_account(api_key, user_id)

130
eve_api/api_puller/alliances.py Executable file
View File

@@ -0,0 +1,130 @@
#!/usr/bin/env python
"""
This module pulls the master alliance XML list from the API and dumps it in the
api_puller/xml_cache directory as needed. All alliance data must be updated
in bulk, which is done reasonably quickly.
"""
from xml.dom import minidom
import os
import sys
from datetime import datetime
if __name__ == "__main__":
# Only mess with the environmental stuff if this is being ran directly.
from importer_path import fix_environment
fix_environment()
from django.conf import settings
from eve_api.models import EVEPlayerAlliance, EVEPlayerCorporation
from eve_proxy.models import CachedDocument
# This stores a list of all corps whose alliance attribute has been updated.
UPDATED_CORPS = []
def __update_corp_from_alliance_node(alliance_node, alliance):
"""
Updates a corp's alliance membership from an alliance <row> element.
"""
member_corp_nodelist = alliance_node.getElementsByTagName('rowset')[0].childNodes
for node in member_corp_nodelist:
corp_row_node = None
try:
# If this fails, this is a Text node and should be ignored.
corporation_id = int(node.getAttribute('corporationID'))
except AttributeError:
# This is probably a Text node, ignore it.
continue
corp, created = EVEPlayerCorporation.objects.get_or_create(id=corporation_id)
corp.id = corporation_id
corp.alliance = alliance
corp.alliance_join_date = datetime.strptime(alliance_node.getAttribute('startDate'),
'%Y-%m-%d %H:%M:%S')
corp.save()
# Store the corp in the updated corps list for later checks.
UPDATED_CORPS.append(corp.id)
def __remove_invalid_corp_alliance_memberships():
"""
Compares UPDATED_CORPS list to the full list of player corporations. If
the corporation was not updated from being found in one of the alliance
data sets, it has no alliance affiliation and needs to be set to no
alliance if it is not already a None value.
"""
all_corps = EVEPlayerCorporation.objects.all()
# This is not terribly efficient, but it will do for a background process.
for corp in all_corps:
"""
If the corp is not in the UPDATED_CORP list that was built from
alliance memberCorporations rowsets, then it does not belong to an
alliance and should be un-allianced if it currently is.
"""
if corp.id not in UPDATED_CORPS and corp.alliance != None:
corp.alliance = None
corp.save()
def __start_full_import():
"""
This method runs a full import of all known alliances. This may take a few
minutes and should be ran regularly if you are maintaining a full corp
list of all EVE corps as well.
"""
print "Querying /eve/AllianceList.xml.aspx/"
alliance_doc = CachedDocument.objects.api_query('/eve/AllianceList.xml.aspx')
print "Parsing..."
dom = minidom.parseString(alliance_doc.body)
result_node_children = dom.getElementsByTagName('result')[0].childNodes
# This will hold a reference to the <rowset name="alliances> Element.
alliances_rowset_node = None
# For some odd reason, two text nodes and an Element are children of
# the result Element. Find the alliances rowset from its children.
for node in result_node_children:
try:
# The node we want has a 'name' attribute.
if node.getAttribute('name') == 'alliances':
# Store the reference for later use.
alliances_rowset_node = node
# Look no further.
break
except AttributeError:
# This must be a Text node, ignore it.
continue
if alliances_rowset_node == None:
print "No alliance rowset node could be found. Your AllianceList.xml file may be corrupt."
sys.exit(1)
# We now have a list of <row> tags representing each alliance.
print "Updating alliance and member corporation data..."
for alliance_node in alliances_rowset_node.childNodes:
try:
# If this fails, this is a Text node and should be ignored.
alliance_id = int(alliance_node.getAttribute('allianceID'))
except AttributeError:
# This is probably a Text node, ignore it.
continue
"""
Search for an existing EVEPlayerAlliance object with the given
alliance ID. Create one if it doesn't exist, retrieve the existing
object if it's already there.
"""
alliance, created = EVEPlayerAlliance.objects.get_or_create(id=alliance_id)
alliance.id = alliance_id
alliance.name = alliance_node.getAttribute('name')
alliance.ticker = alliance_node.getAttribute('shortName')
alliance.member_count = alliance_node.getAttribute('memberCount')
alliance.date_founded = datetime.strptime(alliance_node.getAttribute('startDate'),
'%Y-%m-%d %H:%M:%S')
alliance.save()
# Update member corp alliance attributes.
__update_corp_from_alliance_node(alliance_node, alliance)
print "Alliances and member corps updated."
print "Removing corps alliance memberships that are no longer valid..."
__remove_invalid_corp_alliance_memberships()
if __name__ == "__main__":
__start_full_import()

55
eve_api/api_puller/corps.py Executable file
View File

@@ -0,0 +1,55 @@
#!/usr/bin/env python
"""
Module for updating corp information. If this is ran directly, the module will
iterate through all known alliances, looking at the corps in each alliance's
member list. This can be very time-consuming and should not be done often.
Within your applications, you may call query_and_update_corp() to update
an individual corp object as need be.
NOTE: To get corp data, it must be a member of an alliance.
"""
if __name__ == "__main__":
# Only mess with the environmental stuff if this is being ran directly.
from importer_path import fix_environment
fix_environment()
from eve_api.models import EVEPlayerAlliance, EVEPlayerCorporation
def start_full_import():
"""
Imports all of the corps that are in all of the known alliances.
WARNING: THIS WILL TAKE A _LONG_ TIME AND MUST BE RAN AFTER
eve_db.api_puller.alliances.__start_full_import() OR YOU WON'T GET ALL
OF THE CORPS (or any at all).
"""
alliances = EVEPlayerAlliance.objects.all()
# These two variables are used to track progress.
alliance_count = alliances.count()
# Use this as a progress indicator.
current_alliance_num = 1
for alliance in alliances:
# Keep the user informed as to the progress.
print "Alliance %d of %d..." % (current_alliance_num, alliance_count)
# A list of the alliance's member corps.
member_corps = alliance.eveplayercorporation_set.all()
# We're getting the list of corps to update from alliance memberships.
for corp in member_corps:
print "Querying", corp.id
corp.query_and_update_corp()
# Increment progress counter.
current_alliance_num += 1
if __name__ == "__main__":
"""
If ran directly, this will grab all of the corps from the known alliances.
WARNING: THIS WILL TAKE A VERY LONG TIME TO RUN! IT IS SUGGESTED YOU ONLY
GRAB CORPS AS YOU NEED THEM.
"""
start_full_import()

View File

@@ -0,0 +1,16 @@
import os
import sys
# The path to the folder containing settings.py.
BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
APPS_PATH = os.path.join(BASE_PATH, 'apps')
def fix_environment():
"""
Callable function to set up all of the Django environmental variables and
pathing for directly executable python modules.
"""
from importer_path import BASE_PATH
# Prepare the environment
sys.path.insert(0, APPS_PATH)
sys.path.insert(0, BASE_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

15
eve_api/app_defines.py Normal file
View File

@@ -0,0 +1,15 @@
"""
Standard definitions that don't change.
"""
# API status definitions for EVEAccount.
API_STATUS_PENDING = 0
API_STATUS_OK = 1
API_STATUS_AUTH_ERROR = 2
API_STATUS_OTHER_ERROR = 3
# This tuple is used to assemble the choices list for the field.
API_STATUS_CHOICES = (
(API_STATUS_PENDING, 'Unknown'),
(API_STATUS_OK, 'OK'),
(API_STATUS_AUTH_ERROR, 'Auth Error'),
(API_STATUS_OTHER_ERROR, 'Other Error'),
)

96
eve_api/managers.py Normal file
View File

@@ -0,0 +1,96 @@
from xml.dom import minidom
from django.db import models
from eve_proxy.models import CachedDocument
class InvalidCorpID(Exception):
"""
Thrown when an invalid corp id is given in an api query.
"""
def __init__(self, id):
self.value = "ID: %s does not match any corporation." % id
def __str___(self):
return repr(self.value)
def _api_get_id_from_name(name):
"""
Queries the EVE API looking for the ID of the specified corporation,
alliance, or character based on its name. This is not case sensitive.
name: (str) Corporation name to search for.
"""
query_doc = CachedDocument.objects.api_query('/eve/CharacterID.xml.aspx',
params={'names': name})
query_dat = query_doc.body.decode("utf-8", "replace")
dom = minidom.parseString(query_dat)
id_node = dom.getElementsByTagName('row')[0]
object_id = id_node.getAttribute('characterID')
if object_id == '0':
raise self.model.DoesNotExist('EVE API returned no matches for the provided corp name.')
else:
return int(object_id)
class EVEPlayerCharacterManager(models.Manager):
def api_get_id_from_name(self, name):
"""
This uses a common call for corps, characters, and alliances.
"""
return _api_get_id_from_name(name)
class EVEPlayerAllianceManager(models.Manager):
def api_get_id_from_name(self, name):
"""
This uses a common call for corps, characters, and alliances.
"""
return _api_get_id_from_name(name)
class EVEPlayerCorporationManager(models.Manager):
def get_or_query_by_id(self, corp_id):
"""
Queries for a corporation. If the corp can't be founded, check the
EVE API service for information on it. If a match still can't be
found, return EVEPlayerCorporation.DoesNotExist.
corp_id: (int) Corp's ID.
"""
try:
return self.get(id=corp_id)
except self.model.DoesNotExist:
try:
self.api_corp_sheet_xml(corp_id)
new_corp = self.create(id=corp_id)
new_corp.query_and_update_corp()
return new_corp
except InvalidCorpID:
raise
def api_get_id_from_name(self, name):
"""
This uses a common call for corps, characters, and alliances.
"""
return _api_get_id_from_name(name)
def api_corp_sheet_xml(self, id):
"""
Returns a corp's data sheet from the EVE API in the form of an XML
minidom doc.
"""
corp_doc = CachedDocument.objects.api_query('/corp/CorporationSheet.xml.aspx',
params={'corporationID': id})
corp_dat = corp_doc.body.decode("utf-8", "replace")
# Convert incoming data to UTF-8.
dom = minidom.parseString(corp_dat)
error_node = dom.getElementsByTagName('error')
# If there's an error, see if it's because the corp doesn't exist.
if error_node:
error_code = error_node[0].getAttribute('code')
if error_code == '523':
raise InvalidCorpID(id)
return dom

View File

@@ -0,0 +1,6 @@
"""
By importing all of these sub-modules, the models package is transparently
accessible by the rest of the project. This makes it act just as if it were
one monolithic models.py.
"""
from api_player import *

View File

@@ -0,0 +1,195 @@
"""
This module holds data from the EVE XML API.
"""
from django.db import models
from django.contrib.auth.models import User
from eve_proxy.models import CachedDocument
from eve_api.managers import EVEPlayerCorporationManager, EVEPlayerAllianceManager, EVEPlayerCharacterManager
from eve_api.app_defines import API_STATUS_CHOICES, API_STATUS_PENDING
class EVEAPIModel(models.Model):
"""
A simple abstract base class to set some consistent fields on the models
that are updated from the EVE API.
"""
api_last_updated = models.DateTimeField(blank=True, null=True,
verbose_name="Time last updated from API",
help_text="When this object was last updated from the EVE API.")
class Meta:
abstract = True
class EVEAccount(EVEAPIModel):
"""
Use this class to store EVE user account information. Note that its use is
entirely optional and up to the developer's discretion.
"""
user = models.ForeignKey(User, blank=True, null=True,
help_text="User that owns this account")
description = models.CharField(max_length=50, blank=True,
help_text="User-provided description.")
api_key = models.CharField(max_length=64, verbose_name="API Key")
api_user_id = models.IntegerField(verbose_name="API User ID")
characters = models.ManyToManyField("EVEPlayerCharacter", blank=True,
null=True)
api_status = models.IntegerField(choices=API_STATUS_CHOICES,
default=API_STATUS_PENDING,
verbose_name="API Status",
help_text="End result of the last attempt at updating this object from the API.")
def in_corp(self, corpid):
for char in self.characters.all():
if char.corporation_id == corpid:
return True
return False
class Meta:
app_label = 'eve_api'
verbose_name = 'EVE Account'
verbose_name_plural = 'EVE Accounts'
ordering = ['api_user_id']
class EVEPlayerCharacter(EVEAPIModel):
"""
Represents an individual player character within the game. Not to be
confused with an account.
"""
name = models.CharField(max_length=255, blank=True, null=False)
corporation = models.ForeignKey('EVEPlayerCorporation', blank=True, null=True)
# TODO: Choices field
race = models.IntegerField(blank=True, null=True)
# TODO: Choices field
gender = models.IntegerField(blank=True, null=True)
balance = models.FloatField("Account Balance", blank=True, null=True)
attrib_intelligence = models.IntegerField("Intelligence", blank=True,
null=True)
attrib_memory = models.IntegerField("Memory", blank=True, null=True)
attrib_charisma = models.IntegerField("Charisma", blank=True, null=True)
attrib_perception = models.IntegerField("Perception", blank=True, null=True)
attrib_willpower = models.IntegerField("Willpower", blank=True, null=True)
objects = EVEPlayerCharacterManager()
def __unicode__(self):
if self.name:
return "%s (%d)" % (self.name, self.id)
else:
return "(%d)" % self.id
def __str__(self):
return self.__unicode__()
class Meta:
app_label = 'eve_api'
verbose_name = 'Player Character'
verbose_name_plural = 'Player Characters'
class EVEPlayerAlliance(EVEAPIModel):
"""
Represents a player-controlled alliance. Updated from the alliance
EVE XML API puller at intervals.
"""
name = models.CharField(max_length=255, blank=True, null=False)
ticker = models.CharField(max_length=15, blank=True, null=False)
#executor_character = models.ForeignKey(EVECharacter, blank=True, null=False)
member_count = models.IntegerField(blank=True, null=True)
date_founded = models.DateField(blank=True, null=True)
objects = EVEPlayerAllianceManager()
class Meta:
app_label = 'eve_api'
ordering = ['date_founded']
verbose_name = 'Player Alliance'
verbose_name_plural = 'Player Alliances'
def __unicode__(self):
if self.name:
return "%s (%d)" % (self.name, self.id)
else:
return "(#%d)" % self.id
def __str__(self):
return self.__unicode__()
class EVEPlayerCorporation(EVEAPIModel):
"""
Represents a player-controlled corporation. Updated from a mixture of
the alliance and corporation API pullers.
"""
name = models.CharField(max_length=255, blank=True, null=True)
ticker = models.CharField(max_length=15, blank=True, null=True)
description = models.TextField(blank=True, null=True)
url = models.URLField(verify_exists=False, blank=True, null=True)
ceo_character = models.ForeignKey(EVEPlayerCharacter, blank=True, null=True)
#home_station = models.ForeignKey(EVEStation, blank=True, null=False)
alliance = models.ForeignKey(EVEPlayerAlliance, blank=True, null=True)
alliance_join_date = models.DateField(blank=True, null=True)
tax_rate = models.FloatField(blank=True, null=True)
member_count = models.IntegerField(blank=True, null=True)
shares = models.IntegerField(blank=True, null=True)
# Logo generation stuff
logo_graphic_id = models.IntegerField(blank=True, null=True)
logo_shape1 = models.IntegerField(blank=True, null=True)
logo_shape2 = models.IntegerField(blank=True, null=True)
logo_shape3 = models.IntegerField(blank=True, null=True)
logo_color1 = models.IntegerField(blank=True, null=True)
logo_color2 = models.IntegerField(blank=True, null=True)
logo_color3 = models.IntegerField(blank=True, null=True)
objects = EVEPlayerCorporationManager()
class Meta:
app_label = 'eve_api'
verbose_name = 'Player Corporation'
verbose_name_plural = 'Player Corporations'
def __str__(self):
if self.name:
return self.name
else:
return "Corp #%d" % self.id
def query_and_update_corp(self):
"""
Takes an EVEPlayerCorporation object and updates it from the
EVE API service.
"""
# Pull XML from the EVE API via eve_proxy.
dom = EVEPlayerCorporation.objects.api_corp_sheet_xml(self.id)
# Tuples of pairings of tag names and the attribute on the Corporation
# object to set the data to.
tag_mappings = (
('corporationName', 'name'),
('ticker', 'ticker'),
('url', 'url'),
('description', 'description'),
('memberCount', 'member_count'),
('graphicID', 'logo_graphic_id'),
('shape1', 'logo_shape1'),
('shape2', 'logo_shape2'),
('shape3', 'logo_shape3'),
('color1', 'logo_color1'),
('color2', 'logo_color2'),
('color3', 'logo_color3'),
)
# Iterate through the tag mappings, setting the values of the tag names
# (first member of the tuple) to the attribute named in the second member
# of the tuple on the EVEPlayerCorporation object.
for tag_map in tag_mappings:
try:
setattr(self, tag_map[1],
dom.getElementsByTagName(tag_map[0])[0].firstChild.nodeValue)
except AttributeError:
# This tag has no value, skip it.
continue
except IndexError:
# Something weird has happened
print " * Index Error:", tag_map[0]
continue
print "Updating", self.id, self.name
self.save()