Created
September 30, 2011 23:12
-
-
Save sahib/1255276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './glyros.so' | |
include Glyros | |
# Brief example on how to use glyr_db_foreach() and the require macros | |
# Sorry if this looks like Ruby. It's acutally ruby-flavoured C :-) | |
# A filled DB is required, read to use example: | |
# $ glyrc artistphotos -a "Bring me the horizon" -n 10 -c /tmp | |
# Gets the 'requirements' for a certain fetcher by it's type | |
# (i.e.) the fields to (optionally) fill in order to get data | |
def get_requirements type | |
# Again, a very C-ish thing: | |
# 'info' is a var of the type 'GlyrFetcherInfo' | |
# it basically represents a single fetcher. ('cover','lyrics',etc.) | |
# You can iterate over all fetches similar to caches: by the next/prev pointers. | |
reqs = nil | |
info = glyr_info_get | |
head = info | |
# Iterate over it and stop when we got the matching type | |
until head.nil? and not reqs.nil? | |
reqs = head.reqs if head.type == type | |
head = head.next | |
end | |
# Pass the start of the infolist to free it | |
glyr_info_free info | |
# reqs is an integer | |
reqs | |
end | |
def print_original_query query | |
# Every 'fetcher' has a requirement fields, | |
# telling you what fields need to be filled at least. | |
reqs = get_requirements query.type | |
s = "-- Query for " | |
# reqs is a bitmask, you can test if 'artist' is needed by an binary & | |
# If a certain field is needed (a & b) gives a value > 0 | |
# Since it's valid to set the album field, for e.g. retrieving lyrics, | |
# you cannot just test for nil | |
# (This looks a bit esoteric in ruby, but is a common practice in C) | |
s << " : " + query.artist if (reqs & GLYR_REQUIRES_ARTIST) != 0 | |
s << " / " + query.album if (reqs & GLYR_REQUIRES_ALBUM ) != 0 | |
s << " / " + query.title if (reqs & GLYR_REQUIRES_TITLE ) != 0 | |
# Append the string representation of the type: | |
s << " / [#{glyr_get_type_to_string query.type}]" | |
puts s | |
end | |
# Create or re-open a db at /tmp/metadata.db | |
db = glyr_db_init("/tmp") | |
# This gets called for each item in the database | |
p = Proc.new do |query,cache| | |
print_original_query query | |
glyr_cache_print(cache) | |
puts "\n////////////////////\n\n" | |
0 | |
end | |
# Now iterate over it | |
glyr_db_foreach(db,p) | |
# Free ressources | |
glyr_db_destroy(db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment