Skip to content

Commit e742e85

Browse files
committed
update video.py
fixed the bug of repetitive videos being played, now the program stores the previously played videos
1 parent 5007e48 commit e742e85

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

video.py

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/python
21
import os
32
import subprocess
43
import random
@@ -9,29 +8,69 @@
98
def set_path():
109
if not os.path.exists("./path.txt"):
1110
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
2028

21-
def play():
29+
def listing():
30+
#this function invokes the vlc media player to actually play a video
2231
try:
2332
f = open("./path.txt")
2433
except(IOError):
2534
print "Path invalid, please try again"
26-
exit
35+
exit()
36+
2737
path = f.readline()
2838
f.close()
2939

40+
path=path.lstrip().rstrip()
3041
video_list = os.listdir(path)
31-
os.chdir(path)
42+
total_videos = len(video_list)
3243

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+
3473

3574
if __name__=="__main__":
3675
set_path()
37-
play()
76+
listing()

0 commit comments

Comments
 (0)