#!/usr/bin/perl # # MythMe Script # # Version 0.3 # use strict; use DBI; use warnings; use SOAP::Lite; #use MythTV; use Data::GUID; my $dbname="mythconverg"; my $username="mythtv"; my $pw="mythtv"; my $webserviceUrl="http://www.andreas-fey.com"; my $client_version="0.3"; #my $myth=MythTV->new(); sub getUserId() { my $home = $ENV{ HOME }; my $userId; open(MYFILE, "$home/.mythme.txt"); if(my $line = ) { $userId = $line; } else { open(MYFILE, "> $home/.mythme.txt"); $userId = Data::GUID->new()->as_base64(); print MYFILE $userId; } close (MYFILE); return $userId; } sub fetchDBSettings() { my $home = $ENV{ HOME }; open (MYSQLFILE, "< $home/.mythtv/mysql.txt") or die "can't open file: $!"; while() { chomp; if($_ =~ /DBUserName/) { my ($key,$value) = split(/=/,$_,2); $username=$value; } elsif($_ =~ /DBPassword/) { my ($key,$value) = split(/=/,$_,2); $pw=$value; } elsif($_ =~ /DBName/) { my ($key,$value) = split(/=/,$_,2); $dbname=$value; } } close(MYSQLFILE); } sub fetchRecordedShowsAndCategories() { my $data = DBI->connect("DBI:mysql:$dbname", $username, $pw, { RaiseError => 1 }) || die("Cannot open database!"); my $allRecords=$data->prepare( "SELECT DISTINCT * FROM record" ); $allRecords->execute; my @shows; my @categories; my @priorities; while (my @result = $allRecords->fetchrow_array) { my $title = $result[7]; my $category = $result[10]; my $priority = $result[12]; push( @shows, $title ); push( @categories, $category ); push (@priorities, $priority ); } $allRecords->finish; $data->disconnect; return (\@shows, \@categories, \@priorities); } sub recordRecommendedShows() { my $show = $_[0]; my $data = DBI->connect("DBI:mysql:$dbname", $username, $pw, { RaiseError => 1 }) || die("Cannot open database!"); my $query=$data->prepare( "SELECT * FROM program WHERE title LIKE '$show' AND starttime > now()" ); $query->execute(); while (my @result = $query->fetchrow_array) { # calculate if the recommenden show is in 2009-06-19 19:50:00 my ($year_start, $month_start, $day_start, $hour_start, $minute_start, $second_start) = $result[1] =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/; my ($year_end, $month_end, $day_end, $hour_end, $minute_end, $second_end) = $result[2] =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/; #my ($yearl, $monthl, $dayl, $hourl, $minl, $secl, $doy,$dow,$dst) = Date::Calc::Localtime(); #my ($Dd,$Dh,$Dm,$Ds) = Date::Calc::Delta_DHMS($yearl,$monthl,$dayl, $hourl,$minl,$secl, $year,$month,$day, $hour,$minute,$second); print "Recommendation: Show $result[3] on channel id $result[0] at $result[1]\n"; #$data->prepare( "INSERT INTO record (chanid, starttime, startdate, endtime, enddate, title, subtitle, # description, category, recpriority, seriesid, programid, next_record) # VALUES ( $result[0], '$hour_start:$minute_start:$second_start', '$year_start-$month_start-$day_start', # '$hour_end:$minute_end:$second_end', '$year_end-$month_end-$day_end', '$result[3]', # '$result[4]', '$result[5]', '$result[6]', -100, '$result[18]', '$result[23]', '$result[1]')" )->execute; } $data->disconnect; } # ------------------------------------------------------------------------------- my ($shows_ref, $categories_ref, $priorities_ref) = fetchRecordedShowsAndCategories(); my @shows = @$shows_ref; my @categories = @$categories_ref; my @priorities = @$priorities_ref; &fetchDBSettings(); my $userId = &getUserId(); my $version_response = SOAP::Lite -> uri("$webserviceUrl/Mythme") -> proxy("$webserviceUrl/cgi-bin/mythme.cgi") -> getServerVersion(); my $server_version = $version_response->result; SOAP::Lite -> uri("$webserviceUrl/Mythme") -> proxy("$webserviceUrl/cgi-bin/mythme.cgi") -> updateUser($userId, @shows, @categories, @priorities); my $soap_response = SOAP::Lite -> uri("$webserviceUrl/Mythme") -> proxy("$webserviceUrl/cgi-bin/mythme.cgi") -> getShowRecommendations($userId); my $result = $soap_response->result; # notify user if a new version is available if($server_version > $client_version) { print "\n####################################\n"; print "A new MythMe version ($server_version) is\n"; print "available. Please download the new\n"; print "client script from \n"; print "$webserviceUrl/downloads/mythme.txt, \n"; print "rename it to 'mythme.pl' and execute\n"; print "'perl mythme.pl'.\n"; print "\n####################################\n"; } if($result) { print "Result from server: $result\n"; my @recShows = split(/###/, $result); foreach my $recShow (@recShows) { &recordRecommendedShows($recShow); } } else { print "Sorry dude, currently no recommondations for you. Please try again periodically!\n"; }