You are not logged in.

#1 2007-02-11 21:22:55

Echo
Member
From: Ohio, United States
Registered: 2006-05-16
Posts: 239

Python script help

I have a couple of first gen. Air2pc cards that I wanted to use to record some "over the air" hdtv shows. I wanted something "basic", using the command line, to simply record the shows at the times specified. Well I've been wanting to try python and so I wrote a script to do the above. The script works. Anyway, in regards to my problem, the section of the script below collects my input and then I have a while loop looping through, recording the channels selected. This allows me to schedule 3 recordings only. I would like to be able to schedule an infinite number of recordings. If someone could point me in the right direction I'd appreciate the help. I can post the entire script if need be, but, I hesitated in doing so as it's 170 lines long +/-.

count = 3

channel = raw_input("What channel do you want to record? ")
test = [channel]
channel = valid_ch(test, channels, channel)
title = raw_input("What should the recording be called? ")
title = title + ".ts"
presenttime = time.strftime("%I:%M:%S", time.localtime())
hourmintime = presenttime[:5]
print "The time is now", hourmintime
starttime = raw_input("When should the recording start? ")
add = ":10"
starttime += add
quittime = raw_input("When should the recording end? ")
zero = ":00"
quittime += zero

answerB = raw_input("Would you like to schedule another recording (y or n)? ")

if answerB == "y":
    channelB = raw_input("\nWhat channel do you want to record? ")        
    test = [channelB]
    channelB = valid_ch(test, channels, channelB)
    titleB = raw_input("What should the recording be called? ")
    titleB = titleB + ".ts"
    endtime = quittime[:5]
    print "The previously scheduled recording ends at", endtime 
    starttimeB = raw_input("When should the recording start? ")
    starttimeB += add
    quittimeB = raw_input("When should the recording end? ")
    quittimeB += zero
    count -= 1

    answerC = raw_input("Would you like to record another program (y or n)? ")
        
    if answerC == "y":
        channelC = raw_input("\nWhat channel do you want to record? ")
        test = [channelC]
        channelC = valid_ch(test, channels, channelC)
        titleC = raw_input("What should the recording be called? ")
        titleC = titleC + ".ts"
        endtime = quittimeB[:5]
        print "The previously scheduled recording ends at", endtime
        starttimeC = raw_input("When should the recording start? ")
        starttimeC += add
        quittimeC = raw_input("When should the recording end? ")
        quittimeC += zero

Thanks

Offline

#2 2007-02-11 23:50:35

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: Python script help

well i don't really understand most of it, like the reason for `count`; but hope this helps..
u can just loop...

import sys
while 1:
    if raw_input("Would you like to schedule another recording (y or n)? ") == "y":
        channelB = raw_input("\nWhat channel do you want to record? ")
        test = [channelB]
        channelB = valid_ch(test, channels, channelB)
        titleB = raw_input("What should the recording be called? ")
        titleB = titleB + ".ts"
        endtime = quittime[:5]
        print "The previously scheduled recording ends at", endtime
        starttimeB = raw_input("When should the recording start? ")
        starttimeB += add
        quittimeB = raw_input("When should the recording end? ")
        quittimeB += zero
        count -= 1
    else: sys.exit(0)

answerC would then be obsolete ...

Last edited by noriko (2007-02-11 23:51:20)


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#3 2007-02-12 01:16:43

Echo
Member
From: Ohio, United States
Registered: 2006-05-16
Posts: 239

Re: Python script help

well i don't really understand most of it, like the reason for `count`

Okay, here's the entire script. In hindsight, I probably should've just posted it to begin with.

#!/usr/bin/python

import sys, subprocess, time, signal, shutil, os, popen2

def kill(pid, sig=signal.SIGHUP):
    os.kill(pid, sig)
    os.waitpid(pid, 0)
    print "killed %d"%pid
    
def recnow():                                    
    print "Recording", title, "now."
    
def valid_ch(x, y, z):                                
    while x > 0:
        for key in x:
            if key in y:
                x = 0
                return z
            else:
                print "Note only channels", y, "are valid entries."
                z = raw_input("What channel do you want to record? ")
                x = [z]
                continue
                
def azap_(c):
    # Ant.conf is a list of channels the Air2pc card can tune to.
    azap_cmd = "azap -a 0 -f 0 -d 0 -c ~/Ant.conf "        
    channel = c
    string = str(channel)
    channel_plus = "ch" + string
    azap_end = " -r"
    strng = azap_cmd + channel_plus + azap_end    
    return strng
    
def time_chk(a, b):
    if a >= b:
        while a >= b:
            time.sleep(1)
            b = time.strftime("%I:%M:%S", time.localtime())
            continue
        else:
            a < b    
    
