]> www.vanbest.org Git - motioneye-debian/commitdiff
webhook: added support for transmitting data as form and json
authorCalin Crisan <ccrisan@gmail.com>
Thu, 26 Nov 2015 19:32:36 +0000 (21:32 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Thu, 26 Nov 2015 19:32:36 +0000 (21:32 +0200)
motioneye/templates/main.html
motioneye/webhook.py

index 6daef16d8aca45d6b65af676ad7e32219ad452e3..9187ef3cb2ffe534c6d44657a23ee1df5d9ad8fe 100644 (file)
                     <tr class="settings-item advanced-setting" required="true" strip="true">
                         <td class="settings-item-label"><span class="settings-item-label">Image File Name</span></td>
                         <td class="settings-item-value"><input type="text" class="styled still-images camera-config" id="imageFileNameEntry" placeholder="file name pattern..."></td>
-                        <td><span class="help-mark" title="sets the name pattern for the image (JPEG) files; the following special tokens are accepted: %Y = year, %m = month, %d = date, %H = hour, %M = minute, %S = second, %q = frame number, %v = event number / = subfolder">?</span></td>
+                        <td><span class="help-mark" title="sets the name pattern for the image (JPEG) files; the following special tokens are accepted: %Y = year, %m = month, %d = date, %H = hour, %M = minute, %S = second, %q = frame number, %v = event number, / = subfolder">?</span></td>
                     </tr>
                     <tr class="settings-item advanced-setting" min="0" max="100" snap="2" ticksnum="5" decimals="0" unit="%">
                         <td class="settings-item-label"><span class="settings-item-label">Image Quality</span></td>
                 </div>
                 <table class="settings">
                     <tr class="settings-item advanced-setting">
-                        <td class="settings-item-label"><span class="settings-item-label">Email Notifications</span></td>
+                        <td class="settings-item-label"><span class="settings-item-label">Send An Email</span></td>
                         <td class="settings-item-value"><input type="checkbox" class="styled notifications camera-config" id="emailNotificationsSwitch"></td>
-                        <td><span class="help-mark" title="enable this if you want to receive email notifications whenever a motion event is detected">?</span></td>
+                        <td><span class="help-mark" title="enable this if you want to receive an email whenever a motion event is detected">?</span></td>
                     </tr>
                     <tr class="settings-item advanced-setting" required="true" strip="true">
                         <td class="settings-item-label"><span class="settings-item-label">Email Addresses</span></td>
                         <td colspan="100"><div class="settings-item-separator"></div></td>
                     </tr>
                     <tr class="settings-item advanced-setting">
-                        <td class="settings-item-label"><span class="settings-item-label">Web Hook Notifications</span></td>
+                        <td class="settings-item-label"><span class="settings-item-label">Call A Web Hook</span></td>
                         <td class="settings-item-value"><input type="checkbox" class="styled notifications camera-config" id="webHookNotificationsSwitch"></td>
                         <td><span class="help-mark" title="enable this if you want a URL to be requested whenever a motion event is detected">?</span></td>
                     </tr>
                         <td class="settings-item-value">
                             <select class="styled notifications camera-config" id="webHookHttpMethodSelect">
                                 <option value="GET">GET</option>
-                                <option value="POST">POST</option>
+                                <option value="POST">POST (query)</option>
+                                <option value="POSTf">POST (form)</option>
+                                <option value="POSTj">POST (json)</option>
                             </select>
                         </td>
-                        <td><span class="help-mark" title="the HTTP method to use when requesting the web hook URL">?</span></td>
+                        <td><span class="help-mark" title="the HTTP method to use when requesting the web hook URL (the given URL-encoded parameters will in fact be transmitted as indicated here)">?</span></td>
                     </tr>
                     <tr class="settings-item advanced-setting">
                         <td colspan="100"><div class="settings-item-separator"></div></td>
index 2e7988a5dd8673e49182e0e6987b627b0325de01..d38fb661e3ebe41c7ef3be226bf57b3d1efed4e0 100644 (file)
@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 
+import json
 import logging
 import urllib2
 import urlparse
@@ -41,14 +42,31 @@ def main(parser, args):
     logging.debug('method = %s' % options.method)
     logging.debug('url = %s' % options.url)
     
+    headers = {}    
+    parts = urlparse.urlparse(options.url)
+    url = options.url
+    data = None
+
     if options.method == 'POST':
-        parts = urlparse.urlparse(options.url)
+        headers['Content-Type'] = 'text/plain'
+        data = ''
+
+    elif options.method == 'POSTf': # form url-encoded
+        headers['Content-Type'] = 'application/x-www-form-urlencoded'
         data = parts.query
+        url = options.url.split('?')[0]
+
+    elif options.method == 'POSTj': # json
+        headers['Content-Type'] = 'application/json'
+        data = urlparse.parse_qs(parts.query)
+        data = {k: v[0] for (k, v) in data.iteritems()}
+        data = json.dumps(data)
+        url = options.url.split('?')[0]
 
-    else:
-        data = None
+    else: # GET
+        pass
 
-    request = urllib2.Request(options.url, data)
+    request = urllib2.Request(url, data, headers=headers)
     try:
         urllib2.urlopen(request, timeout=settings.REMOTE_REQUEST_TIMEOUT)
         logging.debug('webhook successfully called')