OSDN Git Service

implement auto delete.
[rec10/rec10-git.git] / rec10 / trunk / src / classify.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5
6 import os
7 import os.path
8 import optparse
9 import sys
10
11 import configreader
12 import recdblist
13 import auto_move
14 import guess
15 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
16 recpath=unicode(configreader.getpath("recpath"),'utf-8')
17
18 if __name__ == "__main__":
19     usage="usage: %prog read -h"
20     version="%prog 0.9.4"
21     parser=optparse.OptionParser(usage=usage,version=version)
22     parser.add_option("-s","--search",action="store",type="string",dest="ltitle",default="",metavar="TITLE",help="test to search where the title should be contained(test for -e)")
23     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
24     parser.add_option("-D","--Delete",action="store_true",dest="delete",default=False,help="auto delete tempfile mode(not a test)")
25     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
26     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -A)")
27     parser.add_option("-t","--tssearch",action="store_true",dest="tssearch",default=False,help="Ts move auto search")
28     parser.add_option("-T","--Tsmove",action="store_true",dest="tsmove",default=False,help="Ts auto move.")
29     parser.add_option("-S","--SeriesNum",action="store",type="string",dest="seriespath",default="",metavar="TITLE",help="Search Series Number in the path.")
30     #parser.add_option("-f","--filenumlist",action="store_true",dest="filenumlist",default=False,help="Guess Program Number.")
31
32     (opts,args)=parser.parse_args(sys.argv)
33     if opts.ltitle!="":##-sの場合
34         ltitle=unicode(opts.ltitle,'utf-8')
35         sf=guess.searchFolder(ltitle, recordedpath)
36         print "###MKV###"
37         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mkv")+".mkv"))
38         print "\n"
39         print "###MP4###"
40         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mp4")+".mp4"))
41         print "\n"
42     elif opts.auto:##-A の場合
43         sa=auto_move.search_file(recpath, recordedpath, ".mkv")
44         for t in sa:
45             recdblist.printutf8(u"自動推測実行中-MKV")
46             sf=guess.searchFolder(t,recordedpath)
47             if sf!="":
48                 recdblist.printutf8(u"移動先")
49                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
50                 recdblist.printutf8(u"実行中")
51                 auto_move.execMove(t, recpath, recordedpath,".mkv",1)
52             else:
53                 recdblist.printutf8(t+" can't find matching folder")
54         sa=auto_move.search_file(recpath, recordedpath, ".mp4")
55         for t in sa:
56             recdblist.printutf8(u"自動推測実行中-MP4")
57             sf=guess.searchFolder(t,recordedpath)
58             if sf!="":
59                 recdblist.printutf8(u"移動先")
60                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
61                 recdblist.printutf8(u"実行中")
62                 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
63             else:
64                 recdblist.printutf8(t+" can't find matching folder")
65     elif opts.delete:##-Dの場合
66         sa=auto_move.search_file(recpath, recordedpath,".mkv")
67         for t in sa:
68             auto_move.execDelete(t, recpath)
69         sa=auto_move.search_file(recpath, recordedpath,".mp4")
70         for t in sa:
71             auto_move.execDelete(t, recpath)
72     elif opts.list:##-lの場合
73         sa=auto_move.search_file(recpath, recordedpath,".mkv")
74         for t in sa:
75             sf=guess.searchFolder(t,recordedpath)
76             if sf!="":
77                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
78         sa=auto_move.search_file(recpath, recordedpath,".mp4")
79         for t in sa:
80             sf=guess.searchFolder(t,recordedpath)
81             if sf!="":
82                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
83     elif opts.etitle!="":
84         if os.path.exists(os.path.join(recpath, etitle+".mkv")):
85             auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
86         elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
87             auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
88     elif opts.tssearch:## -tの場合
89         tsmovepath=""
90         try:
91             tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8')
92         except:
93             ""
94         if tsmovepath!="":
95             sa=auto_move.search_file(recpath, tsmovepath,".ts")
96             for t in sa:
97                 sf=guess.searchFolder(t,tsmovepath,700)
98                 if sf!="":
99                     recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
100                     print "\n"
101             sa=auto_move.search_file(recpath, tsmovepath,".ts")
102         else:
103             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")
104     elif opts.tsmove:##-Tの場合
105         tsmovepath=""
106         try:
107             tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8')
108         except:
109             ""
110         if tsmovepath!="":
111             sa=auto_move.search_file(recpath, tsmovepath,".ts")
112             for t in sa:
113                 sf=guess.searchFolder(t,tsmovepath,700)
114                 if sf!="":
115                     recdblist.printutf8(u"移動先")
116                     recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
117                     recdblist.printutf8(u"実行中")
118                     auto_move.execMove(t, recpath, tsmovepath,".ts",0)
119             sa=auto_move.search_file(recpath, tsmovepath,".ts")
120         else:
121             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")
122     elif opts.seriespath!="":##-Sの場合##与えられたパスにシリーズがそろっているかを調べる。
123         ss=guess.detSeriesNum(opts.seriespath)
124         for sstitle, ssv in ss.iteritems():
125             if len(ssv)>0:
126                 maxt=0
127                 bt=0
128                 alr=[]
129                 for ssi,ssiv in ssv.iteritems():
130                     alr.append(ssi)
131                     if maxt<ssi:
132                         maxt=ssi
133                 alr=list(set(alr))
134                 alr.sort()
135                 alr.reverse()
136                 nuke=""
137                 for i in xrange(1,maxt+1,1):
138                     if alr.count(i)==0:
139                         nuke=nuke+str(i)+","
140                 if len(nuke)==0:
141                     recdblist.printutf8(sstitle+" "+str(maxt))
142                 else:
143                     recdblist.printutf8(u"**"+sstitle+" |"+str(maxt)+"| "+nuke)
144
145