一个非官方的AO3(archiveofourown.org)API
ao3-api的Python项目详细描述
AO3 API
这是一个非正式的api,让您可以通过python访问ao3(archiveofourown.org)的一些数据。
安装
使用包管理器pip安装ao3 api。
pip install ao3_api
Github
https://github.com/ArmindoFlores/ao3_api
用法
api添加了3种类型的类:AO3.Work
、AO3.User
和AO3.Session
您可以使用AO3.Work
类从存储在archiveofourown.org中的工作中提取数据
>>>importAO3>>>work=AO3.Work(14392692)# 14392692 is the workid>>>work.title'The Roots That Clutch'>>>work.date_publisheddatetime.date(2018,4,22)>>>work.authors['laquearia']>>>work.language'English'>>>
您可以通过调用work.load_chapters()
和work.get_chapter_text(chapter)
来获取章节文本
>>>importAO3>>>work=AO3.Work(14392692)>>>work.load_chapters()>>>text=work.get_chapter_text(1)>>>print(' '.join(text.split(' ')[:20]))# Print the first 20 wordsChapterTextItallstartswithasuggestion.“Hey,weshouldmakethisathing,”Midoriyasaysoneday,outofthe>>>
如果不调用work.load_chapters()
,可能会出现以下错误:
Traceback(mostrecentcalllast):File"<pyshell#9>",line1,in<module>work.get_chapter_text(1)File"C:\Python36\lib\site-packages\AO3\works.py",line27,inget_chapter_textraiseutils.UnloadedError("Work.load_chapters() must be called first")AO3.utils.UnloadedError:Work.load_chapters()mustbecalledfirst
您可以使用AO3.User
类从用户配置文件中提取数据:
>>>importAO3>>>user=AO3.User("laquearia")>>>user.url'https://archiveofourown.org/users/laquearia'>>>print(user.bio)IhavenoideawhatI'm doing, but I know I'mdoingitvery,verywell.(Artist,23,inlovewithtea.Checkoutmyshit.)NOTE:Iamknownformyangst.Readmythingswithcautionandaboxoftissues.>>>user.works# Number of works published11>>>
使用user.get_work_list(page)
从用户获取页面中的所有作品。如果您不确定有多少页,请使用user.npages
如果您有一个archiveofourown.org帐户,您可以使用AO3.session(username, password)
登录,并获得一个会话以访问只有在您登录时才可访问的会话。
>>>importAO3>>>sess=AO3.session("myusername","mypassword")>>>sess.get_n_bookmarks()10>>>sess.get_bookmarks(page=1)# Get all bookmarks in a page in the format (id, 'work title', ['author1', 'author2'])[(123456,'Work Title',['author1'])]>>>sess.get_subscriptions(page=1)# Get all subscriptions in a page in the format (id, 'work title', ['author1', 'author2'])[(123456,'Work Title',['author1'])]>>>
如果您提供了错误的用户名/密码,则会引发此错误:
Traceback(mostrecentcalllast):File"<pyshell#11>",line1,in<module>s=AO3.Session("as","as")File"C:\Python36\lib\site-packages\AO3\session.py",line20,in__init__raiseutils.LoginError("Invalid username or password")AO3.utils.LoginError:Invalidusernameorpassword
AO3.utils.workid_from_url(url)
是一个函数,它返回给定工作url的workid:
>>>importAO3>>>AO3.utils.workid_from_url("https://archiveofourown.org/works/14392692/chapters/33236241")14392692>>>AO3.utils.workid_from_url("https://archiveofourown.org/works/14392692")14392692>>>AO3.utils.workid_from_url("works/14392692/chapters/33236241")14392692>>>
未来功能
将来,如果没有正式的api发布,我可能会添加一个搜索选项和更多会话选项(订阅works、kudos和comment)。
联系信息
有关信息或错误报告,请联系francisco.rodrigues0908@gmail.com。