If you have not already, we suggest setting your Plex username to something else rather than email which is displayed on your posts in forum. You can change the username at https://app.plex.tv/desktop#!/account
Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

plexWatch - Plex Notify Script - Send Push Alerts On New Sessions and Stopped

2456767

Comments

  • ljunkieljunkie Plex Dev Team Posts: 4,835Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    edited July 2013
    bauerknight wrote on July 1 2013, 10:41 PM: »

    ljunkie, is there a way to run a report based on users?

    Also would like to see total duration combined for the users?  Would be useful to track how much time the kiddos are using plex(considering they like to 'surf plex' by stopping and starting new episodes every 2 minutes.  I would love to be able to see who's the top viewers based on duration.

     Version 0.0.10 released

     
     * '-stats' option added to '-watched' - show total watched time per user -- total time and per day
     * '-user' option added to '-watched' and '-watching' -- limit output to a specific user
     * $user_display hashref added to config - you can now show a 'friendly' username instead of the users email address [excluding @domain.tld]
     * platform title used if exists over platform. I.E. Chrome - Plex/Web (Chrome) will not show 'Plex/Web (Chrome)' -- it was redundant. Roku will still be Roku
     * log file is now hard coded to $data_dir/plexWatch.log instead of $data_dir/$appname.log. Allows users to change $appname to anything or nothing for notification
     

    Example: notice the --stats will work with date ranges, if you want all, don't include the date range. 

    /plexWatch.pl --watched --stats --user frank --start=yesterday
    
    * Limiting results to frank [real_user]
    
    ======================================== Watched ========================================
    Date Range: Sun Jun 30 00:00:00 2013 through Now
    
    User: Frank
     Sun Jun 30 07:15:02 2013: Frank watched: A Good Day to Die Hard [2013] [R] [duration: 1 hour, 31 minutes, and 59 seconds]
     Sun Jun 30 15:12:01 2013: Frank watched: Your Highness [2011] [R] [duration: 1 hour, 56 minutes, and 53 seconds]
     Sun Jun 30 15:46:02 2013: Frank watched: Star Trek [2009] [PG-13] [duration: 2 hours, 8 minutes, and 18 seconds]
     Sun Jun 30 21:10:01 2013: Frank watched: Judge Dredd [1995] [R] [duration: 1 hour, 41 minutes, and 1 second]
    
    
    ======================================== Stats ========================================
    user: Frank's total duration 7 hours, 18 minutes, and 11 seconds 
     Sun Jun 30 2013: Frank 7 hours, 18 minutes, and 11 seconds
    
    

    I will look into adding top users in the future. 

  • WeeflixWeeflix Posts: 238Members, Plex Pass Plex Pass

    Latest version works great with the stats.  I realize my kids are spending too much time on plex heh.

    I noticed the season and episode number doesn't show up in the notification.  Is there anyway to add it to the nofications?  I don't see it stored in the xml so no biggie if you can't.

  • ljunkieljunkie Plex Dev Team Posts: 4,835Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    bauerknight wrote on July 2 2013, 3:48 AM: »

    Latest version works great with the stats.  I realize my kids are spending too much time on plex heh.

    I noticed the season and episode number doesn't show up in the notification.  Is there anyway to add it to the nofications?  I don't see it stored in the xml so no biggie if you can't.

    Tell your kids I'm sorry :) The season and episode have been added to the alert. Should work going backwards for the cli options. 

  • WeeflixWeeflix Posts: 238Members, Plex Pass Plex Pass

    Thanks again!

  • DrZeussDrZeuss Posts: 214Members, Plex Pass Plex Pass
    edited July 2013

    Thanks ljunkie. I tried to get the script working on OSX 10.8.4. Installed the Perl dependencies via cpan with no problem but when I execute the script (manually) I get the following error message:

    DBD::SQLite::db prepare failed: disk I/O error at ./plexWatch.pl line 575.
    disk I/O error at ./plexWatch.pl line 575.
    
     

    Looking at the code, it looks like its failing to establish a connection to the sqllite database file thats in the plexWatch folder. The plexWatch.db is created (empty) and the error seems to occur because table sqlite_master doesn't appear to exist.

    Any ideas?

    EDIT: Nevermind, I figured out the problem. The path for plexWatch was inside a NAS mounted volume, I moved plexWatch to a local HDD filesystem and hey presto Disk I/O errors have disappeared and all appears to be working well.

    iMac i5QC as Plex Media Server + TV Shows + Trakt.TV + PRS
    2x ATV 2 with Plex Client | iPhone 4S and iPad 2 with Plex iOS Client | MacMini with Plex Client
  • rcorkrcork Posts: 440Members, Plex Pass Plex Pass
    edited July 2013

    Anyone know how to install the dependencies on OSX with macports. I tried cpan but get compile errors with WWW::Curl::Easy. I fear my macport install is impacting cpan.

    UPDATE: Ok, i was able to install the dependencies after upgrading cpan to v2. However, i had to force install WWW::Curl::Easy as it would not install on it's own. All other dependencies installed ok. However, when i run the script it says nothing is being watched or has been watched even. Has anyone got this working on OSX?

  • DrZeussDrZeuss Posts: 214Members, Plex Pass Plex Pass
    rcork wrote on July 3 2013, 6:44 PM: »

    Anyone know how to install the dependencies on OSX with macports. I tried cpan but get compile errors with WWW::Curl::Easy. I fear my macport install is impacting cpan.

    UPDATE: Ok, i was able to install the dependencies after upgrading cpan to v2. However, i had to force install WWW::Curl::Easy as it would not install on it's own. All other dependencies installed ok. However, when i run the script it says nothing is being watched or has been watched even. Has anyone got this working on OSX?

    Yep, I had to force install of WWW::Curl::Easy too.  I can confirm its working well on OSX 10.8.4.

    The script is run in two parts. When run with no parameters or with the --notify flag the script silently collects Plex Now Playing information and logs it to the database and to the log file (default behaviour). This is what the cron job is doing; running ./plexWatch.pl every second, every minute, every day etc.You can also configure the script to integrate with Prowl/Pushover.

    Once the script has been running for a while and its collected data (you need to ensure users have started watching some media before you do this) you can run a 2nd instance of the script with the --watched / --watching flags to interrogate the database and  output any Now Playing start/stop entries.

    iMac i5QC as Plex Media Server + TV Shows + Trakt.TV + PRS
    2x ATV 2 with Plex Client | iPhone 4S and iPad 2 with Plex iOS Client | MacMini with Plex Client
  • rcorkrcork Posts: 440Members, Plex Pass Plex Pass

    I was worried that forcing the install of curl was causing an issue so i actually downloaded the source (https://github.com/szbalint/WWW--Curl) and installed it directly, which installed without error.

    So let me make sure i understand correctly. The script needs to continually run so it can capture the Now Playing data. It can't pull it retroactively. So i will need a cron job or other mechanism to continually run it through the day to capture. Is that correct?

  • ljunkieljunkie Plex Dev Team Posts: 4,835Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    rcork wrote on July 3 2013, 9:54 PM: »

    I was worried that forcing the install of curl was causing an issue so i actually downloaded the source (https://github.com/szbalint/WWW--Curl) and installed it directly, which installed without error.
     
    So let me make sure i understand correctly. The script needs to continually run so it can capture the Now Playing data. It can't pull it retroactively. So i will need a cron job or other mechanism to continually run it through the day to capture. Is that correct?


    Yes, unless plex has some secret log, you have to cron the script. It's lightweight, so running it every minute is safe. Refer to steps #5 and #6.

    I'm mobile otherwise I'd have a more detailed response.
  • DrZeussDrZeuss Posts: 214Members, Plex Pass Plex Pass
    rcork wrote on July 3 2013, 9:54 PM: »

    I was worried that forcing the install of curl was causing an issue so i actually downloaded the source (https://github.com/szbalint/WWW--Curl) and installed it directly, which installed without error.

    So let me make sure i understand correctly. The script needs to continually run so it can capture the Now Playing data. It can't pull it retroactively. So i will need a cron job or other mechanism to continually run it through the day to capture. Is that correct?

    Yes. You have one script running (without any flags) every minute via cron to collect data. This is because the published Plex Now Playing information (http://localhost:32400/status/sessions) holds real time data only. No historical data is logged (to my knowledge).

    You run a second instance to view historical or currently playing information.

    iMac i5QC as Plex Media Server + TV Shows + Trakt.TV + PRS
    2x ATV 2 with Plex Client | iPhone 4S and iPad 2 with Plex iOS Client | MacMini with Plex Client
  • rcorkrcork Posts: 440Members, Plex Pass Plex Pass

    Thanks for the clarification. I created a LaunchAgent and it's running every second. So far so good.

  • rcorkrcork Posts: 440Members, Plex Pass Plex Pass

    I manually edited the script to create another notification service for growlnotify. That way i get notifications on my desktop as well as through Prowl. I know this is very specific to OSX but if anyone's interested i can share the changes i made.

  • ljunkieljunkie Plex Dev Team Posts: 4,835Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    rcork wrote on July 4 2013, 12:53 AM: »

    I manually edited the script to create another notification service for growlnotify. That way i get notifications on my desktop as well as through Prowl. I know this is very specific to OSX but if anyone's interested i can share the changes i made.

    Yes. Any additionally notification options that have been tested is much appreciated. I will add the code to a new version so plexWatch will have it ongoing for any future updates. 

  • ljunkieljunkie Plex Dev Team Posts: 4,835Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    rcork wrote on July 3 2013, 11:01 PM: »

    Thanks for the clarification. I created a LaunchAgent and it's running every second. So far so good.

    Every second? that might be a little too quick. If it seems to work though, go for it. It is possible that someone could stop and start a show within a minute... but I wasn't too worried about those as that isn't really watched at all imho. 

  • rcorkrcork Posts: 440Members, Plex Pass Plex Pass
    ljunkie wrote on July 4 2013, 1:20 AM: »

    Yes. Any additionally notification options that have been tested is much appreciated. I will add the code to a new version so plexWatch will have it ongoing for any future updates. 

    In the variables section i added two variables that are used for growlnotify

    ## Growl variables
    my $growlnotify = '/usr/local/bin/growlnotify';
    my $plexIcon = '/Applications/Plex Media Server.app/Contents/Resources/MyPlex.icns';
    

    Under the section ##Notification Options i added:

    'growl' => {
            'enabled' => 1, ## set to 1 to enable local growl
            'application' => $appname
        },
    

    In subroutine Notify() i added

    if ($notify->{'growl'}->{'enabled'}) {     &NotifyGrowl($alert, $extra); }
    

    Then i created a new subroutine called NotifyGrowl:

    sub NotifyGrowl() { 
    	my $alert = shift;
    	my $extra = shift;
    	my $message = $alert . $extra;
        system( "$growlnotify", "-n", $appname, "--image",  $plexIcon, "-m", $message); 
    }
    

    I don't know perl so i pretty much hacked this together so it's not great and could definitely use some cleaning by someone who is good at perl

  • ljunkieljunkie Plex Dev Team Posts: 4,835Members, Plex Employee, Plex Pass, Plex Ninja Plex Employee
    rcork wrote on July 4 2013, 3:01 AM: »

    In the variables section i added two variables that are used for growlnotify

    ## Growl variables
    my $growlnotify = '/usr/local/bin/growlnotify';
    my $plexIcon = '/Applications/Plex Media Server.app/Contents/Resources/MyPlex.icns';
    

    Under the section ##Notification Options i added:

    'growl' => {
            'enabled' => 1, ## set to 1 to enable local growl
            'application' => $appname
        },
    

    In subroutine Notify() i added

    if ($notify->{'growl'}->{'enabled'}) {     &NotifyGrowl($alert, $extra); }
    

    Then i created a new subroutine called NotifyGrowl:

    sub NotifyGrowl() { 
    	my $alert = shift;
    	my $extra = shift;
    	my $message = $alert . $extra;
        system( "$growlnotify", "-n", $appname, "--image",  $plexIcon, "-m", $message); 
    }
    

    I don't know perl so i pretty much hacked this together so it's not great and could definitely use some cleaning by someone who is good at perl

    Looks good. I will look into adding this in the next release. Can you point me to the source your used for '/usr/local/bin/growlnotify'

  • rcorkrcork Posts: 440Members, Plex Pass Plex Pass
    ljunkie wrote on July 4 2013, 4:02 AM: »

    Looks good. I will look into adding this in the next release. Can you point me to the source your used for '/usr/local/bin/growlnotify'

    info on growlnotify can be found here: http://growl.info/downloads 

    It's only available on OSX so not sure how you'd want to handle since your script is pretty much cross platform

  • gertgermangertgerman Posts: 755Members ✭✭
    Awesome! Will be giving this a shot at the weekend. Excellent work thanks for sharing :)
  • DrZeussDrZeuss Posts: 214Members, Plex Pass Plex Pass
    edited July 2013

    Worth mentioning that growlnotify is an Extra package thats not bundled with the standard Growl.app for OSX. You can download growlnotify from here: http://growl.cachefly.net/GrowlNotify-2.0.zip

    UPDATE: I also tweaked the 'extra' information to drop the summary field data. I did this for both the console log and the Growl notification message as the summary field data can be quite long in cases and when so causes the summary to span several lines. I left the data in the database though.

    UPDATE: Just discovered Growl can chain the notifications to other frameworks including OSX Notificitions, Prowl, SMS, bark and several others. Sweet.

    iMac i5QC as Plex Media Server + TV Shows + Trakt.TV + PRS
    2x ATV 2 with Plex Client | iPhone 4S and iPad 2 with Plex iOS Client | MacMini with Plex Client
  • WeeflixWeeflix Posts: 238Members, Plex Pass Plex Pass

    ljunkie can you add to the stopped notifications the Duration?  For example: User stopped watching: title on platform after 1 hour 2 minutes.  

    It would be great for troubleshooting.  I'll be able to easily tell if a user is keeps getting cut off from their stream.

    Thanks again!

Sign In or Register to comment.