<commit_before>settings = {
	# path and name of the sqlite3 database to use
	# use only a file name to use the database in
	# the current working directory
	# Leave empty to disable the database
	'database': r'/var/twonky/TwonkyServer/tw-video-scraper.db',
	
	# Path where to download and extract the files
	# downloaded from TVDB. The script will check
	# if a file already exists, so it will not be
	# downloaded again.
	'tmpdir': r'/tmp/tw-video-scraper/',

	# List of name of the parent directories
	# Used for matching the directory name of the name
	# of a movie/serie. By knowing the parent dir,the
	# script knows if it should analyse the name
	# E.g. ~/download/my.movie.2011.avi vs. ~/download/My.Movie.2011/part1.avi
	'parentdir': ['download','series','movies','sample'],
	
	# Regular expression patterns to determine if
	# a file is a series. First match should product
	# the title, second match the season, third match
	# the episode number
	'seriepatterns': ['(.*?)S(\d{1,2})E(\d{2})(.*)',
					  '(.*?)\[?(\d{1,2})x(\d{2})\]?(.*)',
					  '(.*?)Season.?(\d{1,2}).*?Episode.?(\d{1,2})(.*)'],
	
	# TVDB api settings
	'tvdbapikey': '6262A88CCAB7E724',
	'tvdblang': 'en',
	
	# Regular expression patterns for movies
	# First try to get the year from the title
	# If that fails, try to match the whole name
	'moviepatterns': ['(.*?)\((\d{4})\)(.*)',
					  '(.*?)\[(\d{4})\](.*)',
					  '(.*?)\{(\d{4})\}(.*)',
					  '(.*?)(\d{4})(.*)',
					  '(.*?)\.(avi|mkv|mpg|mgep|mp4)',
					  '(.*)'],
	# MovieDB api settings
	'moviedbapikey': '<KEY>',

	# Files downloaded from TVDB are stored in the tmpdir,
	# and not downloaded if already present. If the file
	# is too old, it will be deleted and downloaded again
	# This is the number of days after which to delete files
	# and download it again
	'cacherenew': 6,

	# By default, images are stored in the cache folder under
	# a subdirectory for the scale. E.g is a media renderer asks
	# for a thumnail, it can request the scale, such as ?scale=100x100
	# Images from this script are stored in that folder, but are saved
	# in the original size.
	# If you have multiple media renderers, they can request thumbnails
	# in different sizes, leading to duplication of images.
	# Valid option for this setting are:
	# none : (Default option) Store images in a scale subdirectory, in the
	#     original size
	# resize : (Requires PIL Image) Store images in a scale subdirectory, resize
	#     the image to the correct scale. If 'keepaspectratio' is set to 
	#     false, the exact scale will be used, otherwise, the aspect ratio 
	#     is preserved, adding black borders
	# symlink : (Linux only) Store images in an 'Original' folder, and make
	#     symbolic links for each entry in 'symbolicfolders'
	'scaleoption': 'symlink',
	'keepaspectratio': 'true',
	'symbolicfolders': ['0x0','100x100','160x160', '1920x1080'],
	
	# Sometimes the downloaded JPEG cannot be in the correct format to display on
	# certain deviced, e.g. the XBOX360 cannot display JPEG not of time JFIF standard
	# Set this to true to this for all images (requires PIL Image)
	'fixjpeg': 'true',
	
	# If no thumbnail can be found, a thumbnail can be generated
	# using the following command. Leave empty to disable generating
	# thumbnails
	'generatecommand': r'ffmpeg -itsoffset -30 -i "$infile" -y -vcodec mjpeg -vframes 1 -an -f rawvideo -s 624x352 "$outfile"',
}

def main():
	import sys
	
	if not Config['tmpdir'].endswith('/'):
		Config['tmpdir'] = Config['tmpdir']+'/'
	
	inputfile = None
	inputdirectory = None
	outputimage = None
	
	# Validate input arguments
	if len(sys.argv) < 3:
		print("Usage: tw-video-scraper.py inputmovie outputimage")
		exit()
	
	# for windows path names
	if sys.argv[1].find('\\') >= 0:
		sys.argv[1] = sys.argv[1].replace('\\','/')

	serie = Serie(sys.argv[1])
	
	if serie.isSerie():
		thumbnail = serie.getThumbnail()
	else:
		movie = Movie(sys.argv[1])
		thumbnail = movie.getThumbnail()

	if thumbnail:
		print("Downloading file "+thumbnail+"...")
		URL(thumbnail).download(sys.argv[2])
	else:
		file = sys.argv[1]
		if file.find('/') >= 0:
			file = file[file.rindex('/')+1:]
			
		print("Could not get enough information from file name '"+file+"'")
		if Config['generatecommand'] != '':
			print("Generating thumbnail...")
			try:
				import os
				Config['generatecommand'] = Config['generatecommand'].replace('$infile',sys.argv[1]).replace('$outfile',sys.argv[2])
				os.system(Config['generatecommand'])
			except:
				print("Failed to execute generate thumbnail command")
				
	if Config['fixjpeg'] == 'true':
		try:
			import Image
			image = Image.open(sys.argv[2])
			image.save(sys.argv[2])
		except:
			pass

	if Config['scaleoption'] == 'resize':
		# resize the image
		try:
			import Image, re
			# try to get the scale option using regular expression
			pattern = re.compile('(.*)/(\d{1,4})x(\d{1,4})/(.*)', re.IGNORECASE)
			match = pattern.match(sys.argv[2])
			scalewidth = int(match.group(2))
			scaleheight = int(match.group(3))
			
			image = Image.open(sys.argv[2])
			
			if Config['keepaspectratio'] == 'true':
				imagenew = Image.new('RGB', (scaleheight, scalewidth))
				imagewidth = image.size[0]
				imageheight = image.size[1]
				
				# landscape or portait?
				if imageheight > imagewidth:
					imagewidth = int(imagewidth * (float(scaleheight)/float(imageheight)))
					centreoffset = (scalewidth-imagewidth)/2
					image = image.resize((imagewidth, scaleheight), Image.ANTIALIAS)
					imagenew.paste(image, (centreoffset, 0))
				else:
					imageheight = int(imageheight * (float(scalewidth)/float(imagewidth)))
					centreoffset = (scaleheight-imageheight)/2
					image = image.resize((scalewidth, imageheight), Image.ANTIALIAS)
					imagenew.paste(image, (0, centreoffset))
				image = imagenew
			else:
				image = image.resize((scalewidth, scaleheight), Image.ANTIALIAS)
			
			image.save(sys.argv[2])
		except:
			return

	if Config['scaleoption'] == 'symlink':
		# make symlinks
		try:
			import os, shutil, re
			pattern = re.compile('(.*)/(\d{1,4})x(\d{1,4})/(.*)', re.IGNORECASE)
			match = pattern.match(sys.argv[2])
			cachedir = match.group(1)
			if not os.path.isdir(cachedir + '/Original'):
				os.makedirs(cachedir + '/Original')
				shutil.move(sys.argv[2], cachedir + '/Original/' + match.group(4))
				os.rmdir(cachedir + '/' + match.group(2) + 'x' + match.group(3))
			for symlink in Config['symbolicfolders']:
				if not os.path.isdir(cachedir + '/' + symlink):
					os.symlink(cachedir + '/Original', cachedir + '/' + symlink)
		except:
			return
	
