I wanted to stop having to scroll through directory after directory to see if any new videos have been downloaded. The python script I wrote below, starts automatically on my samba server at boot, and checks a specific directory (and all of its sub-directories) every minute to see if they contain .mp4, .mkv, or .avi files, and if they don't, it compiles a list of all of those folders and if the list of directories have changed since the last run, the script will veto them in a new samba conf file and reload the samba configuration.
In the end, I now only see folders on my media server, that actually contain videos.
*I do have to reboot/turn on my media player in order to get the new shares to show up, meaning, if the media player is on while this script changes the shares, I do not see the updated shared directories until next time I turn on my media player.
Configure Samba
Login to your Samba server. In order to configure shares in samba, you edit /etc/samba/smb.conf. I don't want my python script to modify this file, so I will edit smb.conf to include another conf file that my python script will generate and modify.
Configure samba to include an additional conf file
sudo nano /etc/samba/smb.conf
And add this at the end of the smb.conf file
include = /etc/samba/smb.conf.wdtv
Our new file that we will auto generate with the python script below, will be called smb.conf.wdtv
Create the python script
It currently looks for 3 different file extensions, but more can be added or omitted. Upper/Lower case of the extensions is accounted for in the script as well (.mp3 will look for .mp3, .MP3, .mP3, etc)
On the samba server, create a directory for the script
cd
mkdir scripts
cd scripts
create the script file
nano sambaveto.py
and copy&paste the code below, you will need to edit the variables mydirectory, mydirectorydepth, myextensions to match your specific needs.
#!/usr/bin/python
'''
re-write samba config file "smb.conf.wdtv" to only share directories with video files
must include the following line in the original /etc/smb.conf file
include = /etc/samba/smb.conf.wdtv
Algis Salys
'''
import os
import time
import subprocess as sub
starttime = time.time();
old = [];
old2 = [];
def searchfolders(mynotvalidold = []):
# Edit these variables to match your specific configuration
mydirectory = "/mnt/3TB/Video2/TV Shows/";
mydirectorydepth = 5; #Number of "/" in the above path
myextensions = ['.mp4', '.avi', '.mkv'];
# new conf file to write, this file must be included in the smb.conf file with the line > include = /etc/samba/smb.conf.wdtv
myfile = "/etc/samba/smb.conf.wdtv";
myvalid = [];
mynotvalid = [];
mynotset = set();
myset = set();
found = "";
for root, dirs, files in os.walk(mydirectory):
for file in files:
if file.lower().endswith(tuple(myextensions)):
found = 1;
if root not in myset:
myvalid.append(root);
myset.add(root);
if not found:
found = 0;
if cmp(mynotvalid, mynotvalidold): # if different since last check
mynotvalidold = mynotvalid;
# You may need to edit below to change the way you want samba to setup your share, this example shares the directory as a public share
f = open(myfile,'w')
f.write('# DO NOT EDIT - Autogenerated file\n')
f.write('[TV Shows]\n')
f.write('comment = USB 3TB Share /Video2 folder\n')
f.write('path = ' + mydirectory + '\n')
f.write('writeable = Yes\n')
f.write('only guest = Yes\n')
f.write('create mask = 0777\n')
f.write('directory mask = 0777\n')
f.write('browseable = Yes\n')
f.write('public = yes\n')
f.write('veto files = /')
for word in mynotvalid:
s = word.split('/')
f.write(s[mydirectorydepth]);
f.write('/');
f.write('\n')
f.close()
sub.call(["sudo", "smbcontrol", "smbd", "reload-config"])
return mynotvalidold;
# Run every 60 seconds
while True:
old2 = searchfolders(old)
old = old2
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
Make the script executable
chmod +x sambaveto.py
Test run the script (you may need to run as sudo to write in /etc/samba directory)
sudo ./sambaveto.py
Then to test if it worked, cat the /etc/samba/smb.conf.wdtv file to see the directories the script will omit
cat /etc/samba/smb.conf.wdtv
You can manually reload the samba config file for testing
sudo smbcontrol smbd reload-config
Run python script at boot
There are different ways to have the script start at boot, I chose to just include it in the /etc/rc.local file
sudo nano /etc/rc.local
and add this to the bottom of the file (but before the last line exit 0) of rc.local (change the script name/location if needed). Also confirm rc.local is executable. Note: rc.local runs eveything as root
(sleep 15;python /home/pi/scripts/sambaveto.py)&
Reboot the samba server
sudo reboot
Log back in, and confim the script is running
ps aux | grep "sambaveto.py"
Troubleshooting
You can see if the smb.conf files are being interpreted correctly using terparm. It will use the default file /etc/samba/smb.conf
testparm
or specifiy the file to use
testparm <file-to-use>
References: