使用Twisted的异步Spore Python API。
项目描述
历史
Twisted Spore API的创建主要是为了娱乐,并没有什么特别之处;所有资源获取都是使用Twisted网络客户端(twisted.web.client.getPage)完成的。
当可能时(考虑到Twisted Spore API的异步特性),代码从阻塞Python API中重用。这是为了保持与同步Spore Python API的功能兼容性。然而,结果发现,其他API中也有几个功能缺失,以及一些在API中最初编写的REST资源已经不再工作。
依赖
要使用此API,您需要安装Twisted(Python)。最新版本的下载链接始终在主页上
进一步开发
如果您有兴趣将同步Python API更新到spore.com发布的最新功能,我很乐意在此源代码中添加对非Twisted API的显式支持,以便即使在未安装Twisted的情况下也能使用。
有关即将到来的变更的更多详细信息,请务必阅读待办事项部分。
安装
开发
如果您想为txSpore开发或使用我们正在开发的最新代码,您可以从源代码安装。您需要安装bzr,然后只需执行以下操作
$ bzr branch lp:txspore $ cd txspore $ sudo python setup.py install
简单安装
您可以使用setuptools的easy_install脚本来将txSpore安装到您的系统上
$ sudo easy_install txSpore
手动下载
您可以通过访问以下URL从Python包索引手动下载源tarball
您需要解压缩源文件,进入源目录,然后可以执行常规操作
$ sudo python setup.py install
检查源代码
安装完成后,只要您的系统上安装了Twisted,并且您的PATH中包含了试验脚本,您就可以在任何地方执行此操作来测试源代码。
$ trial txspore
这将运行测试套件,并报告任何单元测试的成功和失败。
使用
交互式提示
有时从Python解释器运行Twisted代码并不太实用(超时可能是一个问题,并且从回调中访问结果可能很困难)。不管怎样,下面有一个示例,展示了如何使用REST服务API以及在spore.com提供的静态数据服务。
仅通过REST服务提供的用户成就数据只有与之相关的ID,没有文本数据。Spore数据服务确实有文本。我们需要获取两者。首先,让我们做一些初始导入并设置一些回调
>>> from cStringIO import StringIO >>> from twisted.internet import reactor >>> from twisted.internet.defer import DeferredList >>> from txspore.client import AsyncClient >>> >>> results = [] >>> data = StringIO() >>> >>> def setResults(callback_results, results): ... results.append(callback_results) ... >>> def printError(error): ... print error.getErrorMessage() ... >>> def finish(ignored): ... reactor.stop() ...
有了我们的回调和errback定义,以及一些用于存储结果数据的全局对象,我们就可以进行客户端调用
>>> client = AsyncClient() >>> d1 = client.rest.getAchievementsForUser("oubiwann", 0, 20) >>> d1.addCallback(setResults, results) <Deferred at 0x... >>> d1.addErrback(printError) <Deferred at 0x... >>> >>> d2 = client.data.getAchievementDataXML(fd=data) >>> d2.addErrback(printError) <Deferred at 0x... >>> >>> d = DeferredList([d1, d2]) >>> d.addCallback(finish) <DeferredList at 0x... >>> reactor.run()
让我们确保我们得到预期的成就数量,然后快速查看一些与该用户相关的成就
>>> achievements = results.pop() >>> len(achievements) 20 >>> for achievement in sorted(achievements)[0:4]: ... print achievement.guid ... 0xaec66642!0x0770b845 0x0cc8b2c9!0xb9ff8f07 0x0cc8b2c9!0x19988ceb 0x0cc8b2c9!0xe1f5cf25
我们现在有了ID,但没有文本。让我们获取后者
>>> from txspore import util >>> from txspore import model >>> >>> xmlTree = util.XML(data.getvalue()) >>> achievementsModel = model.RecursiveDataModel(xmlTree) >>> len(achievementsModel.achievements) 124 >>> achievementsLookup = {} >>> for achievement in achievementsModel.achievements: ... achievementsLookup[achievement.id] = ( ... achievement.name, achievement.description) ...
有了查找字典,我们可以以更友好的输出重新打印用户结果
>>> for achievement in sorted(achievements): ... try: ... print "%s: %s" % achievementsLookup[achievement.guid] ... except KeyError: ... print "Couldn't find key '%s' ..." % achievement.guid ... Couldn't find key '0xaec66642!0x0770b845' ... Wanderer Passion: Play as a Wanderer Quest Master: Complete 150 missions in the Space stage Gunner: Destroy at least 500 other space vessels Relentless: Complete the Civilization stage 10 times Tribal: Complete the Tribal stage 10 times Maxis Scout: Earn 100 badges in the Space stage Shaman Hero: Achieve Master Badge Level 10 as a Shaman Universe In A Box: Play in every stage and every creator Slugger: Finish Creature stage without legs Bestial: Play the Creature stage 10 times Max Power: Build a creature with maximum stats in at least 4 abilities... Spore Fan: Spend 50 hours in your Spore galaxy Biologist: Make and publish 100 creatures Bard Passion: Play as a Bard General Custer: Lead 30 posse members to their death Spice Hoarder: Control every resource node on the planet simultaneously Rolling Thunder: Complete the Civilization stage in less than an hour Missionary: Finish the Civilization stage with more than 8 religious cities Speed Demon: Finish Creature stage within an hour
演示
在txSpore顶层源目录中有一个examples目录。其中包含一个演示Web应用程序,展示了
一种将txSpore集成到Web应用程序中的简单方法
如何使用客户端获取用户数据和资产
如何使用回调处理结果
单元测试
单元测试实际上是查找关于使用细节的最佳地方之一。有两个测试模块在确定如何使用txSpore API时可以提供非常有用的信息
txspore/tests/test_client.py - 基本客户端使用和如何处理结果
txspore/tests/test_model.py - 返回模型对象上的可用属性的详细视图
API快速参考
以下是txSpore客户端类上可用的对象和方法列表。
txspore.client.AsyncClient.rest
__init__
参数:parent
getAchievementsForUser
参数:username,start,length
getAssetsForSporeCast
参数:sporeCastID,start,length
getAssetsForUser
参数:username,start,length
getBuddiesForUser
参数:username,start,length
getCommentsForAsset
参数:assetID,start,length
getDailyStats(无参数)
getInfoForAsset
参数:assetID
getProfileInfo
参数:username
getSporeCastsForUser
参数:username
getStatsForCreature
参数:creatureID
searchAssets
参数:searchType,start,length,assetType
txspore.client.AsyncClient.data
__init__
参数:parent
getAchievementDataXML
参数:path,fd
getAchievementIcon
参数:achievementID,path,fd
getAchievementText
参数:path,fd
getAssetDataLargePNG
参数:assetID,path,fd
getAssetDataSmallPNG
参数:assetID,path,fd
getAssetDataXML
参数:assetID,path,fd
getLargeCard
参数:assetID,path,fd
getPaintIcon
参数:remoteFilename,path,fd
getPaintInfo
参数:path,fd
getPartIcon
参数:remoteFilename,path,fd
getPartInfo
参数:blockType,path,fd
txspore.client.AsyncClient.atom
__init__
参数:parent
getAssetsForUser
参数:username
getEventsForAsset
参数:assetID
getEventsForUser
参数:username
getSporeCastFeed
参数:sporeCastID
searchAssets
参数:searchType,start,length
txspore.client.AsyncClient.cache
__init__(无参数)
get
参数:key
purge(无参数)
remove
参数:key
set
参数:key,object
已知错误
目前没有
待办事项
更新所有方法,带有epydoc可解析的docstrings。
为非spore API方法创建一个方面……例如CustomAspect。
添加对使用完整、最新的同步API的支持,无需安装Twisted。
修复test_saveFileWithError中的线程/日志问题。
向客户端添加搜索功能。
在txspore.original.SporeAPICoreUtils中添加行244以下的所有代码。
向示例中添加模式代码,或者添加更多示例。
将结果编码为utf-8。
AsyncClient.custom
使用client.getAchievementsForUser中的二级查询获取有用的成就数据。
根据原型/类别最受欢迎的创建,包含名称和.png(由MrAlex92建议;更多详情:http://forum.spore.com/jforum/posts/list/27693.page)。
按肢体数量、复杂性、特定特性、大小(由Technodude12建议)排序/筛选创建。
按飞行/游泳模式排序/筛选(由docpippo建议)。
添加以下用户数据的方法(由Eochaid1701建议)
创建数量
最新创建(名称)
最新创建(.png)
一个元组结果:用户名,创建数量,最新创建(名称),最新创建(.png)
可能还需要添加用户图标、最新成就、标语、加入日期
变更
0.0.1到0.0.2
添加了一个新的客户端对象;现在使用方法调用,而不是模块级别的函数。
添加了对缓存方法结果的支持。
将REST服务API方法移动到它们自己的对象中,并在客户端类的“rest”属性上实例化。
将静态数据服务API方法移动到它们自己的对象中,并在客户端类的“data”属性上实例化。
在客户端对象的“atom”属性上添加了对Spore Atom(“RSS”)数据的支持。
实现了尚未添加的最后一个Spore API方法。
版本 0.0.1
txSpore的初始发布,支持大多数REST和静态数据API。
项目详情
txSpore-0.0.2.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 2a7d37fc2fa55bffe654c02c7622c700b00904f7c18bbefac4e04c008fdc7b54 |
|
MD5 | 9198760b719775910d5828be459f5dd0 |
|
BLAKE2b-256 | 76b1cf13d8e1db82e5d238c9c0e851bd0fab3afd823ffbd6827025f0b6c97fd3 |