Commit 8ba4613e authored by Gerion Entrup's avatar Gerion Entrup
Browse files

connect model to sqlite, insert recording

parent 2d4ebda9
......@@ -4,13 +4,15 @@ import musicbrainzngs
from sqlalchemy.orm import Session
from mbdata.models import Recording, ArtistCredit
class Collector:
Fetches the musicdata and add them to the DB.
def __init__(self, session):
self.session = session
def __init__(self, Session):
self.session = Session()
musicbrainzngs.set_useragent("fubrainz", "0.1-alpha", "")
def walk(self, path):
......@@ -33,13 +35,20 @@ class Collector:
def fetch_metadata(self, mbid):
result = musicbrainzngs.get_recording_by_id(mbid)
except musicbrainzngs.WebServiceError as exc:
print("Could not connect to Musicbrainz. Request: {}".format(exc))
recording = self.session.query(Recording).filter_by(gid=mbid).first()
if recording is None:
result = musicbrainzngs.get_recording_by_id(mbid, includes=['releases', 'artists'])['recording']
except musicbrainzngs.WebServiceError as exc:
print("Could not connect to Musicbrainz. Request: {}".format(exc))
recording = self.map_result_to_recording(result)
testrec = self.session.query(Recording).filter_by(gid=mbid).first()
def get_mbid(self, mut):
for key in mut:
......@@ -60,3 +69,17 @@ class Collector:
print("should not happen")
return None
def map_result_to_recording(self, result):
artistcredit = ArtistCredit() = result['artist-credit-phrase']
artistcredit.artist_count = len(result['artist-credit'])
recording = Recording()
recording.gid = result['id'] = result['title']
recording.length = result['length']
recording.artist_credit = artistcredit
return recording
......@@ -2,17 +2,26 @@
import argparse
import sys
import os.path
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy.orm import sessionmaker
from collector import Collector
from mbdata.models import Base
from mbdata import patch_model_schemas, NO_SCHEMAS
class Main:
def init_database(self, dbfile):
engine = create_engine('sqlite://')
self.session = Session(bind=engine)
#engine = create_engine('sqlite:///' + dbfile, echo=True)
engine = create_engine('sqlite://')#, echo=True)
#if not os.path.isfile(dbfile):
# Base.metadata.create_all(engine)
self.Session = sessionmaker(bind=engine)
def main(self, args):
......@@ -28,7 +37,7 @@ class Main:
collector = Collector(self.session)
collector = Collector(self.Session)
if __name__ == "__main__":
# Copyright (C) 2013 Lukas Lalinsky
# Distributed under the MIT license, see the LICENSE file for details.
from sqlalchemy.orm import class_mapper, defer
'musicbrainz': None,
'cover_art_archive': None,
'wikidocs': None,
'statistics': None,
'documentation': None,
'musicbrainz': 'musicbrainz',
'cover_art_archive': 'musicbrainz',
'wikidocs': 'musicbrainz',
'statistics': 'musicbrainz',
'documentation': 'musicbrainz',
def patch_model_schemas(mapping):
"""Update mbdata.models to use different schema names
The function accepts a dictionary with schema name mapping
and updates the schema for all MusicBrainz tables.
If you want to use the default schema:
>>> patch_model_schemas(NO_SCHEMAS)
If you have just one 'musicbrainz' schema:
>>> patch_model_schemas(SINGLE_MUSICBRAINZ_SCHEMA)
from mbdata.models import Base
for table in Base.metadata.sorted_tables:
if table.schema is None:
table.schema = mapping.get(table.schema, table.schema)
def defer_everything_but(entity, *cols):
m = class_mapper(entity)
return [defer(k) for k in
set(p.key for p in m.iterate_properties
if hasattr(p, 'columns')).difference(cols)]
def get_something_by_gid(query, redirect_model, gid):
artist = query.filter_by(gid=gid).first()
if artist is None:
subquery = query.session.query(redirect_model.redirect_id).\
artist = query.filter(
return artist
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment