Difference between revisions of "Automatic Parental Control Settings - Mythvideo hack/feature request"

From MythTV Official Wiki
Jump to: navigation, search
m (Notes)
m (Warnings & Caveats: Link to the current backup/restore page)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
A quick db hack that enforces Parental Control levels based on a set of rules. This is for advanced users willing to put their fate into their own hands.
+
A quick db hack that enforces Parental Control levels based on a set of rules. This is for advanced users willing to put the fate of their video metadata into their own hands.
  
 
=Hack: Rule based parental control=
 
=Hack: Rule based parental control=
  
While the parental level controls in MythVideo are rudimentary, they are at least enough and get the job done and I'm quite grateful that they are in place. However, when trying to set the levels of large collections of files, the video-by-video nature of the feature is just too cumbersome. The following is an initial hack of the MySQL tables to automatically set the parental control levels based upon matching rules.
+
While the parental level controls in MythVideo are rudimentary, they are at least enough and get the job done and I'm quite grateful that they are in place. However, when trying to set the levels of large collections of files, the video-by-video nature of the feature is just too cumbersome. The following is an initial hack of the MySQL tables to automatically set the parental control levels based upon filename matching rules.
  
 
==Intent==
 
==Intent==
  
Let the parental control levels be automatically set for files in specific locations or even with specific naming conventions.
+
Let the parental control levels be automatically set for files in specific locations or for files with specific naming conventions.
 +
 
 +
==Requirements==
 +
You should already know how and be able to log into your database from the mysql text console with permissions to administer your MythTv tables. See [[:Category:MySQL]] for details and answers about the database itself.
  
 
==Warnings & Caveats==
 
==Warnings & Caveats==
This is the section where I make scary statements and relieve myself of responsibility for anything that might go wrong and of the responsibility to teach anyone how to use MySQL tools or anything else.
+
{{Warning box|[[Database_Backup_and_Restore|Backup your database]] before trying any of this.}}
  
'''First and foremost, this is for advanced users only.''' If you are confused about what I'm talking about in describing what to do or how the hack works don't just keep plugging along. Either educate yourself on the topics or resign yourself to wait for someone to make this more user-friendly.
+
{{Note box|It is important to read this entire document before actually starting to alter your own system or use the rule matching.}}
  
I haven't tested this against anything other than my own vanilla MythTV on Kubuntu install utilizing the MySQL db that came along for the ride... so 'works for me' is the policy of record.
+
This is the section where I make scary statements and relieve myself of responsibility for anything that might go wrong and of the responsibility to teach anyone how to use MySQL and database tools.  
  
If you're trying to do this, you should at least not be scared of poking around the database yourself and you should know how to start the mysql command line or some equivalent tool or better.
+
I categorize this as being for advanced users only because I am advising direct database alterations. If you are confused about what I'm talking about in describing what to do or how the hack works don't just keep plugging along. If you are lost with what is described, please read up on basic database and trigger concepts and how to use the mysql text console.
  
