root/references/Shepherd/MythTV.pm @ 680

Revision 680, 2.2 kB (checked in by max, 6 years ago)

Added Shepherd::MythTV for interaction with MythTV database
Migrated augment_timezone postprocessor to use it

Line 
1#!/usr/bin/perl
2#
3# Shepherd::MythTV library
4#
5# This module provides some library functions for Shepherd components,
6# relieving them of the need to duplicate functionality.
7#
8# To use this library, components simply need to include the line:
9#
10#   use Shepherd::MythTV;
11
12package Shepherd::MythTV;
13
14use DBI;
15
16my $version = '0.1';
17
18my $db;
19
20sub find_database_settings_file
21{
22    my $cfgfile = shift;
23
24    $cfgfile = &standard_mysql_locations unless ($cfgfile);
25
26    foreach my $f (split(/:/,$cfgfile)) 
27    {
28        return $f if ((-f $f) && (-r $f));
29    }
30
31    print "\nWARNING: Could not find MythTV mysql.txt config file!\n".
32          "Looked in: $cfgfile\n";
33    return undef;
34}
35
36sub standard_mysql_locations
37{
38    return "/usr/local/share/mythtv/mysql.txt".
39           ":/usr/share/mythtv/mysql.txt".
40           ":$ENV{HOME}/.mythtv/mysql.txt".
41           ":/home/mythtv/.mythtv/mysql.txt".
42           ":/root/.mythtv/mysql.txt".
43           ":/etc/mythtv/mysql.txt";
44}
45
46# Find MythTV database settings
47#
48sub setup
49{
50    my $cfgfile = shift;
51
52    $cfgfile = &find_database_settings_file unless ($cfgfile);
53    return if (!defined $cfgfile);
54
55    unless (open(F,"<$cfgfile")) 
56    {
57        print "ERROR: Couldn't read $cfgfile: $!\n";
58        return undef;
59    }
60
61    print "Reading MythTV DB settings from: $cfgfile\n";
62
63    while (<F>) 
64    {
65        chomp;
66        $db->{$1} = $2 if ($_ =~ /^(DB.*)=(.*)/);
67    }
68    close(F);
69}
70
71#
72# Send an SQL query to the MythTV DB. Will try standard locations to
73# find the MythTV mysql.txt file; if you want to specify a non-standard
74# location, first call Shepherd::MythTV::setup($file_location), where
75# $file_location is a colon-separated string of (potential) filenames.
76#
77# Note that data is returned in array form.
78#
79sub query
80{
81    my $sql = shift;
82
83    &setup unless ($db);
84
85    unless ($db->{DBName} and $db->{DBHostName} and $db->{DBUserName} and $db->{DBPassword})
86    {
87        print "ERROR: Missing essential DB connection info.\n";
88        return undef;
89    }
90
91    my $dbh = DBI->connect(
92                "dbi:mysql:database=".$db->{DBName}.":host=".$db->{DBHostName},
93                $db->{DBUserName}, $db->{DBPassword});
94    unless ($dbh) 
95    {
96        print "WARNING: Couldn't connect to database ".$opt->{dbname}.": $!\n";
97        return undef;
98    }
99    return $dbh->selectrow_array($sql);
100}
101
1021;
Note: See TracBrowser for help on using the browser.