OSDN Git Service

convert CRLF to LF
[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-2011 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.getConfPath("recorded"),'utf-8')
16 recpath=unicode(configreader.getConfPath("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("-m","--manual",action="store_true",dest="manual",default=False,help="manual classifying mode asking for y/n(not a test)")
31     #parser.add_option("-f","--filenumlist",action="store_true",dest="filenumlist",default=False,help="Guess Program Number.")
32
33     (opts,args)=parser.parse_args(sys.argv)
34     if opts.ltitle!="":##-sの場合
35         ltitle=unicode(opts.ltitle,'utf-8')
36         sf=guess.searchFolder(ltitle, recordedpath)
37         print "###MKV###"
38         recdblist.printutf8(os.path.join(sf, auto_move.getMoveDestpath(ltitle,recpath,sf,".mkv")+".mkv"),verbose_level=100)
39         print "\n"
40         print "###MP4###"
41         recdblist.printutf8(os.path.join(sf, auto_move.getMoveDestpath(ltitle,recpath,sf,".mp4")+".mp4"),verbose_level=100)
42         print "\n"
43     elif opts.auto:##-A の場合
44         sa=auto_move.searchFile(recpath, recordedpath, ".mkv")
45         for t in sa:
46             recdblist.printutf8(u"自動推測実行中-MKV",verbose_level=100)
47             sf=guess.searchFolder(t,recordedpath)
48             if sf!="":
49                 recdblist.printutf8(u"移動先",verbose_level=100)
50                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mkv")+".mkv"),verbose_level=100)
51                 recdblist.printutf8(u"実行中",verbose_level=100)
52                 auto_move.execMove(t, recpath, recordedpath,".mkv",1)
53             else:
54                 recdblist.printutf8(t+" can't find matching folder",verbose_level=100)
55         sa=auto_move.searchFile(recpath, recordedpath, ".mp4")
56         for t in sa:
57             recdblist.printutf8(u"自動推測実行中-MP4",verbose_level=100)
58             sf=guess.searchFolder(t,recordedpath,200)
59             if sf!="":
60                 recdblist.printutf8(u"移動先",verbose_level=100)
61                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4"),verbose_level=100)
62                 recdblist.printutf8(u"実行中",verbose_level=100)
63                 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
64             else:
65                 recdblist.printutf8(t+" can't find matching folder",verbose_level=100)
66     elif opts.manual:##-m の場合
67         # mkv無視します
68         sa=auto_move.searchFile(recpath, recordedpath, ".mp4")
69         for t in sa:
70             sf=guess.searchFolder(t,recordedpath,200)
71             if sf!="":
72                 recdblist.printutf8(u"○候補:"+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4"),verbose_level=100)
73                 ans = raw_input("移動しますか? (y/n/h)")
74                 if ans == 'y':
75                     recdblist.printutf8(u"移動を実行します",verbose_level=100)
76                     auto_move.execMove(t, recpath, recordedpath,".mp4",1)
77                 elif ans == 'n':
78                     recdblist.printutf8(u"移動を中止します",verbose_level=100)
79                 elif ans == 'h':
80                     recdblist.printutf8(u"移動のヒント",verbose_level=100)
81                     recdblist.printutf8(u"FIXME:not implemented",verbose_level=100)
82             else:
83                 recdblist.printutf8(u"×不明:"+t,verbose_level=100)
84     elif opts.delete:##-Dの場合
85         sa=auto_move.searchFile(recpath, recordedpath,".mkv")
86         for t in sa:
87             auto_move.execDelete(t, recpath)
88         sa=auto_move.searchFile(recpath, recordedpath,".mp4")
89         for t in sa:
90             auto_move.execDelete(t, recpath)
91     elif opts.list:##-lの場合
92         sa=auto_move.searchFile(recpath, recordedpath,".mkv")
93         for t in sa:
94             sf=guess.searchFolder(t,recordedpath)
95             if sf!="":
96                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mkv")+".mkv"),verbose_level=100)
97         sa=auto_move.searchFile(recpath, recordedpath,".mp4")
98         for t in sa:
99             sf=guess.searchFolder(t,recordedpath)
100             if sf!="":
101                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4"),verbose_level=100)
102     elif opts.etitle!="":
103         if os.path.exists(os.path.join(recpath, etitle+".mkv")):
104             auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
105         elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
106             auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
107     elif opts.tssearch:## -tの場合
108         tsmovepath=""
109         try:
110             tsmovepath=unicode(configreader.getConfPath("ts_movepath"),'utf-8')
111         except:
112             ""
113         if tsmovepath!="":
114             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
115             for t in sa:
116                 sf=guess.searchFolder(t,tsmovepath,700)
117                 if sf!="":
118                     recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".ts")+".ts"))
119                     print "\n"
120             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
121         else:
122             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。",verbose_level=100)
123     elif opts.tsmove:##-Tの場合
124         tsmovepath=""
125         try:
126             tsmovepath=unicode(configreader.getConfPath("ts_movepath"),'utf-8')
127         except:
128             ""
129         if tsmovepath!="":
130             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
131             for t in sa:
132                 sf=guess.searchFolder(t,tsmovepath,700)
133                 if sf!="":
134                     recdblist.printutf8(u"移動先",verbose_level=100)
135                     recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".ts")+".ts"),verbose_level=100)
136                     recdblist.printutf8(u"実行中",verbose_level=100)
137                     auto_move.execMove(t, recpath, tsmovepath,".ts",0)
138             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
139         else:
140             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。",verbose_level=100)
141     elif opts.seriespath!="":##-Sの場合##与えられたパスにシリーズがそろっているかを調べる。
142         ss=guess.detSeriesNum(opts.seriespath)
143         for sstitle, ssv in ss.iteritems():
144             if len(ssv)>0:
145                 maxt=0
146                 bt=0
147                 alr=[]
148                 for ssi,ssiv in ssv.iteritems():
149                     alr.append(ssi)
150                     if maxt<ssi:
151                         maxt=ssi
152                 alr=list(set(alr))
153                 alr.sort()
154                 alr.reverse()
155                 nuke=""
156                 for i in xrange(1,maxt+1,1):
157                     if alr.count(i)==0:
158                         nuke=nuke+str(i)+","
159                 if len(nuke)==0:
160                     recdblist.printutf8(sstitle+" "+str(maxt))
161                 else:
162                     recdblist.printutf8(u"**"+sstitle+" |"+str(maxt)+"| "+nuke)