Skip to content

Instantly share code, notes, and snippets.

@monstermunchkin
Created August 5, 2011 20:47
Show Gist options
  • Save monstermunchkin/1128488 to your computer and use it in GitHub Desktop.
Save monstermunchkin/1128488 to your computer and use it in GitHub Desktop.
Create .nfo file for movie sets in XBMC
#!/usr/bin/python3
# Copyright 2011 Thomas Hipp
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sqlite3
import sys
def parse_file(filename):
set_ = None
with open(filename, 'r') as f:
for line in f:
line = line.strip()
# ignore empty lines
if len(line) == 0:
continue
# new set
if line.startswith('['):
set_ = line[1:-1]
episode = 1
print('new set: {0}'.format(set_))
continue
# add movie to set
try:
create_nfo(movie=line, set_=set_, episode=episode)
except Exception as e:
raise e
else:
print(' -> {0}'.format(line))
episode = episode + 1
def create_nfo(movie, set_, episode):
# raise exception if no set was provided
if set_ == None:
raise Exception('No set provided')
# fetch movie information from db
cur.execute(
'''SELECT m.c09, s.strPath, s.strFilename
FROM movie AS m
JOIN (
SELECT *
FROM files AS f
JOIN path AS p
ON f.idPath = p.idPath) AS s
ON m.idFile = s.idFile
WHERE m.c00 = "{0}"'''.format(movie))
rec = cur.fetchone()
if rec == None:
raise Exception('Movie not found: {0}'.format(movie))
# write nfo file
with open('{0}{1}.nfo'.format(rec[1], rec[2].rsplit('.', 1)[0]), 'w') as f:
f.write(
'''<movie>
<title>{0}</title>
<set>{1}</set>
<sorttitle>{1} {2}</sorttitle>
</movie>
http://www.imdb.com/title/{3}/'''.format(movie, set_, episode, rec[0]))
if __name__ == '__main__':
if len(sys.argv) != 3:
print('usage: {0} <filename> <database>'.format(sys.argv[0]))
sys.exit(1)
global conn
conn = sqlite3.connect(sys.argv[2])
global cur
cur = conn.cursor()
try:
parse_file(sys.argv[1])
except Exception as e:
print(e)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment