def main(script, opts, args):
    conn = script.engine.connect()
    min_id, max_id = conn.execute("SELECT min(id), max(id) FROM fingerprint").fetchone()
    print min_id, max_id
    while True:
        id = random.randint(min_id, max_id)
        row = conn.execute("SELECT fingerprint, length FROM fingerprint WHERE id = %s", (id,)).fetchone()
        if row is None:
            continue
        fingerprint, length = row
        for i in range(1):
            t0 = time.time()
            for i in range(len(fingerprint)):
                fingerprint[i] ^= random.getrandbits(2) << random.randint(0, 20)
            searcher = FingerprintSearcher(script.engine, script.index)
            matches = searcher.search(fingerprint, length + random.randint(-8, 8))
            track_ids = [r[1] for r in matches]
            track_mbid_map = lookup_mbids(conn, track_ids)
            mbids = set()
            for track_id, track_mbids in track_mbid_map.iteritems():
                for mbid, sources in track_mbids:
                    mbids.add(mbid)
            metadata = lookup_metadata(conn, mbids, load_releases=True, load_release_groups=True)
            print "Searching for ID", id, len(matches), time.time() - t0
        time.sleep(1)
Example #2
0
def main(script, opts, args):
    conn = script.engine.connect()
    min_id, max_id = conn.execute(
        "SELECT min(id), max(id) FROM fingerprint").fetchone()
    print min_id, max_id
    while True:
        id = random.randint(min_id, max_id)
        row = conn.execute(
            "SELECT fingerprint, length FROM fingerprint WHERE id = %s",
            (id, )).fetchone()
        if row is None:
            continue
        fingerprint, length = row
        for i in range(1):
            t0 = time.time()
            for i in range(len(fingerprint)):
                fingerprint[i] ^= random.getrandbits(2) << random.randint(
                    0, 20)
            searcher = FingerprintSearcher(script.engine, script.index)
            matches = searcher.search(fingerprint,
                                      length + random.randint(-8, 8))
            track_ids = [r[1] for r in matches]
            track_mbid_map = lookup_mbids(conn, track_ids)
            mbids = set()
            for track_id, track_mbids in track_mbid_map.iteritems():
                for mbid, sources in track_mbids:
                    mbids.add(mbid)
            metadata = lookup_metadata(conn,
                                       mbids,
                                       load_releases=True,
                                       load_release_groups=True)
            print "Searching for ID", id, len(matches), time.time() - t0
        time.sleep(1)
Example #3
0
 def inject_m2(self, meta):
     el_recording = self._inject_recording_ids_internal(True)[0]
     metadata = lookup_metadata(self.conn, el_recording.keys(), load_releases=True)
     last_recording_id = None
     for item in metadata:
         if last_recording_id != item['recording_id']:
             recording = self.extract_recording(item, True)
             last_recording_id = recording['id']
             for el in el_recording[recording['id']]:
                 if item['recording_duration']:
                     recording['duration'] = item['recording_duration']
                 recording['tracks'] = []
                 el.update(recording)
     metadata = sorted(metadata, key=operator.itemgetter('recording_id', 'release_id', 'medium_position', 'track_position'))
     for item in metadata:
         for el in el_recording[item['recording_id']]:
             medium = {
                 'track_count': item['medium_track_count'],
                 'position': item['medium_position'],
                 'release': {
                     'id': item['release_id'],
                     'title': item['release_title'],
                 },
             }
             if item['medium_format']:
                 medium['format'] = item['medium_format']
             el['tracks'].append({
                 'title': item['track_title'],
                 'duration': item['track_duration'],
                 'artists': item['track_artists'],
                 'position': item['track_position'],
                 'medium': medium,
             })
Example #4
0
 def inject_m2(self, meta):
     el_recording = self._inject_recording_ids_internal(True)[0]
     metadata = lookup_metadata(self.conn, el_recording.keys(), load_releases=True)
     last_recording_id = None
     for item in metadata:
         if last_recording_id != item["recording_id"]:
             recording = self.extract_recording(item, True)
             last_recording_id = recording["id"]
             for el in el_recording[recording["id"]]:
                 if item["recording_duration"]:
                     recording["duration"] = item["recording_duration"]
                 recording["tracks"] = []
                 el.update(recording)
     metadata = sorted(
         metadata, key=operator.itemgetter("recording_id", "release_id", "medium_position", "track_position")
     )
     for item in metadata:
         for el in el_recording[item["recording_id"]]:
             medium = {
                 "track_count": item["medium_track_count"],
                 "position": item["medium_position"],
                 "release": {"id": item["release_id"], "title": item["release_title"]},
             }
             if item["medium_format"]:
                 medium["format"] = item["medium_format"]
             el["tracks"].append(
                 {
                     "title": item["track_title"],
                     "duration": item["track_duration"],
                     "artists": item["track_artists"],
                     "position": item["track_position"],
                     "medium": medium,
                 }
             )
Example #5
0
 def inject_release_groups(self, meta):
     recording_els, track_mbid_map = self._inject_recording_ids_internal(False)
     metadata = lookup_metadata(self.conn, recording_els.keys(), load_releases=True, load_release_groups=True)
     for track_id, track_metadata in self._group_metadata(metadata, track_mbid_map):
         result = {}
         self._inject_release_groups_internal(meta, result, track_metadata)
         for result_el in self.el_result[track_id]:
             result_el.update(result)
Example #6
0
 def inject_release_groups(self, meta):
     recording_els, track_mbid_map = self._inject_recording_ids_internal(
         False)
     metadata = lookup_metadata(self.conn,
                                recording_els.keys(),
                                load_releases=True,
                                load_release_groups=True)
     for track_id, track_metadata in self._group_metadata(
             metadata, track_mbid_map):
         result = {}
         self._inject_release_groups_internal(meta, result, track_metadata)
         for result_el in self.el_result[track_id]:
             result_el.update(result)
Example #7
0
 def inject_release_groups(self, meta):
     # type: (List[str]) -> None
     recording_els, track_mbid_map = self._inject_recording_ids_internal(
         False)
     metadata = lookup_metadata(self.ctx.db.get_musicbrainz_db(),
                                recording_els.keys(),
                                load_releases=True,
                                load_release_groups=True)
     for track_id, track_metadata in self._group_metadata(
             metadata, track_mbid_map):
         result = {}  # type: Dict[str, Any]
         self._inject_release_groups_internal(meta, result, track_metadata)
         for result_el in self.el_result[track_id]:
             result_el.update(result)
Example #8
0
 def _inject_metadata(self, meta, result_map):
     track_mbid_map = lookup_mbids(self.conn, result_map.keys())
     if meta > 1:
         all_mbids = []
         for track_id, mbids in track_mbid_map.iteritems():
             all_mbids.extend(mbids)
         track_meta_map = lookup_metadata(self.conn, all_mbids)
     for track_id, mbids in track_mbid_map.iteritems():
         result = result_map[track_id]
         result[self.recordings_name] = tracks = []
         for mbid in mbids:
             recording = {}
             tracks.append(recording)
             recording['id'] = str(mbid)
             if meta == 1:
                 continue
             track_meta = track_meta_map.get(mbid)
             if track_meta is None:
                 continue
             recording['tracks'] = [{
                 'title': track_meta['name'],
                 'duration': track_meta['length'],
                 'artist': {
                     'id': track_meta['artist_id'],
                     'name': track_meta['artist_name'],
                 },
                 'position': track_meta['track_num'],
                 'medium': {
                     'track_count': track_meta['total_tracks'],
                     # position
                     # title
                     # format
                     'release': {
                         'id': track_meta['release_id'],
                         'title': track_meta['release_name'],
                         # medium_count
                     },
                 },
             }]
Example #9
0
 def inject_m2(self, meta):
     el_recording = self._inject_recording_ids_internal(True)[0]
     metadata = lookup_metadata(
         self.ctx.db.get_musicbrainz_db(read_only=True),
         el_recording.keys(),
         load_releases=True)
     last_recording_id = None
     for item in metadata:
         if last_recording_id != item['recording_id']:
             recording = self.extract_recording(item, True)
             last_recording_id = recording['id']
             for el in el_recording[recording['id']]:
                 if item['recording_duration']:
                     recording['duration'] = item['recording_duration']
                 recording['tracks'] = []
                 el.update(recording)
     metadata = sorted(metadata,
                       key=operator.itemgetter('recording_id', 'release_id',
                                               'medium_position',
                                               'track_position'))
     for item in metadata:
         for el in el_recording[item['recording_id']]:
             medium = {
                 'track_count': item['medium_track_count'],
                 'position': item['medium_position'],
                 'release': {
                     'id': item['release_id'],
                     'title': item['release_title'],
                 },
             }
             if item['medium_format']:
                 medium['format'] = item['medium_format']
             el['tracks'].append({
                 'title': item['track_title'],
                 'duration': item['track_duration'],
                 'artists': item['track_artists'],
                 'position': item['track_position'],
                 'medium': medium,
             })
