Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 49 lines (38 sloc) 2.016 kb
079acb1 Stavros Korokithakis Initial commit.
authored
1 from __future__ import with_statement
2 from sqlalchemy.ext.declarative import declarative_base
3 from sqlalchemy.orm import sessionmaker, relation, backref
4 from database import Show, Episode, init_db
5
6 import codecs
7 import re
8 import sqlalchemy
9
554ba4a Changed the import script.
Poromenos.org Server authored
10 session = init_db(transactional=True)
079acb1 Stavros Korokithakis Initial commit.
authored
11
12 def import_data(filename):
13 """Import episode names and ratings from a file."""
68ec613 Stavros Korokithakis Fix for missing episode names.
authored
14 regex = re.compile(""""(?P<show_name>.*?)"\s+\((?P<year>\d+)(?:|/.*?)\)\s+\{(?P<episode_name>.*?)\s?\(\#(?P<season_no>\d+)\.(?P<episode_no>\d+)\)\}""")
15
079acb1 Stavros Korokithakis Initial commit.
authored
16 with codecs.open(filename, "r", "latin-1") as ratings:
17 # Generate all the lines that matched.
18 matches = (match for match in (regex.search(line.strip()) for line in ratings) if match)
19 counter = 0
20 for match in matches:
21 counter += 1
22 if not counter % 100:
23 print counter
24 episode = {}
25 for field in ["show_name", "year", "episode_name", "episode_no", "season_no"]:
26 episode[field] = match.group(field)
27
68ec613 Stavros Korokithakis Fix for missing episode names.
authored
28 # If the episode has no name it is given the same name as on imdb.com for consistency.
29 if not episode["episode_name"]:
30 episode["episode_name"] = "Episode #%s.%s" % (episode["season_no"], episode["episode_no"])
31
079acb1 Stavros Korokithakis Initial commit.
authored
32 try:
33 show = session.query(Show).filter_by(name=episode["show_name"], year=episode["year"]).one()
34 except sqlalchemy.orm.exc.NoResultFound:
35 show = Show(episode["show_name"], episode["year"])
36 session.add(show)
37
38 try:
39 episode = session.query(Episode).filter_by(name=episode["episode_name"], show=show).one()
40 except sqlalchemy.orm.exc.NoResultFound:
41 episode = Episode(show, episode["episode_name"], episode["season_no"], episode["episode_no"])
42 session.add(episode)
43
554ba4a Changed the import script.
Poromenos.org Server authored
44 #session.commit()
079acb1 Stavros Korokithakis Initial commit.
authored
45
554ba4a Changed the import script.
Poromenos.org Server authored
46 if __name__ == "__main__":
47 import_data("movies.list")
079acb1 Stavros Korokithakis Initial commit.
authored
48
Something went wrong with that request. Please try again.