(kmysqladmin was easy and useful if you're looking for something slightly better than the command line for getting the 'big picture' of what is going on but I was a bit surprised at the dearth of freeware db tools on Linux for MySQL.)
+
</scary-section> It's really not that scary if you're at all familiar with the MySQL tools and concepts. Plus, we're dealing with the video metadata which isn't crucial for basic operation. Mythvideo is willing to repopulate the information if it is totally lost, but that won't happen since you backed everything up first :)
 +
{{Note box|I know that I tinkered with my permissions previous to this. While I'm not aware of any permission setting that must be done, it's something that might pop up, so be forewarned.}}
  
I know that I tinkered with my permissions previous to doing this. So while I'm not aware of any permission setting that must be done, it's something that might pop up, so be forewarned.
+
==Implementation==
  
'''Backup everything before trying any of this!'''
+
The automatic control setting is accomplished with a small number of additions to the mythconverg database:
 +
* <code>BEFORE INSERT</code> trigger named <code>videoAutolevel_insert</code>
 +
* <code>BEFORE UPDATE</code> trigger named <code>videoAutolevel_update</code>
 +
* function named <code>getVideoAutolevel</code>
 +
* table named <code>videoautolevel</code>
  
'''Proceed at your own risk!'''
+
Triggers are configured snippets of SQL that get executed when certain events occur. So by registering a '''<code>BEFORE INSERT</code>''' trigger, every time data is about to be put into the <code>videometadata</code> table, the registered SQL snippet gets a chance to alter the data before it is put into the table. Likewise the '''<code>BEFORE UPDATE</code>''' trigger allows the registered SQL snippet to similarly massage the incoming data about a movie.
  
'''Read this entire document before trying anything as it contains official warnings!'''
+
The triggers both call the added <code>getVideoAutolevel</code> function with the video's filename. The function masks off the configured video root and then looks for an entry in the added <code>videoautolevel</code> table. If a match occurs, the associated parental control level is returned. For multiple matches, the highest level is returned. If no match occurs, zero is returned. Return values of zero indicate that the parental control level should not be changed.
  
</scary-section> It's really not that scary once you're familiar with the MySQL tools and concepts plus since this deals with metadata (information about the videos), even if you blow away the table contents, it will be repopulated.
+
Mythvideo stores the full filename for each video in the metadata, but since they are all located in the same root directory, there's no need to also repeat the root portion for the rules.
  
==Implementation==
+
The initial named patterns set in <code>videoautolevel</code> are as follows:
  
This adds '''<code>BEFORE INSERT</code>''' and '''<code>BEFORE UPDATE</code>''' triggers to the <code>videometadata</code> table. The triggers ultimately consult with a new table named <code>videoautolevel</code> to automatically set the inserted or updated video's parental control level based on filename pattern matching. The initial named patterns are as follows:
 
 
{| class="wikitable" border="1"
 
{| class="wikitable" border="1"
 
!Pattern Name
 
!Pattern Name
Line 54: Line 61:
 
| style="text-align: center;" | 4
 
| style="text-align: center;" | 4
 
|}
 
|}
 +
 +
{{Note box|The use of triggers is convenient but they come with their own set of constraints that sometimes make them ill-suited as a real solution which is why I categorize this as a hack.}}
  
 
==Matching Examples==
 
==Matching Examples==
 +
{{warning box|Matches are '''NOT''' case sensitive!}}
 +
 +
Some example video file names and how each would be rated with the default rating rules. ''For clarity, the video root value for the sample file paths has not been included in the examples below.''
  
 
{| class="wikitable" border="1"
 
{| class="wikitable" border="1"
!Full File Path
+
!File Path
 
!Control Level Set
 
!Control Level Set
 
|-
 
|-
|/storage/myth/videos/rated/g/smurfs.avi
+
|rated/g/smurfs.avi
| style="text-align: center;" |1
+
|style="text-align: center;" |1
 
|-
 
|-
|/storage/myth/videos/rated/Pg/BattleSmurfs.avi
+
|rated/Pg/BattleSmurfs.avi
| style="text-align: center;" |2
+
|style="text-align: center;" |2
 
|-
 
|-
|/storage/myth/videos/rated/R/RowdySmurfs.avi
+
|rated/R/RowdySmurfs.avi
 
| style="text-align: center;" |3
 
| style="text-align: center;" |3
 
|-
 
|-
|/storage/myth/videos/rated/NR/RandySmurfs.avi
+
|rated/NR/RandySmurfs.avi
 
| style="text-align: center;" |4
 
| style="text-align: center;" |4
 
|-
 
|-
|/storage/myth/videos/cartoons/Return of the Smurfs.avi
+
|cartoons/Return of the Smurfs.avi
 
| style="text-align: center;" |untouched
 
| style="text-align: center;" |untouched
 
|}
 
|}
  
 
==Behavior==
 
==Behavior==
* Matches are '''NOT''' case sensitive! That was your official warning.
+
* Matches are '''NOT''' case sensitive.
 
* If a video's location does not match anything in the table, the showlevel (the parental level) isn't touched.
 
* If a video's location does not match anything in the table, the showlevel (the parental level) isn't touched.
 +
* If multiple patterns apply to a video's filename, the highest parental control level is used.
 
* New patterns can be added to the table as one sees fit, but I've not provided a way to do it other than through direct database manipulation. In fact I'm hoping someone runs with this and creates a more user-friendly mode of operation that ties into the larger platform naturally.
 
* New patterns can be added to the table as one sees fit, but I've not provided a way to do it other than through direct database manipulation. In fact I'm hoping someone runs with this and creates a more user-friendly mode of operation that ties into the larger platform naturally.
* If more than one pattern matches, the highest parental control value is used.
+
* Simple database wildcard matching is being performed for the matches. In SQL, the wildcard is '%' '''not''' '*'. Knowing this, one could create a rule like '%/rated/r/' to automatically set control levels for nested directory structures such as 'movies/rated/r/restricted.mpg'.  
* Simple database wildcard matching is being done where given a pattern, I prepend the video root location and postpend a wildcard and see if it matches the file name using the 'LIKE' operation. Knowing this, one could create rules like '%/rated/r/' to automatically set it for more nested directory structures. Alternatively, one could even look for text within the filename itself: '%Simpsons' but matching this generally and widely might get tricky and isn't as intuitive.
+
* Rules can be written to ignore directory structure. In this case, the match is against all of the text of the filename itself, but matching this generally can get tricky and isn't as intuitive. The rule '%simpsons' would match all filenames that contained the text 'simpsons' in it such as:
* This will only affect videos that are altered or added, it does not process already existing entries. (If you want to do this, see the 'Tips & Tricks' section below)
+
**'cartoons/thesimpsonsshow/episode1.avi'
 +
**'tv/simpsons-S45E54.avi'
 +
**'movies/circusimpsonparade.avi'
 +
* Once installed, only videos that are altered or added will have their control level automatically set. The installation process does not examine preexisting entries. (If you want to do this, see the 'Tips & Tricks' section below)
  
 
==Installation==
 
==Installation==
Line 91: Line 107:
 
==Tips & Tricks==
 
==Tips & Tricks==
  
To add a new rule where we want all files located in the 'coolVideos' directory to be set to level 2. Here's the SQL to do it:
+
Various SQL statements to manage matching rules.
 +
 
 +
===Add a rule===
 +
To add a new rule where all files located in the 'coolVideos' directory to be set to level 2. Here's the SQL to do it:
 
   INSERT INTO videoautolevel(name, path, showlevel) VALUES('cool vids', 'coolVideos/', 2);
 
   INSERT INTO videoautolevel(name, path, showlevel) VALUES('cool vids', 'coolVideos/', 2);
  
Line 104: Line 123:
 
   Bye
 
   Bye
  
To delete a rule (the one we just added above in this example), here is the SQL statement:
+
===Delete a rule===
 +
To delete the 'cool vids' rule added above:
 
   DELETE FROM videoautolevel WHERE name='cool vids';
 
   DELETE FROM videoautolevel WHERE name='cool vids';
  
 +
===List all rules===
 
To list all rules, here is the SQL statement:
 
To list all rules, here is the SQL statement:
 
   SELECT * FROM videoautolevel;
 
   SELECT * FROM videoautolevel;
  
 +
===Adjust all existing video entries===
 
To retroactively set levels according to rule definitions (Note that this will only alter entries that match against the current rules, you can't for instance remove a rule, run this and get back to where you were before the rule existed):
 
To retroactively set levels according to rule definitions (Note that this will only alter entries that match against the current rules, you can't for instance remove a rule, run this and get back to where you were before the rule existed):
 
   UPDATE videometadata  
 
   UPDATE videometadata  
 
     SET showlevel = getVideoAutolevel(filename)
 
     SET showlevel = getVideoAutolevel(filename)
 
     WHERE getVideoAutolevel(filename) > 0 ;
 
     WHERE getVideoAutolevel(filename) > 0 ;
 +
 
(Yes, I'm calling the function twice, if someone knows how to do this more efficiently AND more succinctly, I'm all ears :) )
 
(Yes, I'm calling the function twice, if someone knows how to do this more efficiently AND more succinctly, I'm all ears :) )
  
 
==Notes==
 
==Notes==
  
* Rule paths are expressed relative to the configured video directory allowing users to change the video root without affecting the rules. It also allows one to express rules without having to know what the directory is.
 
* I'm not sure if the BEFORE UPDATE is truly needed but it is included for safety and completeness.
 
 
* This could easily be extended to do things like look at the 'rating' of the movie (such as what is entered via IMDB entries) or other criteria to set the showlevel. A non-hacked approach would be to make the showlevel value be explicitly a computed value with user-settable overrides.
 
* This could easily be extended to do things like look at the 'rating' of the movie (such as what is entered via IMDB entries) or other criteria to set the showlevel. A non-hacked approach would be to make the showlevel value be explicitly a computed value with user-settable overrides.
  
  
 
[[Category:HOWTO]]
 
[[Category:HOWTO]]
[[Category:MySQL]]
 

Latest revision as of 04:03, 11 November 2009

A quick db hack that enforces Parental Control levels based on a set of rules. This is for advanced users willing to put the fate of their video metadata into their own hands.

Hack: Rule based parental control

While the parental level controls in MythVideo are rudimentary, they are at least enough and get the job done and I'm quite grateful that they are in place. However, when trying to set the levels of large collections of files, the video-by-video nature of the feature is just too cumbersome. The following is an initial hack of the MySQL tables to automatically set the parental control levels based upon filename matching rules.

Intent

Let the parental control levels be automatically set for files in specific locations or for files with specific naming conventions.

Requirements

You should already know how and be able to log into your database from the mysql text console with permissions to administer your MythTv tables. See Category:MySQL for details and answers about the database itself.

Warnings & Caveats

Warning.png Warning: Backup your database before trying any of this.


Important.png Note: It is important to read this entire document before actually starting to alter your own system or use the rule matching.

This is the section where I make scary statements and relieve myself of responsibility for anything that might go wrong and of the responsibility to teach anyone how to use MySQL and database tools.

I categorize this as being for advanced users only because I am advising direct database alterations. If you are confused about what I'm talking about in describing what to do or how the hack works don't just keep plugging along. If you are lost with what is described, please read up on basic database and trigger concepts and how to use the mysql text console.

</scary-section> It's really not that scary if you're at all familiar with the MySQL tools and concepts. Plus, we're dealing with the video metadata which isn't crucial for basic operation. Mythvideo is willing to repopulate the information if it is totally lost, but that won't happen since you backed everything up first :)

Important.png Note: I know that I tinkered with my permissions previous to this. While I'm not aware of any permission setting that must be done, it's something that might pop up, so be forewarned.

Implementation

The automatic control setting is accomplished with a small number of additions to the mythconverg database:

  • BEFORE INSERT trigger named videoAutolevel_insert
  • BEFORE UPDATE trigger named videoAutolevel_update
  • function named getVideoAutolevel
  • table named videoautolevel

Triggers are configured snippets of SQL that get executed when certain events occur. So by registering a BEFORE INSERT trigger, every time data is about to be put into the videometadata table, the registered SQL snippet gets a chance to alter the data before it is put into the table. Likewise the BEFORE UPDATE trigger allows the registered SQL snippet to similarly massage the incoming data about a movie.

The triggers both call the added getVideoAutolevel function with the video's filename. The function masks off the configured video root and then looks for an entry in the added videoautolevel table. If a match occurs, the associated parental control level is returned. For multiple matches, the highest level is returned. If no match occurs, zero is returned. Return values of zero indicate that the parental control level should not be changed.

Mythvideo stores the full filename for each video in the metadata, but since they are all located in the same root directory, there's no need to also repeat the root portion for the rules.

The initial named patterns set in videoautolevel are as follows:

Pattern Name Directory Pattern Control Level
General rated/g/ 1
Supervised rated/pg/ 2
Restricted rated/r/ 3
Not Rated rated/nr/ 4


Important.png Note: The use of triggers is convenient but they come with their own set of constraints that sometimes make them ill-suited as a real solution which is why I categorize this as a hack.

Matching Examples

Warning.png Warning: Matches are NOT case sensitive!

Some example video file names and how each would be rated with the default rating rules. For clarity, the video root value for the sample file paths has not been included in the examples below.

File Path Control Level Set
rated/g/smurfs.avi 1
rated/Pg/BattleSmurfs.avi 2
rated/R/RowdySmurfs.avi 3
rated/NR/RandySmurfs.avi 4
cartoons/Return of the Smurfs.avi untouched

Behavior

  • Matches are NOT case sensitive.
  • If a video's location does not match anything in the table, the showlevel (the parental level) isn't touched.
  • If multiple patterns apply to a video's filename, the highest parental control level is used.
  • New patterns can be added to the table as one sees fit, but I've not provided a way to do it other than through direct database manipulation. In fact I'm hoping someone runs with this and creates a more user-friendly mode of operation that ties into the larger platform naturally.
  • Simple database wildcard matching is being performed for the matches. In SQL, the wildcard is '%' not '*'. Knowing this, one could create a rule like '%/rated/r/' to automatically set control levels for nested directory structures such as 'movies/rated/r/restricted.mpg'.
  • Rules can be written to ignore directory structure. In this case, the match is against all of the text of the filename itself, but matching this generally can get tricky and isn't as intuitive. The rule '%simpsons' would match all filenames that contained the text 'simpsons' in it such as:
    • 'cartoons/thesimpsonsshow/episode1.avi'
    • 'tv/simpsons-S45E54.avi'
    • 'movies/circusimpsonparade.avi'
  • Once installed, only videos that are altered or added will have their control level automatically set. The installation process does not examine preexisting entries. (If you want to do this, see the 'Tips & Tricks' section below)

Installation

See this page. Feature_Wishlist_(Plugin_Addons)_mythvideo_autolevel_script

Tips & Tricks

Various SQL statements to manage matching rules.

Add a rule

To add a new rule where all files located in the 'coolVideos' directory to be set to level 2. Here's the SQL to do it:

 INSERT INTO videoautolevel(name, path, showlevel) VALUES('cool vids', 'coolVideos/', 2);

This can be saved into a file (coolRule.sql for instance) and executed like this (assuming 'root' can log in and doesn't require a password):

 mysql mythconverg -u root < coolRule.sql

or just entered at the mysql command line:

 ~$ mysql -u root mythconverg -A
 ...
 mysql> INSERT INTO videoautolevel(name, path, showlevel) VALUES('cool vids', 'coolvideos/', 2);
 Query OK, 1 row affected (0.02 sec)
 mysql> exit
 Bye

Delete a rule

To delete the 'cool vids' rule added above:

 DELETE FROM videoautolevel WHERE name='cool vids';

List all rules

To list all rules, here is the SQL statement:

 SELECT * FROM videoautolevel;

Adjust all existing video entries

To retroactively set levels according to rule definitions (Note that this will only alter entries that match against the current rules, you can't for instance remove a rule, run this and get back to where you were before the rule existed):

 UPDATE videometadata 
   SET showlevel = getVideoAutolevel(filename)
   WHERE getVideoAutolevel(filename) > 0 ;

(Yes, I'm calling the function twice, if someone knows how to do this more efficiently AND more succinctly, I'm all ears :) )

Notes

  • This could easily be extended to do things like look at the 'rating' of the movie (such as what is entered via IMDB entries) or other criteria to set the showlevel. A non-hacked approach would be to make the showlevel value be explicitly a computed value with user-settable overrides.