2010-11-05 15:54:15 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# author Jonas Ohrstrom <jonas@digris.ch>
|
|
|
|
|
|
|
|
"""
|
|
|
|
Python part of radio playout (pypo)
|
|
|
|
|
|
|
|
This function acts as a gateway between liquidsoap and the obp-api.
|
|
|
|
Mainliy used to tell the plattform what pypo/LS does.
|
|
|
|
|
|
|
|
Main case:
|
|
|
|
- whenever LS starts playing a new track, its on_metadata callback calls
|
|
|
|
a function in ls (notify(m)) which then calls the pythin script here
|
|
|
|
with the currently starting filename as parameter
|
|
|
|
- this python script takes this parameter, tries to extract the actual
|
|
|
|
media id from it, and then calls back to obp via api to tell about
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# python defaults (debian default)
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
from optparse import *
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import logging.config
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import string
|
|
|
|
|
|
|
|
# additional modules (should be checked)
|
|
|
|
from configobj import ConfigObj
|
|
|
|
|
|
|
|
# custom imports
|
|
|
|
from util import *
|
|
|
|
from obp import *
|
|
|
|
from dls import *
|
|
|
|
|
|
|
|
PYPO_VERSION = '0.9'
|
|
|
|
|
|
|
|
|
2010-11-08 22:54:54 +01:00
|
|
|
# Set up command-line options
|
2010-11-05 15:54:15 +01:00
|
|
|
parser = OptionParser()
|
|
|
|
|
|
|
|
# help screeen / info
|
|
|
|
usage = "%prog [options]" + " - notification gateway"
|
|
|
|
parser = OptionParser(usage=usage)
|
|
|
|
|
2010-11-08 22:54:54 +01:00
|
|
|
# Options
|
2010-11-05 15:54:15 +01:00
|
|
|
#parser.add_option("-p", "--playing", help="Tell daddy what is playing right now", dest="playing", default=False, metavar=False)
|
|
|
|
parser.add_option("-p", "--playing", help="Tell daddy what is playing right now", default=False, action="store_true", dest="playing")
|
|
|
|
parser.add_option("-t", "--playlist-type", help="Tell daddy what is playing right now", metavar="playlist_type")
|
|
|
|
parser.add_option("-M", "--media-id", help="Tell daddy what is playing right now", metavar="media_id")
|
|
|
|
parser.add_option("-U", "--user-id", help="Tell daddy what is playing right now", metavar="user_id")
|
|
|
|
parser.add_option("-P", "--playlist-id", help="Tell daddy what is playing right now", metavar="playlist_id")
|
|
|
|
parser.add_option("-T", "--transmission-id", help="Tell daddy what is playing right now", metavar="transmission_id")
|
|
|
|
parser.add_option("-E", "--export-source", help="Tell daddy what is playing right now", metavar="export_source")
|
|
|
|
|
|
|
|
# parse options
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
# configure logging
|
|
|
|
logging.config.fileConfig("logging.cfg")
|
|
|
|
|
|
|
|
# loading config file
|
|
|
|
try:
|
|
|
|
config = ConfigObj('config.cfg')
|
|
|
|
TMP_DIR = config['tmp_dir']
|
|
|
|
BASE_URL = config['base_url']
|
2010-11-08 22:54:54 +01:00
|
|
|
API_BASE = BASE_URL + 'mod/medialibrary/'
|
|
|
|
API_KEY = config['api_key']
|
2010-11-05 15:54:15 +01:00
|
|
|
|
|
|
|
except Exception, e:
|
|
|
|
print 'error: ', e
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
class Global:
|
|
|
|
def __init__(self):
|
|
|
|
print
|
|
|
|
|
|
|
|
def selfcheck(self):
|
|
|
|
|
2010-11-08 22:54:54 +01:00
|
|
|
self.api_auth = urllib.urlencode({'api_key': API_KEY})
|
|
|
|
self.api_client = api_client.api_client_factory(config)
|
|
|
|
self.api_client.check_version()
|
2010-11-05 15:54:15 +01:00
|
|
|
|
|
|
|
class Notify:
|
|
|
|
def __init__(self):
|
2010-11-08 22:54:54 +01:00
|
|
|
self.tmp_dir = TMP_DIR
|
|
|
|
self.api_auth = urllib.urlencode({'api_key': API_KEY})
|
|
|
|
self.api_client = api_client.api_client_factory(config)
|
2010-11-05 15:54:15 +01:00
|
|
|
self.dls_client = DlsClient('127.0.0.128', 50008, 'myusername', 'mypass')
|
|
|
|
|
|
|
|
|
|
|
|
def start_playing(self, options):
|
|
|
|
logger = logging.getLogger("start_playing")
|
|
|
|
tnow = time.localtime(time.time())
|
|
|
|
|
|
|
|
#print options
|
|
|
|
|
|
|
|
print '#################################################'
|
|
|
|
print '# calling obp to tell about what\'s playing #'
|
|
|
|
print '#################################################'
|
|
|
|
|
|
|
|
if int(options.playlist_type) < 5:
|
|
|
|
print 'seems to be a playlist'
|
|
|
|
|
|
|
|
try:
|
|
|
|
media_id = int(options.media_id)
|
|
|
|
except Exception, e:
|
|
|
|
media_id = 0
|
|
|
|
|
|
|
|
response = self.api_client.update_start_playing(options.playlist_type, options.export_source, media_id, options.playlist_id, options.transmission_id)
|
|
|
|
|
|
|
|
print response
|
|
|
|
|
|
|
|
if int(options.playlist_type) == 6:
|
|
|
|
print 'seems to be a couchcast'
|
|
|
|
|
|
|
|
try:
|
|
|
|
media_id = int(options.media_id)
|
|
|
|
except Exception, e:
|
|
|
|
media_id = 0
|
|
|
|
|
|
|
|
response = self.api_client.update_start_playing(options.playlist_type, options.export_source, media_id, options.playlist_id, options.transmission_id)
|
|
|
|
|
|
|
|
print response
|
|
|
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def start_playing_legacy(self, options):
|
|
|
|
logger = logging.getLogger("start_playing")
|
|
|
|
tnow = time.localtime(time.time())
|
|
|
|
|
|
|
|
print '#################################################'
|
|
|
|
print '# calling obp to tell about what\'s playing #'
|
|
|
|
print '#################################################'
|
|
|
|
|
|
|
|
path = options
|
|
|
|
|
|
|
|
print
|
|
|
|
print path
|
|
|
|
print
|
|
|
|
|
|
|
|
if 'pl_id' in path:
|
|
|
|
print 'seems to be a playlist'
|
|
|
|
type = 'playlist'
|
|
|
|
id = path[5:]
|
|
|
|
|
|
|
|
elif 'text' in path:
|
|
|
|
print 'seems to be a playlist'
|
|
|
|
type = 'text'
|
|
|
|
id = path[4:]
|
|
|
|
print id
|
|
|
|
|
|
|
|
else:
|
|
|
|
print 'seems to be a single track (media)'
|
|
|
|
type = 'media'
|
|
|
|
try:
|
|
|
|
file = path.split("/")[-1:][0]
|
|
|
|
if file.find('_cue_') > 0:
|
|
|
|
id = file.split("_cue_")[0]
|
|
|
|
else:
|
|
|
|
id = file.split(".")[-2:][0]
|
|
|
|
|
|
|
|
except Exception, e:
|
|
|
|
#print e
|
|
|
|
id = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
id = id
|
|
|
|
except Exception, e:
|
|
|
|
#print e
|
|
|
|
id = False
|
|
|
|
|
|
|
|
print
|
|
|
|
print type + " id: ",
|
|
|
|
print id
|
|
|
|
|
|
|
|
|
|
|
|
response = self.api_client.update_start_playing(type, id, self.export_source, path)
|
|
|
|
|
|
|
|
print 'DONE'
|
|
|
|
|
|
|
|
try:
|
|
|
|
txt = response['txt']
|
|
|
|
print '#######################################'
|
|
|
|
print txt
|
|
|
|
print '#######################################'
|
|
|
|
#self.dls_client.set_txt(txt)
|
|
|
|
|
|
|
|
except Exception, e:
|
|
|
|
print e
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
print
|
|
|
|
print '#########################################'
|
|
|
|
print '# *** pypo *** #'
|
|
|
|
print '# pypo notification gateway #'
|
|
|
|
print '#########################################'
|
|
|
|
print
|
|
|
|
|
|
|
|
# initialize
|
|
|
|
g = Global()
|
|
|
|
g.selfcheck()
|
|
|
|
n = Notify()
|
|
|
|
|
|
|
|
|
|
|
|
run = True
|
|
|
|
while run == True:
|
|
|
|
logger = logging.getLogger("pypo notify")
|
|
|
|
|
|
|
|
if options.playing:
|
|
|
|
try: n.start_playing(options)
|
|
|
|
except Exception, e:
|
|
|
|
print e
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
sys.exit()
|