|
1 |
| -#!/usr/bin/python |
2 | 1 | import os
|
3 | 2 | import subprocess
|
4 | 3 | import random
|
|
9 | 8 | def set_path():
|
10 | 9 | if not os.path.exists("./path.txt"):
|
11 | 10 | path = raw_input("Enter the path where videos are stored")
|
12 |
| - if path !="": |
13 |
| - try: |
14 |
| - f=open("./path.txt","w") |
15 |
| - except(IOError): |
16 |
| - print "Something is wrong with the file" |
17 |
| - exit |
18 |
| - f.write(path) |
19 |
| - f.close() |
| 11 | + path=path.lstrip().rstrip() |
| 12 | + if os.path.exists(path): |
| 13 | + if path !="": |
| 14 | + try: |
| 15 | + f=open("./path.txt","w") |
| 16 | + except(IOError): |
| 17 | + print "Something is wrong with the file" |
| 18 | + exit() |
| 19 | + finally: |
| 20 | + print "file created on this path: "+path |
| 21 | + f.write(path) |
| 22 | + f.write("\n") |
| 23 | + f.close() |
| 24 | + else: |
| 25 | + print "That path is invalid" |
| 26 | + else: |
| 27 | + return |
20 | 28 |
|
21 |
| -def play(): |
| 29 | +def listing(): |
| 30 | +#this function invokes the vlc media player to actually play a video |
22 | 31 | try:
|
23 | 32 | f = open("./path.txt")
|
24 | 33 | except(IOError):
|
25 | 34 | print "Path invalid, please try again"
|
26 |
| - exit |
| 35 | + exit() |
| 36 | + |
27 | 37 | path = f.readline()
|
28 | 38 | f.close()
|
29 | 39 |
|
| 40 | + path=path.lstrip().rstrip() |
30 | 41 | video_list = os.listdir(path)
|
31 |
| - os.chdir(path) |
| 42 | + total_videos = len(video_list) |
32 | 43 |
|
33 |
| - subprocess.Popen(["vlc", video_list[random.randint(0,len(video_list)-1)]]) |
| 44 | + if not os.path.exists("./list.txt"): |
| 45 | + f=open("./list.txt","w") |
| 46 | + num=random.randint(0,len(video_list)-1) |
| 47 | + f.write("%s,"%(num)) |
| 48 | + f.close() |
| 49 | + else: |
| 50 | + f = open("./list.txt","r") |
| 51 | + line = f.readline() |
| 52 | + line = line.split(",") |
| 53 | + num=0 |
| 54 | + if len(line)==total_videos: #when all the videos have been watched, truncate the file |
| 55 | + print "all videos finished" |
| 56 | + f=open("./list.txt","w") |
| 57 | + f.close() |
| 58 | + while True: |
| 59 | + num=random.randint(0,len(video_list)-1) |
| 60 | + if str(num) not in line: |
| 61 | + break |
| 62 | + f=open("./list.txt","a") |
| 63 | + f.write("%s,"%(num)) |
| 64 | + print "written to file" |
| 65 | + f.close() |
| 66 | + |
| 67 | + play(path, video_list, num) |
| 68 | + |
| 69 | +def play(path, video_list, num): |
| 70 | + os.chdir(path) |
| 71 | + subprocess.Popen(["vlc", video_list[num]]) |
| 72 | + |
34 | 73 |
|
35 | 74 | if __name__=="__main__":
|
36 | 75 | set_path()
|
37 |
| - play() |
| 76 | + listing() |
0 commit comments