Example #10
0
 def inject_recordings(self, meta):
     # type: (List[str]) -> None
     recording_els = self._inject_recording_ids_internal(
         True, 'sources' in meta)[0]
     load_releases = False
     load_release_groups = False
     if 'releaseids' in meta or 'releases' in meta:
         load_releases = True
     if 'releasegroupids' in meta or 'releasegroups' in meta:
         load_releases = True
         load_release_groups = True
     metadata = lookup_metadata(self.ctx.db.get_musicbrainz_db(),
                                recording_els.keys(),
                                load_releases=load_releases,
                                load_release_groups=load_release_groups)
     if 'usermeta' in meta and not metadata:
         user_meta_els = self._inject_user_meta_ids_internal(True)[0]
         recording_els.update(user_meta_els)  # type: ignore
         user_meta = lookup_meta(self.ctx.db.get_fingerprint_db(),
                                 user_meta_els.keys())
         metadata.extend(user_meta)
     for recording, recording_metadata in self._group_recordings(
             metadata, 'recordingids' in meta):
         if 'releasegroups' in meta or 'releasegroupids' in meta:
             self._inject_release_groups_internal(meta, recording,
                                                  recording_metadata)
             if 'compress' in meta:
                 for release_group in recording.get('releasegroups', []):
                     for release in release_group.get('releases', []):
                         for medium in release.get('mediums', []):
                             for track in medium['tracks']:
                                 if 'title' in track and track[
                                         'title'] == recording.get('title'):
                                     del track['title']
                 if 'artists' in release_group and release_group[
                         'artists'] == recording.get('artists'):
                     del release_group['artists']
         elif 'releases' in meta or 'releaseids' in meta:
             self._inject_releases_internal(meta, recording,
                                            recording_metadata)
             if 'compress' in meta:
                 for release in recording.get('releases', []):
                     for medium in release.get('mediums', []):
                         for track in medium['tracks']:
                             if 'title' in track and track[
                                     'title'] == recording.get('title'):
                                 del track['title']
                     if 'artists' in release and release[
                             'artists'] == recording.get('artists'):
                         del release['artists']
         for recording_el in recording_els[recording['id']]:
             if 'usermeta' in meta:
                 if isinstance(recording['id'], int):
                     del recording['id']
                     if 'title' in recording and not recording['title']:
                         del recording['title']
                     if 'releasegroups' in recording:
                         releasegroups = []
                         for releasegroup in recording['releasegroups']:
                             del releasegroup['id']
                             if 'title' in releasegroup and not releasegroup[
                                     'title']:
                                 del releasegroup['title']
                             if 'releases' in releasegroup:
                                 releases = []
                                 for release in releasegroup['releases']:
                                     del release['id']
                                     if 'title' in release and not release[
                                             'title']:
                                         del release['title']
                                     if release:
                                         releases.append(release)
                                 if releases:
                                     releasegroup['releases'] = releases
                                 else:
                                     del releasegroup['releases']
                             if releasegroup:
                                 releasegroups.append(releasegroup)
                         if releasegroups:
                             recording['releasegroups'] = releasegroups
                         else:
                             del recording['releasegroups']
                     if 'releases' in recording:
                         releases = []
                         for release in recording['releases']:
                             del release['id']
                             if 'title' in release and not release['title']:
                                 del release['title']
                             if release:
                                 releases.append(release)
                         if releases:
                             recording['releases'] = releases
                         else:
                             del recording['releases']
             recording_el.update(recording)
Example #11
0
 def inject_recordings(self, meta):
     recording_els = self._inject_recording_ids_internal(True)[0]
     load_releases = False
     load_release_groups = False
     if 'releaseids' in meta or 'releases' in meta:
         load_releases = True
     if 'releasegroupids' in meta or 'releasegroups' in meta:
         load_releases = True
         load_release_groups = True
     metadata = lookup_metadata(self.conn, recording_els.keys(), load_releases=load_releases, load_release_groups=load_release_groups)
     if 'usermeta' in meta and not metadata:
         user_meta_els = self._inject_user_meta_ids_internal(True)[0]
         recording_els.update(user_meta_els)
         user_meta = lookup_meta(self.conn, user_meta_els.keys())
         metadata.extend(user_meta)
     for recording, recording_metadata in self._group_recordings(metadata, 'recordingids' in meta):
         if 'releasegroups' in meta or 'releasegroupids' in meta:
             self._inject_release_groups_internal(meta, recording, recording_metadata)
             if 'compress' in meta:
                 for release_group in recording.get('releasegroups', []):
                     for release in release_group.get('releases', []):
                         for medium in release.get('mediums', []):
                             for track in medium['tracks']:
                                 if 'title' in track and track['title'] == recording.get('title'):
                                     del track['title']
                 if 'artists' in release_group and release_group['artists'] == recording.get('artists'):
                     del release_group['artists']
         elif 'releases' in meta or 'releaseids' in meta:
             self._inject_releases_internal(meta, recording, recording_metadata)
             if 'compress' in meta:
                 for release in recording.get('releases', []):
                     for medium in release.get('mediums', []):
                         for track in medium['tracks']:
                             if 'title' in track and track['title'] == recording.get('title'):
                                 del track['title']
                     if 'artists' in release and release['artists'] == recording.get('artists'):
                         del release['artists']
         for recording_el in recording_els[recording['id']]:
             if 'usermeta' in meta:
                 if isinstance(recording['id'], int):
                     del recording['id']
                     if 'title' in recording and not recording['title']:
                         del recording['title']
                     if 'releasegroups' in recording:
                         releasegroups = []
                         for releasegroup in recording['releasegroups']:
                             del releasegroup['id']
                             if 'title' in releasegroup and not releasegroup['title']:
                                 del releasegroup['title']
                             if 'releases' in releasegroup:
                                 releases = []
                                 for release in releasegroup['releases']:
                                     del release['id']
                                     if 'title' in release and not release['title']:
                                         del release['title']
                                     if release:
                                         releases.append(release)
                                 if releases:
                                     releasegroup['releases'] = releases
                                 else:
                                     del releasegroup['releases']
                             if releasegroup:
                                 releasegroups.append(releasegroup)
                         if releasegroups:
                             recording['releasegroups'] = releasegroups
                         else:
                             del recording['releasegroups']
                     if 'releases' in recording:
                         releases = []
                         for release in recording['releases']:
                             del release['id']
                             if 'title' in release and not release['title']:
                                 del release['title']
                             if release:
                                 releases.append(release)
                         if releases:
                             recording['releases'] = releases
                         else:
                             del recording['releases']
             recording_el.update(recording)
