Created
November 14, 2018 14:41
-
-
Save djm300/0817b47ceb052fc8ad3fe6245e5afc55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import xml.etree.ElementTree as ET | |
import requests | |
import re | |
import sys | |
from datetime import date | |
from dateutil.parser import parse | |
from dateutil.rrule import rrule, DAILY | |
# suppresses SSL warning | |
from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
config = { | |
"username": "<USERNAME>", | |
"password": "<PASSWORD>", | |
"protocol": "https", | |
"ip": "<IP>", | |
"url": "/ISAPI/ContentMgmt/search", | |
"resultkeyword": ".//{http://www.hikvision.com/ver20/XMLSchema}playbackURI", | |
"xml": """ | |
<CMSearchDescription> | |
<searchID>C837920F-0F00-0001-A7BF-10BE33B0E3C0</searchID> | |
<trackIDList> | |
<trackID>103</trackID> | |
</trackIDList> | |
<timeSpanList> | |
<timeSpan> | |
<startTime>DATET00:00:01Z</startTime> | |
<endTime>DATET23:59:59Z</endTime> | |
</timeSpan> | |
</timeSpanList> | |
<contentTypeList> | |
<contentType>metadata</contentType> | |
</contentTypeList> | |
<maxResults>1000</maxResults> | |
<searchResultPostion>0</searchResultPostion> | |
<metadataList> | |
<metadataDescriptor>//recordType.meta.std-cgi.com/CMR</metadataDescriptor> | |
</metadataList> | |
</CMSearchDescription> | |
""", | |
"datepattern":".*?starttime=(\d\d\d\d\d\d\d\d)T(.*)Z&endtime.*" | |
} | |
ip_no_auth = config.get("protocol") + "://" + config.get("ip") | |
full_url = ip_no_auth + config.get("url") | |
s = requests.Session() | |
s.auth=(config.get("username"), config.get("password")) | |
def extractDateFromPlaybackURI(playbackuri): | |
date_search = re.search(config.get("datepattern"),playbackuri,re.IGNORECASE) | |
if date_search: | |
return date_search.group(1) + "-" + date_search.group(2) | |
else: | |
return "NOTFOUND" | |
def getImage(playbackuri, session): | |
r = session.get(playbackuri, allow_redirects=False) | |
head = r.headers | |
if not ('jpeg' in head.get('content-type').lower()): | |
print "Not an image file!" | |
return | |
filename = extractDateFromPlaybackURI(playbackuri) | |
print " Getting image: " + playbackuri + " -----> " + filename + ".jpg" | |
open(filename+".jpg", 'wb').write(r.content) | |
def getXML(requestxml): | |
r = s.post( full_url, data=requestxml, verify=False) | |
if r.status_code == 200: | |
print "--> Request OK" | |
loopoverXML(ET.fromstring(r.content)) | |
else: | |
print "--> Request NOK" | |
def loopoverXML(responsexml): | |
snapshots = [] | |
for item in responsexml.findall(config.get("resultkeyword")): | |
snapshots.append(item.text) | |
print "Downloading..." | |
for snap in snapshots: | |
print "Item " + str(snapshots.index(snap)+1) + "/" + str(len(snapshots)) | |
getImage(snap,s) | |
def getDates(startdate): | |
print "Input start date is "+ startdate.strftime('%Y%m%d') | |
# check if we need multiple runs or just one for today | |
if startdate == date.today(): | |
print " Collecting images only for today" | |
startdatestring = startdate.strftime('%Y%m%d') | |
requestxml = config.get("xml") | |
# replace date in template XML with actual date | |
requestxml = requestxml.replace("DATE",startdatestring) | |
getXML(requestxml) | |
else: | |
startdatestring = startdate.strftime('%Y%m%d') | |
print " Collecting images from "+startdatestring + " to " + date.today().strftime('%Y%m%d') | |
# loop over every date between start date and today | |
for dt in rrule(DAILY, dtstart=startdate, until=date.today()): | |
requestxml = config.get("xml") | |
# replace date | |
curdatestring = dt.strftime('%Y%m%d') | |
requestxml = requestxml.replace("DATE",curdatestring) | |
getXML(requestxml) | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
# If argument given - and this exists - we will retrieve all images from that day until today per 24 hours (limit of hikvision) | |
arg_start_date = parse(sys.argv[1]) | |
getDates(arg_start_date) | |
else: | |
# Default to last day | |
getDates(date.today()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment