summaryrefslogtreecommitdiff
path: root/plugin.video.mediathekview/resources/lib/mvutils.py
blob: d4c7663d6bf2029efb28f94e757560096c77eac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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, [], )