Example #12
0
 def inject_recordings(self, meta):
     recording_els = self._inject_recording_ids_internal(True)[0]
     load_releases = False
     load_release_groups = False
     if "releaseids" in meta or "releases" in meta:
         load_releases = True
     if "releasegroupids" in meta or "releasegroups" in meta:
         load_releases = True
         load_release_groups = True
     metadata = lookup_metadata(
         self.conn, recording_els.keys(), load_releases=load_releases, load_release_groups=load_release_groups
     )
     if "usermeta" in meta and not metadata:
         user_meta_els = self._inject_user_meta_ids_internal(True)[0]
         recording_els.update(user_meta_els)
         user_meta = lookup_meta(self.conn, user_meta_els.keys())
         metadata.extend(user_meta)
     for recording, recording_metadata in self._group_recordings(metadata, "recordingids" in meta):
         if "releasegroups" in meta or "releasegroupids" in meta:
             self._inject_release_groups_internal(meta, recording, recording_metadata)
             if "compress" in meta:
                 for release_group in recording.get("releasegroups", []):
                     for release in release_group.get("releases", []):
                         for medium in release.get("mediums", []):
                             for track in medium["tracks"]:
                                 if "title" in track and track["title"] == recording.get("title"):
                                     del track["title"]
                 if "artists" in release_group and release_group["artists"] == recording.get("artists"):
                     del release_group["artists"]
         elif "releases" in meta or "releaseids" in meta:
             self._inject_releases_internal(meta, recording, recording_metadata)
             if "compress" in meta:
                 for release in recording.get("releases", []):
                     for medium in release.get("mediums", []):
                         for track in medium["tracks"]:
                             if "title" in track and track["title"] == recording.get("title"):
                                 del track["title"]
                     if "artists" in release and release["artists"] == recording.get("artists"):
                         del release["artists"]
         for recording_el in recording_els[recording["id"]]:
             if "usermeta" in meta:
                 if isinstance(recording["id"], int):
                     del recording["id"]
                     if "title" in recording and not recording["title"]:
                         del recording["title"]
                     if "releasegroups" in recording:
                         releasegroups = []
                         for releasegroup in recording["releasegroups"]:
                             del releasegroup["id"]
                             if "title" in releasegroup and not releasegroup["title"]:
                                 del releasegroup["title"]
                             if "releases" in releasegroup:
                                 releases = []
                                 for release in releasegroup["releases"]:
                                     del release["id"]
                                     if "title" in release and not release["title"]:
                                         del release["title"]
                                     if release:
                                         releases.append(release)
                                 if releases:
                                     releasegroup["releases"] = releases
                                 else:
                                     del releasegroup["releases"]
                             if releasegroup:
                                 releasegroups.append(releasegroup)
                         if releasegroups:
                             recording["releasegroups"] = releasegroups
                         else:
                             del recording["releasegroups"]
                     if "releases" in recording:
                         releases = []
                         for release in recording["releases"]:
                             del release["id"]
                             if "title" in release and not release["title"]:
                                 del release["title"]
                             if release:
                                 releases.append(release)
                         if releases:
                             recording["releases"] = releases
                         else:
                             del recording["releases"]
             recording_el.update(recording)