dd_rec = "dd if=/dev/dvb/adapter0/dvr0 ibs=19251200 conv=noerror"                
channels = ["3", "8", "11", "13"]                        

count = 3

channel = raw_input("What channel do you want to record? ")
test = [channel]
channel = valid_ch(test, channels, channel)
title = raw_input("What should the recording be called? ")
title = title + ".ts"
presenttime = time.strftime("%I:%M:%S", time.localtime())
hourmintime = presenttime[:5]
print "The time is now", hourmintime
starttime = raw_input("When should the recording start? ")
add = ":10"
starttime += add
quittime = raw_input("When should the recording end? ")
zero = ":00"
quittime += zero

answerB = raw_input("Would you like to schedule another recording (y or n)? ")

if answerB == "y":
    channelB = raw_input("\nWhat channel do you want to record? ")        
    test = [channelB]
    channelB = valid_ch(test, channels, channelB)
    titleB = raw_input("What should the recording be called? ")
    titleB = titleB + ".ts"
    endtime = quittime[:5]
    print "The previously scheduled recording ends at", endtime 
    starttimeB = raw_input("When should the recording start? ")
    starttimeB += add
    quittimeB = raw_input("When should the recording end? ")
    quittimeB += zero
    count -= 1

    answerC = raw_input("Would you like to record another program (y or n)? ")
        
    if answerC == "y":
        channelC = raw_input("\nWhat channel do you want to record? ")
        test = [channelC]
        channelC = valid_ch(test, channels, channelC)
        titleC = raw_input("What should the recording be called? ")
        titleC = titleC + ".ts"
        endtime = quittimeB[:5]
        print "The previously scheduled recording ends at", endtime
        starttimeC = raw_input("When should the recording start? ")
        starttimeC += add
        quittimeC = raw_input("When should the recording end? ")
        quittimeC += zero    

intg = 1

while intg > 0:
    time_chk(starttime, presenttime)
    recnow()                                
    app = popen2.Popen3(azap_(channel))
    file_output = open("/hdtv/tmp_rec.ts", "w")
    rec = subprocess.Popen(dd_rec, shell=True, stdout=file_output)                    
    time_chk(quittime, presenttime)
    name = ('/hdtv/tmp_rec.ts')
    newname = ('/hdtv/' + title)
    shutil.move(name, newname)
    pid = rec.pid
    kill(pid)
    file_output.close()
    pid = app.pid
    kill(pid)
                    
    if count == 2:
        channel = channelB
        title = titleB
        starttime = starttimeB
        quittime = quittimeB
        count -= 1
        continue
                                
    if count == 1:
        try:
            channel = channelC
            title = titleC
            starttime = starttimeC
            quittime = quittimeC
            count -= 1
            continue
        except NameError:
            print "======================"
            print "Exiting RecDVB.py now."
            print "======================"
            sys.exit()                        

    if count == 0:
        intg = 0
    if count == 3:
        intg = 0
                    
print "======================"                                        
print "Exiting RecDVB.py now."
print "======================"                                        
sys.exit()

noriko, thanks for taking a look.

Edit:As I mentioned this is my first attempt at python. I'm assigning values to the variables all at once then going through and recording based on the input. So, as far as I know, I need a unique variable each time, for example channel, channelB, ChannelC ....

Last edited by Echo (2007-02-12 01:30:31)

Offline

#4 2007-02-12 01:55:25

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Python script help

def get_record_specs():
 channel = raw_input("What channel do you want to record? ")
 test = [channel]
 channel = valid_ch(test, channels, channel)
 title = raw_input("What should the recording be called? ")
 title = title + ".ts"
 presenttime = time.strftime("%I:%M:%S", time.localtime())
 hourmintime = presenttime[:5]
 print "The time is now", hourmintime
 starttime = raw_input("When should the recording start? ")
 add = ":10"
 starttime += add
 quittime = raw_input("When should the recording end? ")
 zero = ":00"
 quittime += zero
 return channel, title, starttime, quittime

channel_list = []
title_list = []
starttime = []
quittime = []

while 1:
 choice = raw_input("Would you like to schedule another recording (y or n)? ")
 if choice.lower() == 'y' or choice.lower() == 'yes':
  channel, title, starttime, quittime = get_record_specs()
  channel_list.append(channel)
  title_list.append(title)
  starttime_list.append(starttime)
  quittime_list.append(quittime)
 else:
  break

something like that...

Offline

#5 2007-02-12 16:42:36

Echo
Member
From: Ohio, United States
Registered: 2006-05-16
Posts: 239

Re: Python script help

Thanks T-Dawg, I'll give it a try when I get home this evening. I'm at work at the moment. I was at a stand still on this question to say the least.

Edit: Yep, that did the trick.

Last edited by Echo (2007-02-13 02:03:32)

Offline

Board footer

Powered by FluxBB