#!/usr/bin/env python """ Author : mrcheeks Filename : neighbors.py License : Free to use, modify or distribute Description : A program to fight against stupid neighbors Look for nasty noises on the net like cows, dogs, etc. first """ import os import subprocess import sys import random import glob import time # the delay to play the next sound timers = [1800, 1900, 2000, 2100, 2200, 2300, 2400] # the mp3 folder soundsdir = "/home/mrcheeks/Projects/Python/sounds" programme = "/usr/bin/mplayer" class CoughPlayList: """ Class to play random songs """ def __init__(self): """ Default constructor """ self.program = programme self.timers = timers self.mp3list = glob.glob(soundsdir + os.sep + "*") def playSong(self): """ Play a random song """ # select a song sound = os.path.join(soundsdir, random.choice(self.mp3list)) # open the process p = subprocess.Popen(args=[self.program , sound], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False) # wait for the process to terminate p.wait() def main(argv=None): """ Script entry point """ # create a new instance of the class mplayer = CoughPlayList() # loop forever while True: try: # keyboard interrupt time.sleep(random.choice(mplayer.timers)) # play a song #if int(time.ctime()[11:13]) < 4: mplayer.playSong() except KeyboardInterrupt: # the program has been interrupted by the user print("Program interrupted by user") sys.exit(0) # run the script if __name__ == "__main__": sys.exit(main())