Script to modify files' dates

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
khsefbh
Posts: 4
Joined: 18 Dec 2016, 09:38

Script to modify files' dates

Post by khsefbh »

The reason I created it: I use Kodi on one of my computers that expects movie names to be in a specific format. This file format does not allow me to sort movies and episodes by release date. I created a workaround to use the filesystem's dates instead. So, each file will be dated for the approximate date of the movie's release.

The script is here:

Code: Select all

#!/usr/bin/env python

import sys
import os
from pathlib import Path

def redate(f):
	from datetime import datetime, timezone
	from pprint import pprint
	import xattr             # pip install pyxattr --user
	import json
	from imdbpie import Imdb # pip install imdbpie --user
	import errno
	try:
		year, month, date = 0, 0, 0
		j = json.loads(xattr.getxattr(f, 'user.net.filebot.metadata').decode('utf-8'))
		if 'airdate' in j:
			# detected a TV episode; will set the full date
			year = j['airdate']['year']
			month = j['airdate']['month']
			date = j['airdate']['day']
		elif 'imdbId' in j:
			# detected a movie; filebot does not fill in complete dates, so we get it from IMDB
			imdbId = 'tt%07d'%j['imdbId']
			t = Imdb().get_title_by_id(imdbId)
			if t.release_date:
				year, month, date = map(int, t.release_date.split('-'))
			else:
				print('Missing date information for "%s"' % f)
				return
		else:
			# filebot probably did not fill in the required info; ignore for now
			print("Insufficient data to redate %s" % f)
			# pprint(j)
			return
		dt = datetime(year, month, date, 12, tzinfo=timezone.utc).timestamp()
		# It's not straightforward to get the timezone of the release date
		# so we settle on 12 noon UTC as a compromise.
		os.utime(f, (dt, dt))
	except OSError as e:
		if e.errno in [errno.ENODATA, errno.ENOTSUP, errno.EOPNOTSUPP]:
			print('"%s": filebot xattrs missing'%f, file=sys.stderr)
		else:
			print("Unexpected error:", sys.exc_info()[0], file=sys.stderr)
			raise
	except:
		print("Unexpected error:", sys.exc_info()[0], file=sys.stderr)
		raise


if __name__ == '__main__':
	if len(sys.argv) < 2:
		print('Usage: %s <list of files>'%sys.argv[0], file=sys.stderr)
		sys.exit(1)
	for x in sys.argv[1:]:
		p = Path(x)
		if p.is_file():
			redate(p.as_posix())
Known bugs and limitations:
  • imdbpie gives us US release dates. So movies like The Little Prince (2015) may end up getting dated to 2016.
  • The script can accept a large list of files on the command line, but sometimes imdbpie queries fail when they are being run too often. Maybe because of a limit set by IMDB? I don't know. If you fail to redate a large number of files in one go, try again with a smaller list.
  • Some movies end up with a blank release_date via IMDB's API. If so, the file won't get redated.
User avatar
rednoah
The Source
Posts: 23931
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Script to modify files' dates

Post by rednoah »

Here's how I'd do it:

Code: Select all

filebot -mediainfo -r . --format "touch -t {d.format 'YYYYMMdd0000'} {quote f}" | sh -x
:idea: Please read the FAQ and How to Request Help.
khsefbh
Posts: 4
Joined: 18 Dec 2016, 09:38

Re: Script to modify files' dates

Post by khsefbh »

Oh wow! I assumed that the data present in xattrs was all there was to it.
User avatar
rednoah
The Source
Posts: 23931
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Script to modify files' dates

Post by rednoah »

It's just a simple tool, but you can combine it with other simple tools, to do really neat things, the Unix way.

Here's some more examples:
viewtopic.php?f=4&t=4788
:idea: Please read the FAQ and How to Request Help.
Post Reply