Silver18
Silver18

Reputation: 175

How to poll a webpage that keeps updating?

I'm trying to integrate my telegram bot with my webcam (DLINK DCS-942LB).

Using NIPCA standard (Network IP Camera Application Programming Interface) I managed to solve quite everything. I'm now working on a polling mechanism.

The basic should be:

The problem is: the notify_stream.cgi page keeps updating every 1 second adding events.

I am not able to poll the notify_stream.cgi as I have requests hanging (doesn't get a response):

This can be reproduce with a simple script:

import requests

myurl = "http://CAMERA_IP:CAMERA_PORT/config/notify_stream.cgi"
response = requests.get(myurl, auth=("USERNAME", "PASSWORD"))  

This results in requests hanging until I stop it manually.

Is it possible to keep listening the notify_stream.cgi and passing new lines to a function?

Upvotes: 0

Views: 703

Answers (1)

Silver18
Silver18

Reputation: 175

Thanks to the comment received, using session and strem works fine. Here is the code:

import requests

def getwebcameventstream(webcam_url, webcam_username, webcam_password):
    requestsession = requests.Session()
    
    eventhandler = ["first_evet", "second_event", "third_event"]
    
    with requestsession.get(webcam_url, auth=(webcam_username, webcam_password), stream=True) as webcam_response:
        for event in webcam_response.iter_lines():
            if event in eventhandler:
                handlewebcamalarm(event)
                    
def handlewebcamalarm(event):
    print ("New event received :" + str(event))

url = 'http://CAMERA_IP:CAMERA_PORT/config/notify_stream.cgi'
username="myusername"
password="mypassword"
getwebcamstream(url, username, password)

Upvotes: 1

Related Questions