From a24122bd8fe6119d2a3992d1541e6e51cc45b14d Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 8 Aug 2013 10:47:30 -0300 Subject: [PATCH] convert our yaml db to a proper sqlite3 db --- db/db.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 db/db.py diff --git a/db/db.py b/db/db.py new file mode 100644 index 0000000..43e99e0 --- /dev/null +++ b/db/db.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import yaml +import sys +import sqlite3 +import os +import os.path +import datetime + +result = None +with open('../lyrics.yaml') as content: + result = yaml.load(content) + +if not result: + print 'Error opening file.' + sys.exit(1) + +if os.path.exists('lyrics.sq3'): + print 'Removing old database...' + os.remove('lyrics.sq3') + +print 'Connecting...' +connection = sqlite3.connect('lyrics.sq3') + +print 'Creating tables...' +cursor = connection.cursor() + +print '\tArtists...' +cursor.execute('''CREATE TABLE artists ( + id INTEGER PRIMARY KEY, + name TEXT CONSTRAINT artist_name UNIQUE +)''') + +print '\tSongs...' +cursor.execute('''CREATE TABLE songs ( + id INTEGER PRIMARY KEY, + title TEXT, + artist INTEGER CONSTRAINT aritst_song REFERENCES artists (id), + done_in TEXT +)''') + +print '\tLyrics...' +cursor.execute('''CREATE TABLE lyrics ( + id INTEGER PRIMARY KEY, + song INTEGER CONSTRAINT song_lyrics REFERENCES songs (id), + lyrics TEXT NOT NULL +)''') + +print '\tTips...' +cursor.execute('''CREATE TABLE tips ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL, + value TEXT NOT NULL +)''') + +print 'Converting songs...' +today = datetime.date.today() + +for song in result: + done = song['done'] + lyrics = song['lyrics'] + + # fucking "Yes" + artist = song['artist'] + if isinstance(artist, basestring): + artist = song['artist'].lower() + else: + artist = 'yes' + + song = song['song'].lower() + + print '\t', artist, '-', song + + cursor.execute('SELECT id FROM artists WHERE name=?', + (artist, )) + artist_row = cursor.fetchone() + if not artist_row: + print '\t\tNot in db, adding...' + cursor.execute('INSERT INTO artists (id, name) VALUES (NULL, ?)', + (artist, )) + artist_id = cursor.lastrowid + else: + print '\t\tAlready in db with id', artist_row[0] + artist_id = artist_row[0] + + cursor.execute('INSERT INTO songs (id, title, artist, done_in) VALUES ' + '(NULL, ?, ?, ?)', + (song, artist_id, today if done else None)) + song = cursor.lastrowid + + if lyrics: + # some songs do not have lyrics, as they were in the db just to mark + # them as "done" + cursor.execute('INSERT INTO lyrics (id, song, lyrics) VALUES (NULL, ?, ?)', + (song, lyrics)) + connection.commit() + +connection.close()