0.9.6 - Android

master
inpos 2016-04-02 13:15:55 +03:00
parent 7557ed8223
commit c594801130
2 changed files with 45 additions and 10 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.module.pyrrent2http" name="pyrrent2http" version="0.9.5" provider-name="inpos"> <addon id="script.module.pyrrent2http" name="pyrrent2http" version="0.9.6" provider-name="inpos">
<requires> <requires>
<import addon="xbmc.python" version="2.14.0"/> <import addon="xbmc.python" version="2.14.0"/>
<import addon="script.module.libtorrent" /> <import addon="script.module.libtorrent" />

53
lib/pyrrent2http/pyrrent2http.py 100644 → 100755
View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os, sys
import chardet import chardet
try: try:
@ -26,6 +26,17 @@ import io
from util import localize_path, Struct, detect_media_type, uri2path, encode_msg from util import localize_path, Struct, detect_media_type, uri2path, encode_msg
if os.getenv('ANDROID_ROOT'):
from ctypes import *
libc = CDLL('/system/lib/libc.so')
libc.lseek64.restype = c_ulonglong
libc.lseek64.argtypes = [c_uint, c_ulonglong, c_uint]
libc.read.restype = c_long
libc.read.argtypes = [c_uint, c_void_p, c_long]
O_RDONLY = 0
O_LARGEFILE = 0x8000
###################################################################################### ######################################################################################
if not hasattr(os, 'getppid'): if not hasattr(os, 'getppid'):
@ -165,7 +176,10 @@ class TorrentFile(object):
logging.info('Waiting for file: %s' % (self.save_path,)) logging.info('Waiting for file: %s' % (self.save_path,))
self.tfs.handle.flush_cache() self.tfs.handle.flush_cache()
time.sleep(0.5) time.sleep(0.5)
self.filePtr = io.open(self.save_path, 'rb') if os.getenv('ANDROID_ROOT'):
self.filePtr = libc.open(self.save_path, O_RDONLY | O_LARGEFILE, 755)
else:
self.filePtr = io.open(self.save_path, 'rb')
return self.filePtr return self.filePtr
def log(self, message): def log(self, message):
fnum = self.tfs.openedFiles.index(self) fnum = self.tfs.openedFiles.index(self)
@ -177,7 +191,10 @@ class TorrentFile(object):
def SetPriority(self, priority): def SetPriority(self, priority):
self.tfs.setPriority(self.index, priority) self.tfs.setPriority(self.index, priority)
def readOffset(self): def readOffset(self):
return self.filePtr.seek(0, os.SEEK_CUR) if os.getenv('ANDROID_ROOT'):
return libc.lseek64(self.filePtr, 0, os.SEEK_CUR)
else:
return self.filePtr.seek(0, os.SEEK_CUR)
def havePiece(self, piece): def havePiece(self, piece):
return self.tfs.handle.have_piece(piece) return self.tfs.handle.have_piece(piece)
def pieceFromOffset(self, offset): def pieceFromOffset(self, offset):
@ -210,7 +227,10 @@ class TorrentFile(object):
self.tfs.removeOpenedFile(self) self.tfs.removeOpenedFile(self)
self.closed = True self.closed = True
if self.filePtr is not None: if self.filePtr is not None:
self.filePtr.close() if os.getenv('ANDROID_ROOT'):
libc.close(self.filePtr)
else:
self.filePtr.close()
self.filePtr = None self.filePtr = None
def ShowPieces(self): def ShowPieces(self):
pieces = self.tfs.handle.status().pieces pieces = self.tfs.handle.status().pieces
@ -234,7 +254,10 @@ class TorrentFile(object):
for i in range(startPiece, endPiece + 1): for i in range(startPiece, endPiece + 1):
if not self.waitForPiece(i): if not self.waitForPiece(i):
raise IOError raise IOError
read = filePtr.readinto(buf) if os.getenv('ANDROID_ROOT'):
read = libc.read(self.filePtr, addressof(buf), toRead)
else:
read = filePtr.readinto(buf)
return read return read
def Seek(self, offset, whence): def Seek(self, offset, whence):
filePtr = self.__fileptr_() filePtr = self.__fileptr_()
@ -242,7 +265,10 @@ class TorrentFile(object):
if whence == os.SEEK_END: if whence == os.SEEK_END:
offset = self.size - offset offset = self.size - offset
whence = os.SEEK_SET whence = os.SEEK_SET
newOffset = filePtr.seek(offset, whence) if os.getenv('ANDROID_ROOT'):
newOffset = libc.lseek64(self.filePtr, offset, whence)
else:
newOffset = filePtr.seek(offset, whence)
self.log('Seeking to %d/%d' % (newOffset, self.size)) self.log('Seeking to %d/%d' % (newOffset, self.size))
return newOffset return newOffset
def IsComplete(self): def IsComplete(self):
@ -405,17 +431,26 @@ def HttpHandlerFactory():
f.Seek(start_range, 0) f.Seek(start_range, 0)
chunk = f.piece_length chunk = f.piece_length
total = 0 total = 0
buf = bytearray(chunk) if os.getenv('ANDROID_ROOT'):
buf = create_string_buffer(chunk)
else:
buf = bytearray(chunk)
while chunk > 0: while chunk > 0:
if start_range + chunk > end_range: if start_range + chunk > end_range:
chunk = end_range - start_range chunk = end_range - start_range
buf = bytearray(chunk) if os.getenv('ANDROID_ROOT'):
buf = create_string_buffer(chunk)
else:
buf = bytearray(chunk)
try: try:
if f.Read(buf) < 1: break if f.Read(buf) < 1: break
while self.server.root_obj.pause: while self.server.root_obj.pause:
time.sleep(0.1) time.sleep(0.1)
continue continue
self.wfile.write(buf) if os.getenv('ANDROID_ROOT'):
self.wfile.write(buf.raw)
else:
self.wfile.write(buf)
except: except:
break break
total += chunk total += chunk