class Serie:
	fileName = None
	file = None
	path = None
	name = None
	id = None
	season = None
	episode = None
	thumbnail = None
	
	def __init__(self, fileName):
		self.fileName = fileName
		self._parseFileName()
	
	def isSerie(self):
		if self.name and self.season and self.episode and self.id:
			return True
		else:
			return False
		
	def getThumbnail(self):
		if not self.thumbnail:
			self._getTVDBThumbnail()
		return self.thumbnail

	def _parseFileName(self):
		import re

		match = None<commit_msg>Added option to save image in local folder<commit_after>settings = {
	# path and name of the sqlite3 database to use
	# use only a file name to use the database in
	# the current working directory
	# Leave empty to disable the database
	'database': r'/var/twonky/TwonkyServer/tw-video-scraper.db',
	
	# Path where to download and extract the files
	# downloaded from TVDB. The script will check
	# if a file already exists, so it will not be
	# downloaded again.
	'tmpdir': r'/tmp/tw-video-scraper/',

	# List of name of the parent directories
	# Used for matching the directory name of the name
	# of a movie/serie. By knowing the parent dir,the
	# script knows if it should analyse the name
	# E.g. ~/download/my.movie.2011.avi vs. ~/download/My.Movie.2011/part1.avi
	'parentdir': ['download','series','movies','sample'],
	
	# Regular expression patterns to determine if
	# a file is a series. First match should product
	# the title, second match the season, third match
	# the episode number
	'seriepatterns': ['(.*?)S(\d{1,2})E(\d{2})(.*)',
					  '(.*?)\[?(\d{1,2})x(\d{2})\]?(.*)',
					  '(.*?)Season.?(\d{1,2}).*?Episode.?(\d{1,2})(.*)'],
	
	# TVDB api settings
	'tvdbapikey': '6262A88CCAB7E724',
	'tvdblang': 'en',
	
	# Regular expression patterns for movies
	# First try to get the year from the title
	# If that fails, try to match the whole name
	'moviepatterns': ['(.*?)\((\d{4})\)(.*)',
					  '(.*?)\[(\d{4})\](.*)',
					  '(.*?)\{(\d{4})\}(.*)',
					  '(.*?)(\d{4})(.*)',
					  '(.*?)\.(avi|mkv|mpg|mgep|mp4)',
					  '(.*)'],
	# MovieDB api settings
	'moviedbapikey': '<KEY>',

	# Files downloaded from TVDB are stored in the tmpdir,
	# and not downloaded if already present. If the file
	# is too old, it will be deleted and downloaded again
	# This is the number of days after which to delete files
	# and download it again
	'cacherenew': 6,

	# By default, images are stored in the cache folder under
	# a subdirectory for the scale. E.g is a media renderer asks
	# for a thumnail, it can request the scale, such as ?scale=100x100
	# Images from this script are stored in that folder, but are saved
	# in the original size.
	# If you have multiple media renderers, they can request thumbnails
	# in different sizes, leading to duplication of images.
	# Valid option for this setting are:
	# none : (Default option) Store images in a scale subdirectory, in the
	#     original size
	# resize : (Requires PIL Image) Store images in a scale subdirectory, resize
	#     the image to the correct scale. If 'keepaspectratio' is set to 
	#     false, the exact scale will be used, otherwise, the aspect ratio 
	#     is preserved, adding black borders
	# symlink : (Linux only) Store images in an 'Original' folder, and make
	#     symbolic links for each entry in 'symbolicfolders'
	'scaleoption': 'symlink',
	'keepaspectratio': 'true',
	'symbolicfolders': ['0x0','100x100','160x160', '1920x1080'],
	
	# Sometimes the downloaded JPEG cannot be in the correct format to display on
	# certain deviced, e.g. the XBOX360 cannot display JPEG not of time JFIF standard
	# Set this to true to this for all images (requires PIL Image)
	'fixjpeg': 'true',
	
	# Save a copy of the image file in the same folder as the movie
	# if 'savelocal' is true, a copy will be saved
	# 'savelocalfilename' controls the name of the local copy. If set to
	# Folder.jpg, the name will be Folder.jpg. If left empty, the name of the
	# local file is the same as the name of the input movie, with the extension
	# replace by .jpg. Thus, movie.2012.avi will be saved as movie.2012.jpg
	# 'savelocalwaysoverwrite' controls if a local file should be overwritten if
	# if already exists
	'savelocal': 'false',
	'savelocalfilename': '',
	'savelocalalwaysoverwrite': 'false',
	
	# If no thumbnail can be found, a thumbnail can be generated
	# using the following command. Leave empty to disable generating
	# thumbnails
	'generatecommand': r'ffmpeg -itsoffset -30 -i "$infile" -y -vcodec mjpeg -vframes 1 -an -f rawvideo -s 624x352 "$outfile"',
}

