|
Here's an example of using the DOPAL interactive module - it gives you an idea of what the API is like. We'll just play around with a few torrents and print some data.
Let's get run the dopal.interact module...
-
Enter host: 127.0.0.1
Enter port (default is 6884):
Enter user name (leave blank if not applicable):
------------------------
DOPAL 0.54 - interact module
Connection object stored in 'connection' variable.
Plugin interface stored in 'interface' variable.
>>> sys.tracebacklimit = 3
>>> print connection
AzureusObjectConnection for 127.0.0.1 [Azureus 2.3.0.7_B16]
Now let's see what's being downloaded.
-
>>> dm = interface.getDownloadManager()
>>> dm
<DopalDownloadManager object at 0x009EEE90, sid=9EC940>
>>> downloads = dm.getDownloads()
>>> for download in downloads:
... print download
...
Download: [Lunar] Bleach - 59 [EADDC84D].avi [Downloading, 16.1%]
Download: [AnimeLito]_Rozen_Maiden_Traumend_9_[3E237847].avi [Downloading, 25.1%]
Download: RSSImport_2.2.1.zip [Queued, 100.0%]
Download: [Lunar] Suzuka - 23 [7A531830].avi [Stopped, 0.0%]
Download: Gokusen [Stopped, 0.0%]
Download: UKTV.80s.Ads.divx [Seeding, 100.0%]
>>> downloads[0]
<DopalDownload object at 0x00D6AE30, sid=9EC8AE, for "[Lunar] Bleach - 59 [EADDC84D].avi">
Let's change what is being downloaded, so only the torrent with the largest amount of data remaining to be downloaded is running.
First of all - let's just get all the torrents which haven't been downloaded yet.
(Note: The "< 1000" test is done because the "completed" attribute represents percentages as a value out of 1000.)
-
>>> not_downloaded = [dl for dl in downloads if dl.stats.completed < 1000]
>>> for download in not_downloaded:
... print download
...
Download: [Lunar] Bleach - 59 [EADDC84D].avi [Downloading, 16.1%]
Download: [AnimeLito]_Rozen_Maiden_Traumend_9_[3E237847].avi [Downloading, 25.1%]
Download: [Lunar] Suzuka - 23 [7A531830].avi [Stopped, 0.0%]
Download: Gokusen [Stopped, 0.0%]
Now we'll stop them all.
-
>>> for download in not_downloaded:
... download.stop()
...
Traceback (most recent call last):
File "<console>", line 2, in ?
File "c:\dopal-cvs\src\dopal\objects.py", line 292, in stop
return self.invoke_object_method(__funcname__, args, **kwargs)
RemoteMethodError: Download::stop: download already stopped
Oops! I should've been more careful there. The first two have probably been stopped - the third one is likely to have caused the error. Let's examine the objects again.
-
>>> for download in not_downloaded:
... print download
...
Download: [Lunar] Bleach - 59 [EADDC84D].avi [Downloading, 16.1%]
Download: [AnimeLito]_Rozen_Maiden_Traumend_9_[3E237847].avi [Downloading, 25.1%]
Download: [Lunar] Suzuka - 23 [7A531830].avi [Stopped, 0.0%]
Download: Gokusen [Stopped, 0.0%]
>>>
>>>
>>>
>>> for download in not_downloaded:
... download.refresh_object() # This is a DOPAL-specific method.
... print download
...
Download: [Lunar] Bleach - 59 [EADDC84D].avi [Stopped, 34.7%]
Download: [AnimeLito]_Rozen_Maiden_Traumend_9_[3E237847].avi [Stopped, 57.2%]
Download: [Lunar] Suzuka - 23 [7A531830].avi [Stopped, 0.0%]
Download: Gokusen [Stopped, 0.0%]
Time to find the one with the most to download. We'll sort by data remaining to find the one we are after.
-
>>> def get_bytes_remaining(download):
...
...
... return download.torrent.size - download.stats.downloaded
...
>>> def compare_by_remaining_bytes(download_a, download_b):
... a_remaining = get_bytes_remaining(download_a)
... b_remaining = get_bytes_remaining(download_b)
... return cmp(a_remaining, b_remaining)
...
>>> not_downloaded.sort(compare_by_remaining_bytes)
>>> for dl in not_downloaded:
... print get_bytes_remaining(dl), dl
...
82529571 Download: [AnimeLito]_Rozen_Maiden_Traumend_9_[3E237847].avi [Stopped, 57.2%]
118019862 Download: [Lunar] Bleach - 59 [EADDC84D].avi [Stopped, 34.7%]
178530304 Download: [Lunar] Suzuka - 23 [7A531830].avi [Stopped, 0.0%]
2384037376 Download: Gokusen [Stopped, 0.0%]
Looks like the last torrent has the most remaining (we haven't even started it - it's around 2 GB's). Let's fire it up.
-
>>> not_downloaded[-1].start()
Traceback (most recent call last):
File "<console>", line 1, in ?
File "c:\dopal-cvs\src\dopal\objects.py", line 292, in start
return self.invoke_object_method(__funcname__, args, **kwargs)
RemoteMethodError: Download::start: download not ready (state=70)
>>>
>>>
>>> not_downloaded[-1].restart()
>>>
>>>
>>> print not_downloaded[-1]
Download: Gokusen [Stopped, 0.0%]
>>>
>>>
>>> not_downloaded[-1].refresh_object()
>>> print not_downloaded[-1]
Download: Gokusen [Downloading, 0.0%]
>>>
|