#!/usr/bin/env python # -*- coding: utf-8 -*- import pycurl from urllib import urlencode from StringIO import StringIO import simplejson import hashlib class uberMetrics: def __init__(self): self.credentials = {'session_id':"",'auth_tokens': "",'user_id':""} self.apiurl = 'http://api.ubermetrics-technologies.com' def login(self,uname, pwd, hashed=False): # uname = Login Username to delta.ubermetrics-technologies.com # pwd = the required password for the user or the md5 hash of this password if # hashed = True # First step: send an login_request with username # result should be the user_id, user_seed and auth_seed # see: http://doc.ubermetrics-technologies.com/api-reference/user-account-calls/#login_request buffer = StringIO() c = pycurl.Curl() c.setopt(c.URL,self.apiurl) #c.setopt(c.WRITEDATA, buffer) c.setopt(c.WRITEFUNCTION, buffer.write) post_data = {'action': 'login_request', 'user_name': uname} postfields = urlencode(post_data) c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() data=simplejson.loads(buffer.getvalue()) buffer.close() # if request was not successful, return with False if data['success']=="false": return False # Second step: try to login with user_id and the hashed password, user_seed and auth_seed # see: http://doc.ubermetrics-technologies.com/api-reference/user-account-calls/#login_request user_id=data['data']['user_id'] pwd_hash=hashlib.md5(pwd).hexdigest() mypwd=hashlib.md5(hashlib.md5(pwd_hash+data['data']['user_seed']).hexdigest()+data['data']['auth_seed']).hexdigest() buffer = StringIO() c = pycurl.Curl() c.setopt(c.URL, 'http://api.ubermetrics-technologies.com') #c.setopt(c.WRITEDATA, buffer) c.setopt(c.WRITEFUNCTION, buffer.write) post_data = {'action': 'login', 'user_id': user_id, 'pass':mypwd} postfields = urlencode(post_data) c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() data=simplejson.loads(buffer.getvalue()) buffer.close() # if login was successful, save credentials and return True if data['message']=="login_successful": self.credentials = {'session_id':data['data']['session_id'],'auth_tokens':data['data']['auth_tokens'],'user_id':user_id} return True else: return False def get_search_id_by_name(self,searchname): # Find the search_id of a given search name # has to be an exact match # returns -1 if no search with the given name is found # you have to be logged in if not len(self.credentials['auth_tokens'])>0: print "Buh!" return -1 buffer = StringIO() c = pycurl.Curl() c.setopt(c.URL, self.apiurl) #c.setopt(c.WRITEDATA, buffer) c.setopt(c.WRITEFUNCTION, buffer.write) post_data = {'action': 'get_searches',\ 'user_id': self.credentials['user_id'],\ 'session_id':self.credentials['session_id'],\ 'auth_token':self.credentials['auth_tokens'].pop()} postfields = urlencode(post_data) c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() data=simplejson.loads(buffer.getvalue()) # Check if one of the results has the given searchname # return the uberMetrics search_id for this search for element in data['data']: if searchname==element['name']: return element['id'] # if no match is found, return -1 return -1 def get_searches(self): # Return all saved searches for the active user # you have to be logged in if not len(self.credentials['auth_tokens'])>0: return -1 buffer = StringIO() c = pycurl.Curl() c.setopt(c.URL, self.apiurl) #c.setopt(c.WRITEDATA, buffer) c.setopt(c.WRITEFUNCTION, buffer.write) post_data = {'action': 'get_searches',\ 'user_id': self.credentials['user_id'],\ 'session_id':self.credentials['session_id'],\ 'auth_token':self.credentials['auth_tokens'].pop()} # Form data must be provided already urlencoded. postfields = urlencode(post_data) # Sets request method to POST, # Content-Type header to application/x-www-form-urlencoded # and data to send in request body. c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() data=simplejson.loads(buffer.getvalue()) return data['data'] def get_search_results(self,search_id,t_min="2015-01-01 00:00:00",t_max="2099-12-31 23:59:59",order_by="time",ascending=0,read=-1,marked=-1,critical=-1,mailed=-1,sentiment="",group_type="",language="",pointer="",ipp=10000): # Return all the elements of a given search (by search_id) # for parameters please refer to http://doc.ubermetrics-technologies.com/api-reference/search-result-calls/#get_search_result # "action" => "get_search_result", # "version" => 4, # "user_id" => $login_id, # "session_id" => $session_id, # "auth_token" => array_pop($auth_tokens), # "element_id" => "#all", //results for all projects # "t_min" => "2013-08-01 00:00:00", # "t_max" => "2013-08-01 23:59:59", # "order_by" => "time", # "ascending" => 0, //newest mentions first # "ipp" => 2, //give at most 2 mentions in result # "pointer" => "", //show first page # "read" => -1, //no filtering by read status # "marked" => -1, //no filtering by marked status # "critical" => -1, //no filtering by critical status # "mailed" => -1, //no filtering by mailed status # "sentiment" => "", //no filtering by sentiment # "group_type" => "", //no filtering by media segment # "language" => "" //no filtering by language # you have to be logged in if not len(self.credentials['auth_tokens'])>0: return -1 buffer = StringIO() c = pycurl.Curl() c.setopt(c.URL, self.apiurl) #c.setopt(c.WRITEDATA, buffer) c.setopt(c.WRITEFUNCTION, buffer.write) post_data = {'action': 'get_search_result',\ 'user_id': self.credentials['user_id'],\ 'session_id':self.credentials['session_id'],\ 'auth_token':self.credentials['auth_tokens'].pop(),\ 'element_id': search_id,\ 't_min': t_min,\ 't_max': t_max,\ 'order_by': order_by,\ 'ascending' : ascending,\ 'pointer': pointer,\ 'read': read,\ 'marked': marked,\ 'critical': critical,\ 'mailed': mailed,\ 'sentiment': sentiment,\ 'group_type': group_type,\ 'language': language,\ 'ipp': ipp } postfields = urlencode(post_data) c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() data=simplejson.loads(buffer.getvalue()) return data['data'] def extract_content_from_html(self,url): # Return all saved searches for the active user # you have to be logged in if not len(self.credentials['auth_tokens'])>0: return -1 storage = StringIO() c = pycurl.Curl() c.setopt(c.URL, url) c.setopt(c.WRITEFUNCTION, storage.write) c.perform() c.close() content = storage.getvalue() buffer = StringIO() c = pycurl.Curl() c.setopt(c.URL, self.apiurl) #c.setopt(c.WRITEDATA, buffer) c.setopt(c.WRITEFUNCTION, buffer.write) post_data = {'action': 'extract_content_from_html',\ 'user_id': self.credentials['user_id'],\ 'session_id':self.credentials['session_id'],\ 'auth_token':self.credentials['auth_tokens'].pop(), 'url': url, 'html':content} # Form data must be provided already urlencoded. postfields = urlencode(post_data) # Sets request method to POST, # Content-Type header to application/x-www-form-urlencoded # and data to send in request body. c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() data=simplejson.loads(buffer.getvalue()) return data