這是一個非正式的API,可讓您通過Python訪問AO3的某些(ArchiveOfourown.org)數據。
使用軟件包管理器PIP安裝AO3 API。
pip install ao3_api
https://github.com/armindoflores/ao3_api
該軟件包分為9個核心模塊:作品,章節,用戶,系列,搜索,會話,評論,額外和utils。
您可能想處理的最基本的事情之一是加載工作並檢查其統計信息和信息。為此,您需要AO3.Work
課程。
我們首先找到要加載的工作的工作形式。我們要么通過使用AO3.utils.workid_from_url(url)
或僅查看我們自己的URL來做到這一點。讓我們看看:
import AO3
url = "https://archiveofourown.org/works/14392692/chapters/33236241"
workid = AO3 . utils . workid_from_url ( url )
print ( f"Work ID: { workid } " )
work = AO3 . Work ( workid )
print ( f"Chapters: { work . nchapters } " )
運行此片段後,我們將獲得輸出:
Work ID: 14392692
Chapters: 46
重要的是要注意,訪客用戶可能無法訪問某些作品,在這種情況下,您將獲得0章作為輸出,而錯誤AO3.utils.AuthError: This work is only available to registered users of the Archive
嘗試加載它。儘管如此,我們仍然可以對這個工作對像做更多的事情:讓我們嘗試獲取第二章的前20個單詞。
import AO3
work = AO3 . Work ( 14392692 )
print ( work . chapters [ 1 ]. title ) # Second chapter name
text = work . chapters [ 1 ]. text # Second chapter text
print ( ' ' . join ( text . split ( " " )[: 20 ]))
What Branches Grow Meaning
December 27, 2018
Christmas sucked this year, and Shouto’s got the black eye to prove it.
Things had started out well enough,
工作中的對象。章是AO3.Chapter
型。他們具有與Work
對象相同的屬性。
您可以使用工作對象可以做的另一件事是將整個工作作為PDF或電子書下載。目前,您可以下載作為AZW3,EPUB,HTML,MOBI和PDF文件的工作。
import AO3
work = AO3 . Work ( 14392692 )
with open ( f" { work . title } .pdf" , "wb" ) as file :
file . write ( work . download ( "PDF" ))
高級功能
通常,當您調用Work
類的構造函數時,有關它的所有信息都會在__init__()
函數中加載。但是,此過程需要相當長的時間(〜1-1.5秒),例如,如果要加載系列的作品列表,則可能需要等待30秒以上。為了避免此問題, Work.reload()
函數在初始化時稱為“螺紋”函數,這意味著,如果您使用參數threaded=True
調用它,它將返回Thread
對象並並行工作,含義您可以同時加載多個作品。讓我們看一下實施:
import AO3
import time
series = AO3 . Series ( 1295090 )
works = []
threads = []
start = time . time ()
for work in series . work_list :
works . append ( work )
threads . append ( work . reload ( threaded = True ))
for thread in threads :
thread . join ()
print ( f"Loaded { len ( works ) } works in { round ( time . time () - start , 1 ) } seconds." )
Loaded 29 works in 2.2 seconds.
負載=在Work
構建器內部的load=False
確保我們在創建類實例後不會立即加載工作。最後,我們迭代每個線程,然後等待最後一個使用.join()
完成。讓我們將加載AO3的標準方法進行比較:
import AO3
import time
series = AO3 . Series ( 1295090 )
works = []
start = time . time ()
for work in series . work_list :
work . reload ()
works . append ( work )
print ( f"Loaded { len ( works ) } works in { round ( time . time () - start , 1 ) } seconds." )
Loaded 29 works in 21.6 seconds.
如我們所見,性能的大幅提高。該軟件包中還有其他功能具有此功能。要查看函數是“螺紋”,請使用hasattr(function, "_threadable")
或檢查其__doc__
字符串。
為了節省更多的時間,如果您僅對元數據感興趣,則可以使用load_chapters
選項加載設置為false的工作。另外,請注意,某些功能(例如Series.work_list
或Search.results
)可能會返回半加載的Work
對象。這意味著沒有提出任何要求加載此工作的請求(因此您無法訪問章節文本,筆記等),但是幾乎所有的元數據都將被緩存,並且您可能無需致電Work.reload()
。
關於Work
類別的最後一個重要信息是,其大多數屬性(例如書籤,榮譽,作者的名字等)都是屬性的屬性。這意味著,一旦您檢查一次,該值就會存儲,即使這些值更改,它也不會更改。要更新這些值,您需要調用Work.reload()
。請參閱下面的示例:
import AO3
sess = AO3 . GuestSession ()
work = AO3 . Work ( 16721367 , sess )
print ( work . kudos )
work . leave_kudos ()
work . reload ()
print ( work . kudos )
392
393
您可能想做的另一件事是獲取有關誰寫的工作 /評論的信息。為此,我們使用AO3.User
類。
import AO3
user = AO3 . User ( "bothersomepotato" )
print ( user . url )
print ( user . bio )
print ( user . works ) # Number of works published
https://archiveofourown.org/users/bothersomepotato
University student, opening documents to write essays but writing this stuff instead. No regrets though. My Tumblr, come chat with -or yell at- me if you feel like it! :)
2
要搜索作品,您可以使用AO3.search()
函數,然後解析返回的美麗的對象,也可以使用AO3.Search
類為您自動做到這一點。
import AO3
search = AO3 . Search ( any_field = "Clarke Lexa" , word_count = AO3 . utils . Constraint ( 5000 , 15000 ))
search . update ()
print ( search . total_results )
for result in search . results :
print ( result )
3074
<Work [five times lexa falls for clarke]>
<Work [an incomplete list of reasons (why Clarke loves Lexa)]>
<Work [five times clarke and lexa aren’t sure if they're a couple or not]>
<Work [Chemistry]>
<Work [The New Commander (Lexa Joining Camp Jaha)]>
<Work [Ode to Clarke]>
<Work [it's always been (right in front of me)]>
<Work [The Girlfriend Tag]>
<Work [The After-Heda Chronicles]>
<Work [The Counter]>
<Work [May We Meet Again]>
<Work [No Filter]>
<Work [The Games We Play]>
<Work [A l'épreuve des balles]>
<Work [Celebration]>
<Work [Another level of fucked up]>
<Work [(Don't Ever Want to Tame) This Wild Heart]>
<Work [Self Control]>
<Work [Winter]>
<Work [My only wish]>
然後,您可以使用WorkID加載您搜索的一件作品。要獲得比前20個作品的更多,請使用
search . page = 2
您可能想要採取的許多操作可能需要一個AO3帳戶。如果您已經有一個,則可以使用AO3.Session對象訪問這些操作。您首先登錄使用用戶名和密碼,然後可以使用該對象訪問受限制的內容。
import AO3
session = AO3 . Session ( "username" , "password" )
print ( f"Bookmarks: { session . bookmarks } " )
session . refresh_auth_token ()
print ( session . kudos ( AO3 . Work ( 18001499 , load = False ))
Bookmarks: 67
True
我們成功地離開了Kudos,並檢查了我們的書籤。對於某些活動,例如留下榮譽和評論,需要session.refresh_auth_token()
。如果它過期或忘記調用此功能,則錯誤AO3.utils.AuthError: Invalid authentication token. Try calling session.refresh_auth_token()
。
您還可以通過致電Work.leave_kudos()
/ Work.comment()
來評論 /離開工作中的榮譽,並且只要您已經使用會話實例化了該對象( AO3.Work(xxxxxx, session=sess)
或使用Work.set_session()
)。這可能是最好的方法,因為您會遇到較少的身份驗證問題(因為將使用工作的真實性令牌)。
如果您希望匿名發表評論或榮譽,則可以使用AO3.GuestSession
就像您使用普通會話一樣,除非您無法查看書籤,訂閱等。因為您'我實際上沒有登錄。
要檢索和處理評論線程,您可能需要查看Work.get_comments()
方法。它返回特定章節及其各自主題中的所有評論。然後,您可以根據需要處理它們。讓我們看看:
from time import time
import AO3
work = AO3 . Work ( 24560008 )
work . load_chapters ()
start = time ()
comments = work . get_comments ( 5 )
print ( f"Loaded { len ( comments ) } comment threads in { round ( time () - start , 1 ) } seconds n " )
for comment in comments :
print ( f"Comment ID: { comment . id } n Replies: { len ( comment . get_thread ()) } " )
Loaded 5 comment threads in 1.8 seconds
Comment ID: 312237184
Replies: 1
Comment ID: 312245032
Replies: 1
Comment ID: 312257098
Replies: 1
Comment ID: 312257860
Replies: 1
Comment ID: 312285673
Replies: 2
加載評論需要很長時間,因此您應該嘗試盡可能少地使用它。這也導致許多請求發送到AO3服務器,這可能導致遇到錯誤utils.HTTPError: We are being rate-limited. Try again in a while or reduce the number of requests
。如果發生這種情況,您應該嘗試將請求放大或減少其數字。還可以選擇使用AO3.utils.limit_requests()
啟用請求限制請求,這使得您不能在特定時間窗口中提出的x請求多於x請求。您還可以使用Comment.reply()
函數回复註釋,或使用Comment.delete()
刪除一個(如果是您的話)。
AO3.EXTRA包含代碼,以下載一些額外的資源,這些資源不是此軟件包功能的核心,並且不會經常更改。一個例子是AO3認可的粉絲列表。要下載資源,只需使用AO3.extra.download(resource_name)
。要下載每個資源,您可以使用AO3.extra.download_all()
。要查看可用資源的列表,請使用AO3.extra.get_resources()
。
有關信息或錯誤報告,請聯繫[email protected]。
麻省理工學院