Python Forum

Full Version: Help with calling the function as the code keep firing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I need your help. I am working on play function to fetch the video links in the database so I can play the video. I have got a bit of trouble with looping the code, because when I find the video link is dead I want to skip and fetch the video link in the next column until I find the working video link to play the video.

Here is the code:

def play(self, item = None, windowed = False):
    print "you are working on play.........................."

    bool = True
    url = ''

    if not self.player.isPlayingVideo():
       conn = database.connect(self.profilePath + 'source.db')
       cur = conn.cursor()
       self.count += 1

       print "self.count...............................for stream_url column"
       print self.count


       try:
          cur.execute('SELECT stream' + str(self.count) + '_url FROM streams WHERE channels=?', [self.EPG_Channel])
          data = cur.fetchone()

          if data is not None:
             url = data[0]           

       except:
           self.count = 1
           cur.execute('SELECT stream1_url FROM streams WHERE channels=?', [self.EPG_Channel])
           data = cur.fetchone()

           if data is not None:
              url = data[0]
       conn.close()

       self.player.play(url, windowed = bool)
       self.channel_pressed = False

       if not self.player.isPlayingVideo():
          self.play()
On my code that I wrote, it will keep looping and firing the code to fetch the link from the column stream1_url as it will not skip to the next column as it will make the application to get crash.

I have got 3 columns in the database stream1_url, stream2_url and stream3_url in a database. What I am trying to do is I want to fetch the video link from the column stream1_url and play the video. If the video don't start to play as the link is dead, I want to call the play function again when I am using self.count += 1 to count it to up so I can fetch the new video link from the column stream2_url, but it the link is not working I want to call the play function again and fetch the video link from the column stream3_url and play the video.

Can you please show me an example what I should write in a proper way without crash the application?

Thanks in advance.
does anyone know?????????????
Where does you code fail? Do you have an Exception and could post it? On the first glance your code looks fine.
(May-03-2018, 06:14 PM)ThiefOfTime Wrote: [ -> ]Where does you code fail? Do you have an Exception and could post it? On the first glance your code looks fine.


The code fail through this:

if not self.player.isPlayingVideo():
   self.play()
It will keep firing the code as it will not check the video to see if the link is dead. I want to call the play function once at a time when the video start to fail as I want to fetch the data from column stream2_url and stream3_url to play the video once at a time before calling play function again.

Do you know how I can call the play function once at a time without keep firing?
Ok I see what you mean. You are currently using recursion but if your videos can't play you keep on trying and will not find an end. When you know that you after you check the last url you will not find a video, I would suggest a loop. is there a way for you to check if a link is down, besides trying to play it? maybe search for the file, url or anything?
if you can check this on the forehand, try iterating until your self.count reaches the maximum, until you find a working url. if you find an url start playing the video.
Mainly I would not use the recursion
(May-03-2018, 06:36 PM)ThiefOfTime Wrote: [ -> ]Ok I see what you mean. You are currently using recursion but if your videos can't play you keep on trying and will not find an end. When you know that you after you check the last url you will not find a video, I would suggest a loop. is there a way for you to check if a link is down, besides trying to play it? maybe search for the file, url or anything?
if you can check this on the forehand, try iterating until your self.count reaches the maximum, until you find a working url. if you find an url start playing the video.
Mainly I would not use the recursion

I am glad you get what I mean. Yes I am currently using recursion. I am trying to find the link that are alive so I can start play the video.

I can change my code to use a loop instead. Can you please post an example how I can use the loop instead of recursion?
Depending on what you want to do if you do not find a functioning link. currently you keep on trying the first url.
possibility one: just one test over all urls
def play(self, item = None, windowed = False):
    print "you are working on play.........................."
    max_number_urls = 3
    bool = True
    url = ''
    if not self.player.isPlayingVideo():
       conn = database.connect(self.profilePath + 'source.db')
       cur = conn.cursor()
       self.count += 1
       print "self.count...............................for stream_url column"
       print self.count
       try:
          while self.count <= max_number_urls:
              cur.execute('SELECT stream%i_url FROM streams WHERE channels=?' % self.count, [self.EPG_Channel])
              data = cur.fetchone()
              if data is not None:
                 # you found an url in the database
                 url = data[0]
                 if self.check_url_functioning() == True:
                       break
              self.count += 1
           if self.count == max_number_urls + 1:
              raise RuntimeError("no URL works")               
       except:
           print "All URLs down"
       conn.close()
       self.player.play(url, windowed = bool)
       self.channel_pressed = False
what you could do alternatively is keep on searching through the known urls if they will come back some day. or you may do that just (lets say) 10 times before checking the next one. then use a counter to count the repetitions. Or a timer if you want to do ist every 5 seconds :)
It look like to me you don't understand what I am trying to do. I am fetching the url from the database to play the video. If the video link is dead, I want to fetch the new video link in the database and play the video. This is what I am trying to do. I hope this is clear to you?

On your code that you wrote, it will fetch the url from the column stream1_url to play the video but the video link I am trying to play is dead so the code that you wrote did not call the play function again to fetch the new link from the column stream2_url when the video link is dead. I think you need to read my post again to try to understand what I am trying to achieve.


(May-03-2018, 08:27 PM)ThiefOfTime Wrote: [ -> ]Depending on what you want to do if you do not find a functioning link. currently you keep on trying the first url.
possibility one: just one test over all urls
def play(self, item = None, windowed = False):
    print "you are working on play.........................."
    max_number_urls = 3
    bool = True
    url = ''
    if not self.player.isPlayingVideo():
       conn = database.connect(self.profilePath + 'source.db')
       cur = conn.cursor()
       self.count += 1
       print "self.count...............................for stream_url column"
       print self.count
       try:
          while self.count <= max_number_urls:
              cur.execute('SELECT stream%i_url FROM streams WHERE channels=?' % self.count, [self.EPG_Channel])
              data = cur.fetchone()
              if data is not None:
                 # you found an url in the database
                 url = data[0]
                 if self.check_url_functioning() == True:
                       break
              self.count += 1
           if self.count == max_number_urls + 1:
              raise RuntimeError("no URL works")               
       except:
           print "All URLs down"
       conn.close()
       self.player.play(url, windowed = bool)
       self.channel_pressed = False
what you could do alternatively is keep on searching through the known urls if they will come back some day. or you may do that just (lets say) 10 times before checking the next one. then use a counter to count the repetitions. Or a timer if you want to do ist every 5 seconds :)
It was clear to me. I'm not sure why you want to call the play function again, if the url is dead? If you want to do it that way add another counter to let your recursion end at some point
There should be a way to check if the url is dead without starting to play the video, that is why I inserted:
if self.check_url_functioning() == True:
                       break
This would be the way I would approach this problem. If there is no other way to check that the url is dead, please let me know