gtermors

foscam.py

Mar 17th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import sys
  2. import inspect
  3. import os.path
  4. import urllib2
  5. import ConfigParser
  6.  
  7. class FoscamCamera(object):
  8.    
  9.     def __init__(self, url='', user='', pwd=''):
  10.         self._user = user
  11.         self._pwd = pwd
  12.         self._url = url
  13.  
  14.     def readconfig(self):
  15.         config = ConfigParser.ConfigParser(allow_no_value=True)
  16.  
  17.         sourcePath = os.path.dirname(inspect.getsourcefile(self.readconfig))
  18.         foscamConfigFile = os.path.join(sourcePath,'foscamcamera.cfg')
  19.  
  20.         #Check if ConfigFile exists
  21.         if not os.path.isfile(foscamConfigFile):
  22.             configExists = False
  23.             print '\nERROR: Missing Foscam configfile\n'
  24.         else:
  25.             configExists = True
  26.  
  27.         #Read Config
  28.         if configExists:
  29.             config.read(foscamConfigFile)
  30.  
  31.             try:
  32.                 self._host = config.get('Foscam', 'Host')
  33.                 self._port = config.get('Foscam', 'Port')
  34.                 self._user = config.get('Foscam', 'User')
  35.                 self._pwd  = config.get('Foscam', 'Password')
  36.                 self._url  = self._host + ':' + self._port
  37.             except ConfigParser.NoOptionError, err:
  38.                     print '\nERROR: Could not parse config file\n'
  39.  
  40.         if self._host and self._port and self._user and self._pwd:    
  41.             configRead = True
  42.         else:
  43.             configRead = False
  44.  
  45.         return configExists and configRead
  46.    
  47.     def setURL(self, url):
  48.         self._url = url
  49.  
  50.     def url(self):
  51.         return self._url
  52.    
  53.     def setUser(self, usr):
  54.         self._user = usr
  55.  
  56.     def user(self):
  57.         return self._user
  58.  
  59.     def setPassword(self, pwd):
  60.         self._pwd = pwd
  61.  
  62.     def password(self):
  63.         return self._pwd
  64.  
  65.     def sendCommand(self, cgi, parameterDict):
  66.         url = 'http://%s/%s?user=%s&pwd=%s' % (self.url(),
  67.                                                cgi,
  68.                                                self.user(),
  69.                                                self.password())
  70.         for param in parameterDict:
  71.             url = url + '&%s=%s' % (param, parameterDict[param])
  72.  
  73.         return urllib2.urlopen(url)
  74.    
  75.     def snapshot(self):
  76.         f = self.sendCommand('snapshot.cgi', {})
  77.         return f.read()
  78.  
  79.     def snapshotToFile(self, filename):
  80.         data = self.snapshot()
  81.         open(filename, 'wb').write(data)
  82.  
  83. if __name__ == '__main__':
  84.  
  85.     print
  86.     print 'testing the Foscam camera code'
  87.     print
  88.    
  89.     foscam = FoscamCamera()
  90.  
  91.     foscam.readconfig()
  92.    
  93.     foscam.snapshotToFile('snapshotnew.jpg')
  94.    
  95.     print 'wrote snapshot'
Advertisement
Add Comment
Please, Sign In to add comment