OSDN Git Service

tunerec: add version, 0.0.1
[rec10/rec10-git.git] / rec10 / recque.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2012 Yukikaze
5 import datetime
6 import os
7 import os.path
8 import glob
9 import time
10 import traceback
11
12 import configreader
13 import recdblist
14 import recdb
15 tmppath = configreader.getConfPath("tmp")+"/"
16 if not os.path.exists(tmppath):
17     os.mkdir(tmppath)
18 def writeRecQue(parentpath,chtxt,title,opts):
19     outputpath=os.path.join(parentpath,title+".recq")
20     tmpoppath=os.path.join(tmppath,title+".recq")
21     f=open(outputpath,"w")
22     optt=opts
23     str="99"+","+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+","+configreader.getConfEnv("iff")+","+chtxt+","+title+","+optt
24     f.write(str.encode('utf-8'))
25     f.close()
26     if not os.path.exists(outputpath):
27         if os.path.exists(tmpoppath):
28             os.remove(tmpoppath)
29         f2=open(tmpoppath,"w")
30         f2.write(str.encode('utf-8'))
31         f2.close()
32         try:
33             os.chmod(tmpoppath)
34         except:
35             ""
36         shutil.copy(tmpoppath,outputpath)
37         os.remove(tmpoppath)
38 def readRecQue(recquepath):
39     f=open(recquepath,"r")
40     line = unicode(f.readline(),'utf-8')
41     title=""
42     opts=""
43     chtxt=""
44     while line:
45         linec=line.split(",")
46         if len(linec)>3:
47             if linec[0]=="99":
48                 title=linec[4]
49                 opts=linec[5]
50                 chtxt=linec[2]+u"_"+linec[3]
51                 break
52         line = unicode(f.readline(),'utf-8')
53     dbkey=""
54     if opts.find("R")>-1:
55         dbkey=recdblist.REC_TS_DECODE_QUE
56         opts=opts.replace("R","")
57     elif opts.find("D")>-1:
58         dbkey=recdblist.REC_ENCODE_QUE
59         opts=opts.replace("D","")
60     elif opts.find("E")>-1:
61         dbkey=""
62         opts=opts.replace("E","")
63     
64     bt=datetime.datetime.now()+datetime.timedelta(minutes=5)
65     et=bt+datetime.timedelta(minutes=30)
66     btime=bt.strftime("%Y-%m-%d %H:%M:%S")
67     etime=et.strftime("%Y-%m-%d %H:%M:%S")
68     if len(dbkey)>2:
69         recdb.reserveReckey(dbkey, title, chtxt, btime, etime, opts)
70 def searchRecQue(folderpath):
71     for file in glob.glob(os.path.join(folderpath,"*.recq")):
72         dtime = time.time()-os.path.getmtime(file)
73         dtime = int(dtime)
74         if dtime > 300:
75             try:
76                 readRecQue(os.path.join(folderpath, file))
77                 os.remove(os.path.join(folderpath, file))
78             except Exception, inst:
79                 recdblist.addCommonlogEX("Error", "searchRecQue(recque.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
80