OSDN Git Service

110b7489e2a2c7c31d182b879f1a97d423f55512
[rec10/rec10-git.git] / rec10 / classify.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2013 Yukikaze,long.inus
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 import rec10d
16 recordedpath=unicode(configreader.getConfPath("recorded"),'utf-8')
17 recpath=unicode(configreader.getConfPath("recpath"),'utf-8')
18
19 if __name__ == "__main__":
20     usage="usage: %prog read -h"
21     version="%prog 0.9.10"
22     parser=optparse.OptionParser(usage=usage,version=version)
23     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)")
24     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
25     parser.add_option("-D","--Delete",action="store_true",dest="delete",default=False,help="auto delete tempfile mode(not a test)")
26     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
27     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -A)")
28     parser.add_option("-t","--tssearch",action="store_true",dest="tssearch",default=False,help="Ts move auto search")
29     parser.add_option("-T","--Tsmove",action="store_true",dest="tsmove",default=False,help="Ts auto move.")
30     parser.add_option("-S","--SeriesNum",action="store",type="string",dest="seriespath",default="",metavar="TITLE",help="Search Series Number in the path.")
31     parser.add_option("-m","--manual",action="store_true",dest="manual",default=False,help="manual classifying mode asking for y/n(not a test)")
32     parser.add_option("-r","--rec10",action="store_true",dest="rec10",default=False,help="save moving plan in SQL table")
33     #parser.add_option("-f","--filenumlist",action="store_true",dest="filenumlist",default=False,help="Guess Program Number.")
34
35     (opts,args)=parser.parse_args(sys.argv)
36     if opts.ltitle!="":##-sの場合
37         ltitle=unicode(opts.ltitle,'utf-8')
38         sf=guess.searchFolder(ltitle, recordedpath)
39         print "###MKV###"
40         recdblist.printutf8(os.path.join(sf, auto_move.getMoveDestpath(ltitle,recpath,sf,".mkv")+".mkv"),verbose_level=100)
41         print "\n"
42         print "###MP4###"
43         recdblist.printutf8(os.path.join(sf, auto_move.getMoveDestpath(ltitle,recpath,sf,".mp4")+".mp4"),verbose_level=100)
44         print "\n"
45     elif opts.auto:##-A の場合
46         sa=auto_move.searchFile(recpath, recordedpath, ".mkv")
47         for t in sa:
48             recdblist.printutf8(u"自動推測実行中-MKV",verbose_level=100)
49             sf=guess.searchFolder(t,recordedpath)
50             if sf!="":
51                 recdblist.printutf8(u"移動先",verbose_level=100)
52                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mkv")+".mkv"),verbose_level=100)
53                 recdblist.printutf8(u"実行中",verbose_level=100)
54                 auto_move.execMove(t, recpath, recordedpath,".mkv",1)
55             else:
56                 recdblist.printutf8(t+" can't find matching folder",verbose_level=100)
57         sa=auto_move.searchFile(recpath, recordedpath, ".mp4")
58         for t in sa:
59             recdblist.printutf8(u"自動推測実行中-MP4",verbose_level=100)
60             sf=guess.searchFolder(t,recordedpath,200)
61             if sf!="":
62                 recdblist.printutf8(u"移動先",verbose_level=100)
63                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4"),verbose_level=100)
64                 recdblist.printutf8(u"実行中",verbose_level=100)
65                 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
66             else:
67                 recdblist.printutf8(t+" can't find matching folder",verbose_level=100)
68     elif opts.manual:##-m の場合
69         # mkv無視します
70         sa=auto_move.searchFile(recpath, recordedpath, ".mp4")
71         for t in sa:
72             sf=guess.searchFolder(t,recordedpath,200)
73             if sf!="":
74                 recdblist.printutf8(u"○候補:"+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4"),verbose_level=100)
75                 ans = raw_input("移動しますか? (y/n/h)")
76                 if ans == 'y':
77                     recdblist.printutf8(u"移動を実行します",verbose_level=100)
78                     auto_move.execMove(t, recpath, recordedpath,".mp4",1)
79                 elif ans == 'n':
80                     recdblist.printutf8(u"移動を中止します",verbose_level=100)
81                 elif ans == 'h':
82                     recdblist.printutf8(u"移動のヒント",verbose_level=100)
83                     recdblist.printutf8(u"FIXME:not implemented",verbose_level=100)
84             else:
85                 recdblist.printutf8(u"×不明:"+t,verbose_level=100)
86     elif opts.rec10:##-r の場合
87         # mkv無視します
88         sa=auto_move.searchFile(recpath, recordedpath, ".mp4")
89         for t in sa:
90             sf=guess.searchFolder(t,recordedpath,200)
91             if sf!="":
92                 frompath = os.path.join(recpath, t + ".mp4")
93                 topath = os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4")
94                 rec10d.rec10db.add_auto_classify(t,frompath, topath)
95         dbl=rec10d.rec10db.select_move_auto_classify()
96         if len(dbl)>0:
97             for dbt in dbl:
98                 title= dbt[1]
99                 #frompath = dbt[1]
100                 #topath = dbt[2]
101                 #t=os.path.splitext(os.path.split(frompath)[1])[0]
102                 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
103     elif opts.delete:##-Dの場合
104         sa=auto_move.searchFile(recpath, recordedpath,".mkv")
105         for t in sa:
106             auto_move.execDelete(t, recpath)
107         sa=auto_move.searchFile(recpath, recordedpath,".mp4")
108         for t in sa:
109             auto_move.execDelete(t, recpath)
110     elif opts.list:##-lの場合
111         sa=auto_move.searchFile(recpath, recordedpath,".mkv")
112         for t in sa:
113             sf=guess.searchFolder(t,recordedpath)
114             if sf!="":
115                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mkv")+".mkv"),verbose_level=100)
116         sa=auto_move.searchFile(recpath, recordedpath,".mp4")
117         for t in sa:
118             sf=guess.searchFolder(t,recordedpath)
119             if sf!="":
120                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".mp4")+".mp4"),verbose_level=100)
121     elif opts.etitle!="":
122         if os.path.exists(os.path.join(recpath, etitle+".mkv")):
123             auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
124         elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
125             auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
126     elif opts.tssearch:## -tの場合
127         tsmovepath=""
128         try:
129             tsmovepath=unicode(configreader.getConfPath("ts_movepath"),'utf-8')
130         except:
131             ""
132         if tsmovepath!="":
133             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
134             for t in sa:
135                 sf=guess.searchFolder(t,tsmovepath,700)
136                 if sf!="":
137                     recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".ts")+".ts"))
138                     print "\n"
139             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
140         else:
141             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。",verbose_level=100)
142     elif opts.tsmove:##-Tの場合
143         tsmovepath=""
144         try:
145             tsmovepath=unicode(configreader.getConfPath("ts_movepath"),'utf-8')
146         except:
147             ""
148         if tsmovepath!="":
149             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
150             for t in sa:
151                 sf=guess.searchFolder(t,tsmovepath,700)
152                 if sf!="":
153                     recdblist.printutf8(u"移動先",verbose_level=100)
154                     recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.getMoveDestpath(t, recpath,sf,".ts")+".ts"),verbose_level=100)
155                     recdblist.printutf8(u"実行中",verbose_level=100)
156                     auto_move.execMove(t, recpath, tsmovepath,".ts",0)
157             sa=auto_move.searchFile(recpath, tsmovepath,".ts")
158         else:
159             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。",verbose_level=100)
160     elif opts.seriespath!="":##-Sの場合##与えられたパスにシリーズがそろっているかを調べる。
161         ss=guess.detSeriesNum(opts.seriespath)
162         for sstitle, ssv in ss.iteritems():
163             if len(ssv)>0:
164                 maxt=0
165                 bt=0
166                 alr=[]
167                 for ssi,ssiv in ssv.iteritems():
168                     alr.append(ssi)
169                     if maxt<ssi:
170                         maxt=ssi
171                 alr=list(set(alr))
172                 alr.sort()
173                 alr.reverse()
174                 nuke=""
175                 for i in xrange(1,maxt+1,1):
176                     if alr.count(i)==0:
177                         nuke=nuke+str(i)+","
178                 if len(nuke)==0:
179                     recdblist.printutf8(sstitle+" "+str(maxt))
180                 else:
181                     recdblist.printutf8(u"**"+sstitle+" |"+str(maxt)+"| "+nuke)