summaryrefslogtreecommitdiff
path: root/plugin.video.mediathekview/resources/lib/mvutils.py
diff options
context:
space:
mode:
Diffstat (limited to 'plugin.video.mediathekview/resources/lib/mvutils.py')
-rw-r--r--plugin.video.mediathekview/resources/lib/mvutils.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/plugin.video.mediathekview/resources/lib/mvutils.py b/plugin.video.mediathekview/resources/lib/mvutils.py
new file mode 100644
index 0000000..d4c7663
--- /dev/null
+++ b/plugin.video.mediathekview/resources/lib/mvutils.py
@@ -0,0 +1,87 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2017-2018, Leo Moll
+
+# -- Imports ------------------------------------------------
+import os
+import stat
+import string
+import urllib2
+import xbmcvfs
+
+from contextlib import closing
+
+# -- Functions ----------------------------------------------
+def dir_exists( name ):
+ try:
+ s = os.stat( name )
+ return stat.S_ISDIR( s.st_mode )
+ except OSError:
+ return False
+
+def file_exists( name ):
+ try:
+ s = os.stat( name )
+ return stat.S_ISREG( s.st_mode )
+ except OSError:
+ return False
+
+def file_size( name ):
+ try:
+ s = os.stat( name )
+ return s.st_size
+ except OSError:
+ return 0
+
+def find_xz():
+ for xzbin in [ '/bin/xz', '/usr/bin/xz', '/usr/local/bin/xz' ]:
+ if file_exists( xzbin ):
+ return xzbin
+ return None
+
+def make_search_string( val ):
+ cset = string.letters + string.digits + ' _-#'
+ search = ''.join( [ c for c in val if c in cset ] )
+ return search.upper().strip()
+
+def make_duration( val ):
+ if val == "00:00:00":
+ return None
+ elif val is None:
+ return None
+ x = val.split( ':' )
+ if len( x ) != 3:
+ return None
+ return int( x[0] ) * 3600 + int( x[1] ) * 60 + int( x[2] )
+
+def cleanup_filename( val ):
+ cset = string.letters + string.digits + u' _-#äöüÄÖÜßáàâéèêíìîóòôúùûÁÀÉÈÍÌÓÒÚÙçÇœ'
+ search = ''.join( [ c for c in val if c in cset ] )
+ return search.strip()
+
+def url_retrieve( url, filename, reporthook, chunk_size = 8192 ):
+ with closing( urllib2.urlopen( url ) ) as u, closing( open( filename, 'wb' ) ) as f:
+ total_size = int( u.info().getheader( 'Content-Length' ).strip() ) if u.info() and u.info().getheader( 'Content-Length' ) else 0
+ total_chunks = 0
+
+ while True:
+ reporthook( total_chunks, chunk_size, total_size )
+ chunk = u.read( chunk_size )
+ if not chunk:
+ break
+ f.write( chunk )
+ total_chunks += 1
+ return ( filename, [], )
+
+def url_retrieve_vfs( videourl, filename, reporthook, chunk_size = 8192 ):
+ with closing( urllib2.urlopen( videourl ) ) as u, closing( xbmcvfs.File( filename, 'wb' ) ) as f:
+ total_size = int( u.info().getheader( 'Content-Length' ).strip() ) if u.info() and u.info().getheader( 'Content-Length' ) else 0
+ total_chunks = 0
+
+ while True:
+ reporthook( total_chunks, chunk_size, total_size )
+ chunk = u.read( chunk_size )
+ if not chunk:
+ break
+ f.write( chunk )
+ total_chunks += 1
+ return ( filename, [], )