Updated README.md
parent
8c8944b822
commit
56e97e1f46
158
README.md
158
README.md
|
@ -17,90 +17,94 @@ Usage
|
||||||
|
|
||||||
Getting list of files inside torrent is straightforward:
|
Getting list of files inside torrent is straightforward:
|
||||||
|
|
||||||
import xbmc
|
```python
|
||||||
from torrent2http import State, Engine, MediaType
|
import xbmc
|
||||||
from contextlib import closing
|
from torrent2http import State, Engine, MediaType
|
||||||
|
from contextlib import closing
|
||||||
|
|
||||||
# Create instance of Engine
|
# Create instance of Engine
|
||||||
engine = Engine(uri="...")
|
engine = Engine(uri="...")
|
||||||
files = []
|
files = []
|
||||||
# Ensure we'll close engine on exception
|
# Ensure we'll close engine on exception
|
||||||
with closing(engine):
|
with closing(engine):
|
||||||
# Start engine
|
# Start engine
|
||||||
engine.start()
|
engine.start()
|
||||||
# Wait until files received
|
# Wait until files received
|
||||||
while not files and not xbmc.abortRequested:
|
while not files and not xbmc.abortRequested:
|
||||||
# Will list only video files in torrent
|
# Will list only video files in torrent
|
||||||
files = engine.list(media_types=[MediaType.VIDEO])
|
files = engine.list(media_types=[MediaType.VIDEO])
|
||||||
# Check if there is loading torrent error and raise exception
|
# Check if there is loading torrent error and raise exception
|
||||||
engine.check_torrent_error()
|
engine.check_torrent_error()
|
||||||
xbmc.sleep(200)
|
xbmc.sleep(200)
|
||||||
|
```
|
||||||
|
|
||||||
### Start streaming ###
|
### Start streaming ###
|
||||||
|
|
||||||
import xbmc
|
```python
|
||||||
from torrent2http import State, Engine, MediaType
|
import xbmc
|
||||||
from contextlib import closing
|
from torrent2http import State, Engine, MediaType
|
||||||
|
from contextlib import closing
|
||||||
|
|
||||||
# XBMC addon handle
|
# XBMC addon handle
|
||||||
handle = ...
|
handle = ...
|
||||||
# Playable list item
|
# Playable list item
|
||||||
listitem = ...
|
listitem = ...
|
||||||
# We can know file_id of needed video file on this step, if no, we'll try to detect one.
|
# We can know file_id of needed video file on this step, if no, we'll try to detect one.
|
||||||
file_id = None
|
file_id = None
|
||||||
# Flag will set to True when engine is ready to resolve URL to XBMC
|
# Flag will set to True when engine is ready to resolve URL to XBMC
|
||||||
ready = False
|
ready = False
|
||||||
# Set pre-buffer size to 15Mb. This is a size of file that need to be downloaded before we resolve URL to XMBC
|
# Set pre-buffer size to 15Mb. This is a size of file that need to be downloaded before we resolve URL to XMBC
|
||||||
pre_buffer_bytes = 15*1024*1024
|
pre_buffer_bytes = 15*1024*1024
|
||||||
engine = Engine(uri="...")
|
engine = Engine(uri="...")
|
||||||
with closing(engine):
|
with closing(engine):
|
||||||
# Start engine and instruct torrent2http to begin download first file,
|
# Start engine and instruct torrent2http to begin download first file,
|
||||||
# so it can start searching and connecting to peers
|
# so it can start searching and connecting to peers
|
||||||
engine.start(file_id or 0)
|
engine.start(file_id or 0)
|
||||||
while not xbmc.abortRequested and not ready:
|
while not xbmc.abortRequested and not ready:
|
||||||
xbmc.sleep(500)
|
xbmc.sleep(500)
|
||||||
status = engine.status()
|
status = engine.status()
|
||||||
# Check if there is loading torrent error and raise exception
|
# Check if there is loading torrent error and raise exception
|
||||||
engine.check_torrent_error(status)
|
engine.check_torrent_error(status)
|
||||||
# Trying to detect file_id
|
# Trying to detect file_id
|
||||||
if file_id is None:
|
if file_id is None:
|
||||||
# Get torrent files list, filtered by video file type only
|
# Get torrent files list, filtered by video file type only
|
||||||
files = engine.list(media_types=[MediaType.VIDEO])
|
files = engine.list(media_types=[MediaType.VIDEO])
|
||||||
# If torrent metadata is not loaded yet then continue
|
# If torrent metadata is not loaded yet then continue
|
||||||
if files is None:
|
if files is None:
|
||||||
continue
|
continue
|
||||||
# Torrent has no video files
|
# Torrent has no video files
|
||||||
if not files:
|
if not files:
|
||||||
break
|
break
|
||||||
# Select first matching file
|
# Select first matching file
|
||||||
file_id = files[0].index
|
file_id = files[0].index
|
||||||
file_status = files[0]
|
file_status = files[0]
|
||||||
else:
|
else:
|
||||||
# If we've got file_id already, get file status
|
# If we've got file_id already, get file status
|
||||||
file_status = engine.file_status(file_id)
|
file_status = engine.file_status(file_id)
|
||||||
# If torrent metadata is not loaded yet then continue
|
# If torrent metadata is not loaded yet then continue
|
||||||
if not file_status:
|
if not file_status:
|
||||||
continue
|
continue
|
||||||
if status.state == State.DOWNLOADING:
|
if status.state == State.DOWNLOADING:
|
||||||
# Wait until minimum pre_buffer_bytes downloaded before we resolve URL to XBMC
|
# Wait until minimum pre_buffer_bytes downloaded before we resolve URL to XBMC
|
||||||
if file_status.download >= pre_buffer_bytes:
|
if file_status.download >= pre_buffer_bytes:
|
||||||
ready = True
|
|
||||||
break
|
|
||||||
elif status.state in [State.FINISHED, State.SEEDING]:
|
|
||||||
# We have already downloaded file
|
|
||||||
ready = True
|
ready = True
|
||||||
break
|
break
|
||||||
# Here you can update pre-buffer progress dialog, for example.
|
elif status.state in [State.FINISHED, State.SEEDING]:
|
||||||
# Note that State.CHECKING also need waiting until fully finished, so it better to use resume_file option
|
# We have already downloaded file
|
||||||
# for engine to avoid CHECKING state if possible.
|
ready = True
|
||||||
# ...
|
break
|
||||||
if ready:
|
# Here you can update pre-buffer progress dialog, for example.
|
||||||
# Resolve URL to XBMC
|
# Note that State.CHECKING also need waiting until fully finished, so it better to use resume_file option
|
||||||
listitem.SetPath(file_status.url)
|
# for engine to avoid CHECKING state if possible.
|
||||||
xbmcplugin.SetResolvedUrl(handle, True, listitem)
|
# ...
|
||||||
# Wait until playing finished or abort requested
|
if ready:
|
||||||
while not xbmc.abortRequested and xbmc.Player().isPlaying():
|
# Resolve URL to XBMC
|
||||||
xbmc.sleep(500)
|
listitem.SetPath(file_status.url)
|
||||||
|
xbmcplugin.SetResolvedUrl(handle, True, listitem)
|
||||||
|
# Wait until playing finished or abort requested
|
||||||
|
while not xbmc.abortRequested and xbmc.Player().isPlaying():
|
||||||
|
xbmc.sleep(500)
|
||||||
|
```
|
||||||
|
|
||||||
### Fully working example ###
|
### Fully working example ###
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue