- from tvchecker.models import *
- from xml.etree import ElementTree as et
- import urllib
- import zipfile
- import os
- MIRROR = "http://www.thetvdb.com/"
- API_KEY = "4E5D5C8EFC4175A1"
- REMOVE_OLD = False
- def getIdFor(name):
- print "Retrieving info about %s" % name
- filehandle = urllib.urlopen('%sapi/GetSeries.php?seriesname=%s' % (MIRROR, name.replace('&', '')))
- tvxml = et.fromstring(filehandle.read())
- series = tvxml.find("Series")
- serName = series.findtext("SeriesName")
- print "Found %s" % serName
- if serName == name:
- sId = int(series.findtext("seriesid"))
- print 'Id = %d' % sId
- else:
- print 'Id not found'
- return None
- return sId
- def getInfoFile(ser_id):
- d = '%s/seriesZip' % os.getcwd()
- if not os.path.exists(d):
- print "Creating %s" % d
- os.makedirs(d)
- if ser_id:
- print "Retrieving zipfile for %s" % ser_id
- tvdbZip = "seriesZip/%s.zip" % ser_id
- folder = "seriesZip/%s" % ser_id
- info = "%s/en.xml" % folder
- #print "Checking %s/en.xml" % folder
- if os.path.isfile(info) and REMOVE_OLD:
- print "Removing old files"
- if os.path.isfile(tvdbZip):
- print "Removing old zipfile from %s" % tvdbZip
- os.remove(tvdbZip)
- if os.path.exists(folder):
- print "Removing contents from %s" % folder
- for the_file in os.listdir(folder):
- file_path = os.path.join(folder, the_file)
- print "Removing %s" % file_path
- try:
- if os.path.isfile(file_path):
- os.unlink(file_path)
- except Exception, e:
- print e
- else:
- os.makedirs(folder)
- if not os.path.isfile(info):
- urllib.urlretrieve('http://www.thetvdb.com/api/%s/series/%d/all/en.zip' % (API_KEY, ser_id), tvdbZip)
- if os.path.isfile(tvdbZip):
- print "Extracting files from %s" % tvdbZip
- z = zipfile.ZipFile(tvdbZip, 'r')
- z.extractall(folder)
- else:
- print "found en.xml"
- return info
- def getLanguage_id(lang):
- old = Language.query.filter_by(caption=lang).first()
- if old:
- return old
- newLang = Language(lang)
- db.session.add(newLang)
- return newLang
- def getGenre_id(genre):
- old = Genre.query.filter_by(caption=genre).first()
- if old:
- return old
- newGen = Genre(genre)
- db.session.add(newGen)
- #db.session.commit()
- return newGen
- def getGenres(genre):
- genres = []
- for gen in genre.split('|'):
- if gen:
- genres.append(getGenre_id(gen))
- return genres
- def getAllInfo(series):
- if series:
- print "Retrieving all info about %d" % series.tvdb_id
- info = getInfoFile(series.tvdb_id)
- if os.path.isfile(info):
- print "Parsing en.xml"
- infoFile = open(info, "r")
- # -----------------------
- tvxml = et.fromstring(infoFile.read())
- header = tvxml.find("Series")
- # = = =
- overview = header.findtext("Overview")
- if overview:
- print "Saving overview.."
- series.overview = overview
- # = = =
- lang = header.findtext("Language")
- if lang:
- print "Saving language.."
- series.lang_id = getLanguage_id(lang)
- # = = =
- first_air = header.findtext("FirstAired")
- if first_air:
- print "Saving first aired.."
- series.first_aired = first_air.replace('-', '')
- # = = =
- air_dow = header.findtext("Airs_DayOfWeek")
- if air_dow:
- print "Saving airs day of week.."
- series.airs_dow = air_dow[:2]
- # = = =
- air_time = header.findtext("Airs_Time")
- if air_time:
- print "Saving air time.."
- series.airs_time = air_time.replace(' ', '')
- # = = =
- genre = header.findtext("Genre")
- if genre:
- print "Saving genres.."
- series.genre_id = getGenres(genre)
- # -----------------------
- infoFile.close()
- print "Checking series for missing info"
- series = Series.query.all()
- for s in series:
- print "Checking %s" % s.name
- if not s.tvdb_id:
- print "---------------------------------------"
- print "Missing id for - %s" % s.name
- newId = getIdFor(s.name)
- if newId:
- s.tvdb_id = newId
- db.session.commit()
- if (s.genre_id is None) and s.tvdb_id:
- print "---------------------------------------"
- print "Missing overview - %s" % s.name
- getAllInfo(s)
- print "Committing..."
- db.session.commit()
- -----------------------------------------------------------------------
- ERRORS:
- Missing overview - Dexter
- Retrieving all info about 79349
- Retrieving zipfile for 79349
- found en.xml
- Parsing en.xml
- Saving overview..
- Saving language..
- Saving first aired..
- Saving airs day of week..
- Saving air time..
- Saving genres..
- Committing...
- Traceback (most recent call last):
- File "checker.py", line 147, in <module>
- db.session.commit()
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/scoping.py", line 114, in do
- return getattr(self.registry(), name)(*args, **kwargs)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 655, in commit
- self.transaction.commit()
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 313, in commit
- self._prepare_impl()
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 297, in _prepare_impl
- self.session.flush()
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1587, in flush
- self._flush(objects)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1658, in _flush
- flush_context.execute()
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 331, in execute
- rec.execute(self)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 475, in execute
- uow
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 59, in save_obj
- mapper, table, update)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 485, in _emit_update_statements
- execute(statement, params)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1450, in execute
- params)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1583, in _execute_clauseelement
- compiled_sql, distilled_params
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1697, in _execute_context
- context)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1690, in _execute_context
- context)
- File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 335, in do_execute
- cursor.execute(statement, parameters)
- File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in execute
- self.errorhandler(self, exc, value)
- File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 35, in defaulterrorhandler
- raise errorclass, errorvalue
- sqlalchemy.exc.OperationalError
- furby@furby-VirtualBox:~/Flask/tvchecker-project$