-
Notifications
You must be signed in to change notification settings - Fork 1
/
Flash-Video-Saver.py
90 lines (82 loc) · 2.4 KB
/
Flash-Video-Saver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python2
import os
from stat import S_IWUSR, S_IRUSR
from shutil import copyfile
from time import strftime, localtime, sleep, time
hosting_processes=['plugin-container'] #flash plugin container process name
dest_dir=os.environ['HOME']+'/Downloads' #target dir where file will be saved
min_size=500000 #minimum file size limit
file_age=30 #files this many seconds untouched will be saved (implicit facepalm)
saved_items={}
def find_container():
return filter(is_container, os.listdir('/proc'))
def is_container(pid):
try:
exe=os.path.realpath("/proc/%s/exe" % pid)
return any(exe.endswith(process) for process in hosting_processes)
except:
return False
def find_fds(pid):
writing=[]
written=[]
basedir="/proc/%s/fd" % pid
for i in os.listdir(basedir):
rp=os.path.realpath(basedir+'/'+i)
if 'tmp/FlashX' in rp:
print "found %s" % i
stat=os.stat(basedir+'/'+i)
mode=stat.st_mode
# epic fail:
#
# due to the non-deterministic behaviour of the flash plugin,
# i have to workaround detection of downloaded stuff like below:
#
#print S_IWUSR, mode
#if S_IWUSR & mode:
# if S_IRUSR & mode:
# written.append(basedir+'/'+i)
# else:
# writing.append(basedir+'/'+i)
print time()
print stat.st_mtime
print "diff:", time()-stat.st_mtime
if time()-stat.st_mtime > file_age:
written.append(basedir+'/'+i)
else:
writing.append(basedir+'/'+i)
print(written,writing)
return(written,writing)
def save_file(fd):
i=0
filename=strftime("%Y-%m-%d_%H.%M.%S", localtime())
destfile="%s/%s.flv" % (dest_dir, filename)
while os.path.exists(destfile):
i+=1
destfile="%s/%s-%d.flv" % (dest_dir, filename, i)
try:
print "Copying %s to %s" % (fd, destfile)
copyfile(fd, destfile)
except:
return False
return os.stat(fd)
while True:
pids=find_container()
for pid in pids:
found_fds=find_fds(pid)
for fd in found_fds[0]:
lstat=os.lstat(fd)
rstat=os.stat(fd)
print "fd in saved:", fd in saved_items
if (fd not in saved_items) or (fd in saved_items and saved_items[fd] != rstat):
if fd in saved_items:
print saved_items[fd]
print rstat
if rstat.st_size>min_size:
stat=save_file(fd)
if stat:
saved_items[fd]=stat
if fd in saved_items and saved_items[fd] == rstat:
pass
if found_fds[1]:
print "Not saving uncomplete fds: %s" % repr(found_fds[1])
sleep(5)