Using a Database to cache items locally

Sometimes it’s nice to have a local database that caches already downloaded items, so they don’t get downloaded over and over. This can be achieved quite easy:

# Create a database in /tmp/metadata.db
db = Database('/tmp/')

# use it in queries
qry = Query(database=db,
            artist='The Cranberries',


But what happenes if a item is not found? Nothing is written to the db, and the next time it is requeried. Not always what you want. If you get an empty return from qry.commit() you could do the following:

def insert_dummy(db, used_query):
    dummy = Database.make_dummy()
    db.insert(used_query, dummy)

On the next commit you will get this item instead of an empty return, you can check for it via:

if returned_cache.rating is -1:
   pass  # it's a dummy
   pass  # real item


class plyr.Database

Database gives you access to a local cache of downloaded (or generated) items. Under the hood a SQLite Database is used.


Same as lookup, but deletes stuff instead.

See also:

Query :the search query
Returns:the number of deleted items

Replace a cache (or even a list of caches) with a new one.

See also:

Query :The search query.
Cache :The new cache to replace the old one.
Returns:The number of replaced caches.

Iterate over all items in the database, calling a callback on each item.

The callback needs to take two arguments:

def foreach_callback(query, cache):
    # query is the original query used to search this item
    # but only with reconstructable fields filled.
    # cache is the actual item including the data.
Py_func :A callable object, like a function.

Insert a Cache manually into the database.

Query :The Query describing artist, album, title, get_type
Cache :The Cache to insert.

Lookup data from the cache.

It uses following fields from Query:

  • artist / album / title / get_type: they must be filled accordingly
  • donwload: If true search for images, else search for links
  • number: The number of items to search for.
  • from: Limit providers.

See also:

Query :the search query
Returns:a list of caches

Generate a Dummy Cache with a rating of -1.

This is useful to insert ‘empty’ items, indicating a query that led to no results. (So it does not get re-queried)

Returns:An empty Cache

Replace a cache from which you know the md5sum.

See also:

Md5sum :A 32 char long string, being a checksum in hexrepr.
Query :The search query.
Cache :The Cache to replace the item.

Table Of Contents

Previous topic

Looking up Providers

Next topic

Miscellaneous Functions

This Page