Framework

In writing the code for Viridian I have created two python frameworks that can be used without Viridian: AmpacheSession and DatabaseSession.

AmpacheSession

This python module allows a user to authenticate to an Ampache server, and query the server for information.

Example:

>>> from AmpacheSession import *
>>> a = AmpacheSession()
>>> a.set_credentials('user', 'password', 'http://example.com/ampache/')
True
>>> a.has_credentials()
True
>>> a.authenticate()
True
>>> print a.auth
bb7149623dd26b7afb4e0d6eb0a65de5
>>> a.get_credentials()
('user', 'password', 'http://example.com/ampache/')
>>> a.is_authenticated()
True
>>> a.get_last_update_time()
1281370810
a.get_song_info(827)
{'rating': 0.0, 'song_id': 827, 'precise_rating': 0, 'art': u'http://example.com/ampache/image.php?id=149&auth=bb7149623dd26b7afb4e0d6eb0a65de5&name=art.jpg', 'album_id': 149, 'song_time': 557, 'artist_name': u'Tiger Lou', 'song_size': 8825701, 'song_track': 10, 'url': u'http://example.com/ampache/play/index.php?ssid=bb7149623dd26b7afb4e0d6eb0a65de5&oid=827&uid=5&name=/Tiger%20Lou%20-%20A%20Partial%20Print.mp3', 'song_title': u'A Partial Print', 'artist_id': 38, 'album_name': u'A Partial Print'}

DatabaseSession

This module allows a user to create a sqlite database file (or connect to an existing one), and easily save variables to be read in later (this is how Viridian stores user-specific data). The constructor takes one argument, the path to a file, and the module will connect that file as a sqlite database file. It will also create a table called variable, which is used to store variables.

Example:

>>> from DatabaseSession import *
[Warn] sqlite3 not found -- loading sqlite2
>>> import os
>>> a = DatabaseSession(os.path.expanduser("~") + os.sep + 'example.sqlite')
>>> foo = 5
>>> bar = 'string'
>>> baz = True
>>>
>>> a.variable_set('foo', foo)
>>> a.variable_set('bar', bar)
>>> a.variable_set('baz', baz)
>>>
>>> print a.variable_get('foo')
5
>>> print a.variable_get('bar')
string
>>> print a.variable_get('baz')
True
>>>
>>> a.variable_get('foo') == foo
True
>>> a.variable_get('bar') == bar
True
>>> a.variable_get('baz') == baz
True
>>>
>>> a.variable_get('not_exists') # returns None
>>>
>>> a.variable_get('not_exists', "return me if variable doesn't exist")
"return me if variable doesn't exist"
>>> a.variable_get('foo', "return me if variable doesn't exist")
5

Documentation

The documentation for these 2 modules can be found at http://viridian.daveeddy.com/python-modules-documentation.

Download

To download the latest versions of these 2 modules, just download the newest Viridian code (check the download section of this site), and the 2 files will be in the folder called AmpacheTools.

Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]