Python Forum
Help with calling the function as the code keep firing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with calling the function as the code keep firing
#1
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.
Reply
#2
does anyone know?????????????
Reply
#3
Where does you code fail? Do you have an Exception and could post it? On the first glance your code looks fine.
Reply
#4
(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?
Reply
#5
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
Reply
#6
(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?
Reply
#7
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 :)
Reply
#8
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 :)
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,438 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 821 Jun-07-2023, 08:55 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 2,055 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,823 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  [Solved] TypeError when calling function Laplace12 2 2,891 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 1,998 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  calling a function and argument in an input phillup7 3 2,617 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,250 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Calling DLL function OptoBruh 0 1,594 Nov-15-2019, 11:51 PM
Last Post: OptoBruh
  Duplicate output when calling a custom function from the same file? road2knowledge 2 2,389 May-10-2019, 07:58 AM
Last Post: road2knowledge

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020