This commit is contained in:
yumoqing 2020-04-26 15:16:31 +08:00
parent e11a398439
commit 35b6d7a565
5 changed files with 89 additions and 38 deletions

View File

@ -86,7 +86,8 @@ class PressableLabel(ButtonBehavior, Text):
class HTTPDataHandler(EventDispatcher):
def __init__(self, url,method='GET',params={},
headers={},
files={}
files={},
stream=False
):
EventDispatcher.__init__(self)
self.url = url
@ -94,6 +95,7 @@ class HTTPDataHandler(EventDispatcher):
self.params = params
self.headers = headers
self.files=files
self.stream=stream
self.hc = App.get_running_app().hc
self.register_event_type('on_success')
self.register_event_type('on_error')
@ -129,6 +131,8 @@ class HTTPDataHandler(EventDispatcher):
self.hc(self.url,method=self.method,
params=p,
headers=h,
files=self.files,
stream=self.stream,
callback=self.onSuccess,
errback=self.onError)

35
kivyblocks/hostimage.py Normal file
View File

@ -0,0 +1,35 @@
import tempfile
from kivyblocks.baseWidget import HTTPDataHandler
from kivyblocks.utils import blocksImage, absurl
from kivy.uix.image import Image
class HostImage(Image):
def __init__(self, target,**kwargs):
self.options = kwargs
self.target = target
kwargs['source'] = blocksImage('running.gif')
url = kwargs.get('url')
del kwargs['url']
super().__init__(**kwargs)
self.downloadImage(url)
def downloadImage(url):
realurl = absurl(url,self.target.parenturl)
loader = HTTPDataHandler(url,stream=True)
loader.bind(on_success=self.createTmpfile)
loader.bind(on_failed=self.showBadImage)
loader.handle()
def showBadImage(self,o,e):
self.source = blocksImage('break.png')
def createTmpfile(self,o,resp):
fn = tempfile.NamedTemporaryFile(delete=True)
with open(fn, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# f.flush()
self.source = fn

View File

@ -152,7 +152,7 @@ class PageLoader(EventDispatcher):
filter
}
events:
'on_bufferraise': erase
'on_deletepage': erase
"""
class RelatedLoader(PageLoader):
@ -244,8 +244,6 @@ class RelatedLoader(PageLoader):
class Paging(PageLoader):
def __init__(self,**options):
PageLoader.__init__(self,**options)
self.adder = options.get('adder')
self.clearer = options.get('clearer')
self.target = options.get('target')
self.init()
@ -254,6 +252,7 @@ class Paging(PageLoader):
kwargs['size_hint_y'] = None
kwargs['height'] = CSize(2)
kwargs['orientation'] = 'horizontal'
kwargs['spacing'] = CSize(1)
self.widget = BoxLayout(**kwargs)
self.b_f = PagingButton(text="|<")
self.b_p = PagingButton(text="<")
@ -270,16 +269,6 @@ class Paging(PageLoader):
if self.filter:
self.widget.add_widget(self.filter)
def show_page(self,o,d):
super().show_page(o,data)
d = DictObject(**d)
self.total_cnt = d.total
self.calculateTotalPage()
self.clearer()
for r in d.rows:
self.adder(r)
self.loading = False
def loadFirstPage(self,o=None):
if self.curpage == 1:
print('return not loading')

View File

@ -7,6 +7,7 @@ from .vplayer import VPlayer
from .form import Form, StrSearchForm
from .boxViewer import BoxViewer
from .pagescontainer import PageContainer
from .hostimage import HostImage
r = Factory.register
r('PageContainer', PageContainer)
@ -36,6 +37,7 @@ r('PressableLabel', PressableLabel)
r('Tree',Tree)
r('TextTree',TextTree)
r('PopupMenu',PopupMenu)
r('HostImage',HostImage)
if platform == 'android':
r('PhoneButton',PhoneButton)
r('AWebView',AWebView)

View File

@ -89,15 +89,14 @@ class HttpClient:
pre = '/'.join(parts)
return pre
def webcall(self,url,method="GET",params={},files={},headers={}):
def _webcall(self,url,method="GET",
params={},
files={},
headers={}
stream=False):
app = App.get_running_app()
domain = self.url2domain(url)
sessionid = hostsessions.get(domain,None)
print('hostsessions=', hostsessions,
'sessionid=',sessionid,
'domain=',domain,
'url=',url
)
if sessionid:
headers.update({'session':sessionid})
elif app.getAuthHeader():
@ -114,9 +113,35 @@ class HttpClient:
if resp.status_code == 200:
h = resp.headers.get('Set-Cookie',None)
if h:
print('*************set-Cookie=',h,'domain=',domain)
sessionid = h.split(';')[0]
hostsessions[domain] = sessionid
if resp.status_code == 401:
print('NeedLogin:',url)
raise NeedLogin
if resp.status_code == 403:
raise InsufficientPrivilege
if resp.status_code != 200:
print('Error', url, method,
params, resp.status_code,
type(resp.status_code))
raise HTTPError(resp.status_code)
return resp
def webcall(self,url,method="GET",
params={},
files={},
headers={},
stream=False):
resp = self._webcall(url,method=method,
params=params,
files=files,
headers=headers,
stream=stream)
if stream:
return resp
try:
data = resp.json()
if type(data) != type({}):
@ -129,19 +154,14 @@ class HttpClient:
return data
except:
return resp.text
if resp.status_code == 401:
print('NeedLogin:',url)
raise NeedLogin
if resp.status_code == 403:
raise InsufficientPrivilege
print('Error', url, method,
params, resp.status_code,
type(resp.status_code))
raise HTTPError(resp.status_code)
def __call__(self,url,method="GET",params={},headers={},files={},callback=None,errback=None):
def __call__(self,url,method="GET",
params={},
headers={},
files={},
stream=False,
callback=None,
errback=None):
def cb(t,resp):
return resp
@ -161,6 +181,7 @@ class HttpClient:
"method":method,
"params":params,
"files":files,
"stream":stream,
"headers":headers
}
self.workers.add(self.webcall,callback,errback,kwargs=kwargs)