def main():
	import sys
	
	if not Config['tmpdir'].endswith('/'):
		Config['tmpdir'] = Config['tmpdir']+'/'
	
	inputfile = None
	inputdirectory = None
	outputimage = None
	
	# Validate input arguments
	if len(sys.argv) < 3:
		print("Usage: tw-video-scraper.py inputmovie outputimage")
		exit()

	if Config['scaleoption'] == 'symlink':
		# make symlinks
		try:
			import os, re
			pattern = re.compile('(.*)/(\d{1,4})x(\d{1,4})/(.*)', re.IGNORECASE)
			match = pattern.match(sys.argv[2])
			cachedir = match.group(1)
			if not os.path.isdir(cachedir + '/Original'):
				os.makedirs(cachedir + '/Original')
				os.rmdir(cachedir + '/' + match.group(2) + 'x' + match.group(3))

			for symlink in Config['symbolicfolders']:
				if not os.path.isdir(cachedir + '/' + symlink):
					os.symlink(cachedir + '/Original', cachedir + '/' + symlink)
		except:
			print("Error in making symbolic link folders")
	
	# for windows path names
	if sys.argv[1].find('\\') >= 0:
		sys.argv[1] = sys.argv[1].replace('\\','/')

	serie = Serie(sys.argv[1])
	
	if serie.isSerie():
		thumbnail = serie.getThumbnail()
	else:
		movie = Movie(sys.argv[1])
		thumbnail = movie.getThumbnail()

	if thumbnail:
		print("Downloading file "+thumbnail+"...")
		URL(thumbnail).download(sys.argv[2])
	else:
		file = sys.argv[1]
		if file.find('/') >= 0:
			file = file[file.rindex('/')+1:]
			
		print("Could not get enough information from file name '"+file+"'")
		if Config['generatecommand'] != '':
			print("Generating thumbnail...")
			try:
				import os
				Config['generatecommand'] = Config['generatecommand'].replace('$infile',sys.argv[1]).replace('$outfile',sys.argv[2])
				os.system(Config['generatecommand'])
			except:
				print("Failed to execute generate thumbnail command")
				
	if Config['fixjpeg'] == 'true':
		try:
			import Image
			image = Image.open(sys.argv[2])
			image.save(sys.argv[2])
		except:
			pass
			
	if Config['savelocal'] == 'true':
		try:
			if Config['savelocalfilename'] == '':
				# if name is empty, take filename
				file = sys.argv[1]
				if file.find('/') >= 0:
					file = file[file.rindex('/')+1:]
				if file.find('.') >= 0:
					file = file[0:file.rindex('.')] + '.jpg'
			else:
				file = Config['savelocalfilename']
			
			if file == '':
				# something went wrong with the file name, exit 'savelocal'
				pass
				
			folder = sys.argv[1]
			if folder.find('/') >= 0:
				folder = folder[0:folder.rindex('/')+1]
			else:
				folder = ''	
									
			import Image, os
			
			if not os.path.isfile(folder + file) or Config['savelocalalwaysoverwrite'] == 'true':
				image = Image.open(sys.argv[2])
				image.save(folder + file)
		except:
			pass

	if Config['scaleoption'] == 'resize':
		# resize the image
		try:
			import Image, re
			# try to get the scale option using regular expression
			pattern = re.compile('(.*)/(\d{1,4})x(\d{1,4})/(.*)', re.IGNORECASE)
			match = pattern.match(sys.argv[2])
			scalewidth = int(match.group(2))
			scaleheight = int(match.group(3))
			
			image = Image.open(sys.argv[2])
			
			if Config['keepaspectratio'] == 'true':
				imagenew = Image.new('RGB', (scaleheight, scalewidth))
				imagewidth = image.size[0]
				imageheight = image.size[1]
				
				# landscape or portait?
				if imageheight > imagewidth:
					imagewidth = int(imagewidth * (float(scaleheight)/float(imageheight)))
					centreoffset = (scalewidth-imagewidth)/2
					image = image.resize((imagewidth, scaleheight), Image.ANTIALIAS)
					imagenew.paste(image, (centreoffset, 0))
				else:
					imageheight = int(imageheight * (float(scalewidth)/float(imagewidth)))
					centreoffset = (scaleheight-imageheight)/2
					image = image.resize((scalewidth, imageheight), Image.ANTIALIAS)
					imagenew.paste(image, (0, centreoffset))
				image = imagenew
			else:
				image = image.resize((scalewidth, scaleheight), Image.ANTIALIAS)
			
			image.save(sys.argv[2])
		except:
			return

	
	
class Serie:
	fileName = None
	file = None
	path = None
	name = None
	id = None
	season = None
	episode = None
	thumbnail = None
	
	def __init__(self, fileName):
		self.fileName = fileName
		self._parseFileName()
	
	def isSerie(self):
		if self.name and self.season and self.episode and self.id:
			return True
		else:
			return False
		
	def getThumbnail(self):
		if not self.thumbnail:
			self._getTVDBThumbnail()
		return self.thumbnail

	def _parseFileName(self):
		import re

		match = None
{'fraction_non_alphanumeric': 0.14515921103519402, 'fraction_numerical': 0.015985561428387262, 'mean_word_length': 3.535223618824905, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '192197', 'n_tokens_mistral': 5940, 'n_tokens_neox': 5179, 'n_words': 1601}