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',
            title='Zombie',
            get_type='lyrics')

...

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
else:
   pass  # real item

Reference

class plyr.Database

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

delete()

Same as lookup, but deletes stuff instead.

See also: http://sahib.github.com/glyr/doc/html/libglyr-Cache.html#glyr-db-delete

Query :the search query
Returns:the number of deleted items
edit()

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

See also: http://sahib.github.com/glyr/doc/html/libglyr-Cache.html#glyr-db-edit

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

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.
    pass
Py_func :A callable object, like a function.
insert()

Insert a Cache manually into the database.

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

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: http://sahib.github.com/glyr/doc/html/libglyr-Cache.html#glyr-db-lookup

Query :the search query
Returns:a list of caches
make_dummy()

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()

Replace a cache from which you know the md5sum.

See also: http://sahib.github.com/glyr/doc/html/libglyr-Cache.html#glyr-db-replace

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