summaryrefslogtreecommitdiff
path: root/plugin.video.mediathekview/resources/lib/mvutils.py
blob: 7ccaf3a8ee1ea6941f79883c394db86d864caea9 (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
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2018, Leo Moll

# -- Imports ------------------------------------------------
import os
import sys
import stat
import string
import urllib
import urllib2

from contextlib import closing
from resources.lib.exceptions import ExitRequested

# -- Kodi Specific Imports ----------------------------------
try:
	import xbmcvfs
	is_kodi = True
except ImportError:
	is_kodi = False

# -- 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, aborthook = None ):
	with closing( urllib2.urlopen( url ) ) as u, closing( open( filename, 'wb' ) ) as f:
		_chunked_url_copier( u, f, reporthook, chunk_size, aborthook )

def url_retrieve_vfs( url, filename, reporthook, chunk_size = 8192, aborthook = None ):
	with closing( urllib2.urlopen( url ) ) as u, closing( xbmcvfs.File( filename, 'wb' ) ) as f:
		_chunked_url_copier( u, f, reporthook, chunk_size, aborthook )

def build_url( query ):
	return sys.argv[0] + '?' + urllib.urlencode( query )

def _chunked_url_copier( u, f, reporthook, chunk_size, aborthook ):
	aborthook = aborthook if aborthook is not None else lambda: False
	total_size = int( u.info().getheader( 'Content-Length' ).strip() ) if u.info() and u.info().getheader( 'Content-Length' ) else 0
	total_chunks = 0

	while not aborthook():
		reporthook( total_chunks, chunk_size, total_size )
		chunk = u.read( chunk_size )
		if not chunk:
			# operation has finished
			return
		f.write( chunk )
		total_chunks += 1
	# abort requested
	raise ExitRequested( 'Reception interrupted.' )