Example #1
0
def kashflowApiCall(endpoint='', verb='GET', data=''):
    """ perform a http request to app.kashflow.com and return the response as a dict"""
    # put incoming verb into uppercase chars
    verb = verb.upper()

    # get session key if not available
    if not session.get('kashflowToken'):
        tmpToken = getKashflowTemporaryToken()
        session['kashflowToken'] = getKashflowSessionToken(tmpToken)

    # add auth header if in session
    headers = {'Content-Type': 'application/json'}
    if session.get('kashflowToken'):
        headers['Authorization'] = 'KfToken {}'.format(
            session['kashflowToken'])

    url = app.config['KASHFLOW_API_URL'] + '/{}'.format(endpoint)

    if verb == 'POST':
        r = requests.post(url, headers=headers, data=data)
    elif verb == 'PUT':
        r = requests.put(url, headers=headers, data=data)
    elif verb == 'UPDATE':
        r = requests.update(url, headers=headers, data=data)
    elif verb == 'DELETE':
        r = requests.delete(url, headers=headers, data=data)
    else:
        r = requests.get(url, headers=headers)

    jsonBody = r.json()
    return jsonBody
Example #2
0
    def request(self, method, request="?", headers=None, postData={}, domain="discordapp.com", files={}):
        while(time.time() - self.lastRequest < 1):
            time.sleep(0.025)

        url = 'https://{}/api/{}'.format(domain, request)
        response = None

        if(method.lower() in ["post", "get", "delete", "head", "options", "put"]):
            self.lastRequest = time.time()
            if(method == "POST"):
                response = requests.post(url, files=files, json=postData, headers=headers)
            elif(method == "GET"):
                response = requests.get(url, postData, headers=headers)
            elif(method == "PUT"):
                response = requests.put(url, postData, headers=headers)
            elif(method == "DELETE"):
                response = requests.delete(url, postData, headers=headers)
            elif(method == "OPTIONS"):
                response = requests.options(url)
            elif(method == "HEAD"):
                response = requests.head(url)
            elif(method == "UPDATE"):
                response = requests.update(url, postData, headers=headers)
        else:
            raise Exception("Invalid HTTP request method")

        if(response.status_code not in range(200, 206)):
            raise Exception("API responded with HTTP code  " + str(response.status_code) + "\n\n" + response.text)
        else:
            if(response.text):
                returnData = json.loads(response.text)

                return returnData
            else:
                return None
Example #3
0
 def work(self):
     self.toggle_show_progress.emit(False)
     self.toggle_show_cancel.emit(False)
     if self.method == 'get':
         resp = requests.get(self.url,
                             json=self.payload,
                             headers=self.headers,
                             timeout=TIMEOUT)
     elif self.method == 'post':
         resp = requests.post(self.url,
                              json=self.payload,
                              headers=self.headers,
                              timeout=TIMEOUT)
     elif self.method == 'update':
         resp = requests.update(self.url,
                                json=self.payload,
                                headers=self.headers,
                                timeout=TIMEOUT)
     elif self.method == 'delete':
         resp = requests.delete(self.url,
                                json=self.payload,
                                headers=self.headers,
                                timeout=TIMEOUT)
     elif self.method == 'patch':
         resp = requests.patch(self.url,
                               json=self.payload,
                               headers=self.headers,
                               timeout=TIMEOUT)
     else:
         raise ValueError("Unrecognized method: {}".format(method))
         resp = None
     return resp
	def UPDATE(self,url,params):
		try:
			response = requests.update(url=url,params=params,headers=self._headers)
		except requests.exceptions.ConnectionError:
			print("Connection refused")
			return -1
		data = response.json()
		return data
Example #5
0
def update(instance, table, sysid, data):
    """update arbitrary data of object with sys_id sysid"""
    url = 'https://%s/api/now/table/%s/%s' % (instance, table, sysid)
    resp = requests.update(url,
                           proxies=this.proxies,
                           headers=this.headers,
                           verify=False,
                           auth=this.auth)
    return resp.text
def call(api='', data='', headers='', method='GET'):
    ## headers = {'user-agent': 'my-app/0.0.1'}
    ## r = requests.get(url, headers=headers)
    ## r = requests.get(url, data={key: value})
    ## r = requests.get('http://github.com', allow_redirects=False, timeout=0.001)
    ## r.status_code
    ## r.url
    ## r.content
    ## r.text
    ## r.json() ValueError: No JSON object could be decoded.
    ## r.headers()
    ## r.cookies['example_cookie_name']
    ## r.history
    ## r.raise_for_status() ConnectionError exception
    if data == '':
        data = {}
    if headers == '':
        headers = {}
    url = API_HOST + api.strip(' ', '/')
    headers['Authorization'] = 'Basic '+API_KEY

    if method.uppercase() == 'GET':
        requests.get(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'POST':
        requests.post(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'PUT':
        requests.put(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'HEAD':
        requests.head(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'PATCH':
        requests.patch(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'UPDATE':
        requests.update(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'OPTIONS':
        requests.options(url, data=data, headers=headers, allow_redirects=False)
    elif method.uppercase() == 'DELETE':
        requests.delete(url, data=data, headers=headers, allow_redirects=False)

    try:
        r_data = r.json()
    except ValueError, e:
        return {"status": "error", "message": repr(e)}
Example #7
0
    def get_queryset(self):
        # Selects all accepted FF Requests sent by this user but not yet seen by this user
        requests = FFRequest.objects.filter(
            status=FFRequestStatusEnum.ACCEPTED.value,
            sender=self.request.user,
            has_sender_seen_accepted_view=False,
        )
        list_of_requests = list(requests)

        # remove from lobby
        if len(list_of_requests) > 0:
            self.request.user.lobby_expiration = timezone.now()
            self.request.user.save()

        # update the field before we pass it back
        # this line will udpate the requests filter and make it empty
        requests.update(has_sender_seen_accepted_view=True)

        # just return all requests which match this; should probably be 0 or 1 but we don't check
        # print(requests, list(requests), list_of_requests)
        return list_of_requests
Example #8
0
def update_rec(email, key, zoneid, logger, recid, rec):
    ''' Update DNS record '''
    headers = {
        'X-Auth-Email' : email,
        'X-Auth-Key' : key,
        'Content-Type' : 'application/json'
    }
    url = "%s/zones/%s/dns_records/%s" % (baseurl, str(zoneid), str(recid))
    payload = json.dumps(rec)
    try:
        req = requests.update(url=url, headers=headers, data=payload)
        return validate_response(req, logger)
    except:
        return False
Example #9
0
def update_rec(email, key, zoneid, logger, recid, rec):
    ''' Update DNS record '''
    headers = {
        'X-Auth-Email': email,
        'X-Auth-Key': key,
        'Content-Type': 'application/json'
    }
    url = "%s/zones/%s/dns_records/%s" % (baseurl, str(zoneid), str(recid))
    payload = json.dumps(rec)
    try:
        req = requests.update(url=url, headers=headers, data=payload)
        return validate_response(req, logger)
    except:
        return False
Example #10
0
    def request(self,
                method,
                request="?",
                headers=None,
                postData={},
                domain="discordapp.com",
                files={}):
        while (time.time() - self.lastRequest < 1):
            time.sleep(0.025)

        url = 'https://{}/api/{}'.format(domain, request)
        response = None

        if (method.lower()
                in ["post", "get", "delete", "head", "options", "put"]):
            self.lastRequest = time.time()
            if (method == "POST"):
                response = requests.post(url,
                                         files=files,
                                         json=postData,
                                         headers=headers)
            elif (method == "GET"):
                response = requests.get(url, postData, headers=headers)
            elif (method == "PUT"):
                response = requests.put(url, postData, headers=headers)
            elif (method == "DELETE"):
                response = requests.delete(url, postData, headers=headers)
            elif (method == "OPTIONS"):
                response = requests.options(url)
            elif (method == "HEAD"):
                response = requests.head(url)
            elif (method == "UPDATE"):
                response = requests.update(url, postData, headers=headers)
        else:
            raise Exception("Invalid HTTP request method")

        if (response.status_code not in range(200, 206)):
            raise Exception("API responded with HTTP code  " +
                            str(response.status_code) + "\n\n" + response.text)
        else:
            if (response.text):
                returnData = json.loads(response.text)

                return returnData
            else:
                return None
Example #11
0
 def _update(self, url):
     if not self.debug:
         return requests.update(f'{self.API}{url}', headers=self.headers)
Example #12
0
from requests import put, get, delete, update
# print(put('http://localhost:5000/todo1', data={'data': 'Remember the milk'}).json())
# print(put('http://localhost:5000/todo2', data={'data': 'Remember the meat'}).json())
# print(get('http://localhost:5000/todo1').json())

print(get('http://localhost:5000/todos').json())
# print(put('http://localhost:5000/todos/todo3', data={'task': 'Finish hw'}).json())
# print(get('http://localhost:5000/todos/todo3').json())
print(delete('http://localhost:5000/todos/todo2'))
print(update('http://localhost:5000/todos/todo2', data={'task': 'Something different'}).json())
print(get('http://localhost:5000/todos').json())
Example #13
0
 def do_UPDATE(self):
     self._set_headers()
     r = requests.update(URL + self.path, auth=kerberos_auth, verify=False)
     self.wfile.write(r.text)
Example #14
0
def processTweet(tweet):
    #print(json.dumps(tweet, indent=4, separators=(',', ': ')))
    try:
        user = tweet['user']
    except:
        print('No user!')
        return False

    #Check if user is in our spam list
    try:
        result = requests.get(panoptic_url + 'spammers?data=twitter&name=' +
                              user['screen_name']).json()['data']
    except:
        print('Cannot retrieve spammer data')
        return False
    #If user is not in spam list, continue
    if not result:
        screen_name = user['screen_name']
        name = strip_non_ascii(user['name'])
        description = strip_non_ascii(
            user['description']) if user['description'] else ''
        location = user['location'] if user['location'] else ''
        timezone = user['time_zone'] if user['time_zone'] else ''
        followers = user['followers_count'] if user['followers_count'] else 0
        friends = user['friends_count'] if user['friends_count'] else 0

        status_link = 'https://twitter.com/' + screen_name + '/status/' + tweet[
            'id_str']
        user_id = requests.post(panoptic_url + 'user',
                                data={
                                    'twitterid': user['id'],
                                    'name': name,
                                    'screenname': screen_name,
                                    'description': description,
                                    'location': location,
                                    'timezone': timezone,
                                    'followers': followers,
                                    'friends': friends,
                                    'token': panoptic_token,
                                    'data': 'twitter'
                                }).json()['data']
        if (tweet['text'].startswith('RT ') is False):  #Remove any retweets
            #Check for tweet
            result = requests.get(panoptic_url + 'tweet?tweetid=' +
                                  tweet['id_str']).json()['data']
            if not result:
                if (tweet['truncated']):
                    try:
                        text = tweet['extended_tweet']['full_text']
                        entities = tweet['extended_tweet']['entities']
                    except:
                        text = tweet['text']
                        entities = tweet['entities']
                else:
                    text = tweet['text']
                    entities = tweet['entities']

                #print(json.dumps(user['name'], indent=4, separators=(',', ': ')))
                print(
                    json.dumps(user['screen_name'],
                               indent=4,
                               separators=(',', ': ')))
                print(json.dumps(text, indent=4, separators=(',', ': ')))
                #print('')

                analysis = tb(text)
                sentiment = analysis.sentiment.polarity
                #print(sentiment)
                #Get time
                created_at = time.strptime(tweet['created_at'],
                                           "%a %b %d %H:%M:%S +0000 %Y")
                unix = time.strftime('%s', created_at)
                datetime = time.strftime('%Y-%m-%d %H:%M:00', created_at)
                #Add tweet
                favorites = tweet['favorite_count'] if tweet[
                    'favorite_count'] else 0
                retweets = tweet['retweet_count'] if tweet[
                    'retweet_count'] else 0
                try:
                    quotes = tweet['quote_count'] if tweet['quote_count'] else 0
                except:
                    quotes = 0
                try:
                    tweet_id = requests.post(panoptic_url + 'tweet',
                                             data={
                                                 'statusid': tweet['id'],
                                                 'userid': user_id,
                                                 'twitterid': user['id'],
                                                 'tweet':
                                                 strip_non_ascii(text),
                                                 'favorites': favorites,
                                                 'retweets': retweets,
                                                 'quotes': quotes,
                                                 'sentiment': sentiment,
                                                 'unix': unix,
                                                 'token': panoptic_token
                                             }).json()['data']
                except:
                    print('Posting tweet failed')
                    return False
                #If quote, add connection, process quote
                if (tweet['is_quote_status']):
                    try:
                        requests.post(
                            panoptic_url + 'connection',
                            data={
                                'userid':
                                user_id,
                                'twitterid':
                                tweet['quoted_status']['user']['id'],
                                'screenname':
                                tweet['quoted_status']['user']['screen_name'],
                                'name':
                                tweet['quoted_status']['user']['name'],
                                'tweetid':
                                tweet_id,
                                'action':
                                'quote',
                                'token':
                                panoptic_token,
                                'data':
                                'twitter'
                            })
                        processTweet(tweet['quoted_status'])
                    except:
                        pass
                #Add connections
                if entities['user_mentions']:
                    for entity in entities['user_mentions']:
                        if (tweet['in_reply_to_user_id'] == entity['id']):
                            requests.post(panoptic_url + 'connection',
                                          data={
                                              'userid':
                                              user_id,
                                              'twitterid':
                                              entity['id'],
                                              'screenname':
                                              entity['screen_name'],
                                              'name':
                                              entity['name'],
                                              'tweetid':
                                              tweet_id,
                                              'replyid':
                                              tweet['in_reply_to_status_id'],
                                              'action':
                                              'reply',
                                              'token':
                                              panoptic_token,
                                              'data':
                                              'twitter'
                                          })
                        else:
                            requests.post(panoptic_url + 'connection',
                                          data={
                                              'userid': user_id,
                                              'twitterid': entity['id'],
                                              'screenname':
                                              entity['screen_name'],
                                              'name': entity['name'],
                                              'tweetid': tweet_id,
                                              'action': 'at',
                                              'token': panoptic_token,
                                              'data': 'twitter'
                                          })

                topics = []
                if entities['hashtags']:
                    for entity in entities['hashtags']:
                        tag = '#' + entity['text'].lower()
                        if tag not in hashtags:
                            requests.post(panoptic_url + 'hashtag',
                                          data={
                                              'topic': entity['text'].lower(),
                                              'token': panoptic_token
                                          })
                            hashtags.append(tag)
                        #Post mention to api
                        requests.post(panoptic_url + 'mention',
                                      data={
                                          'datetime': datetime,
                                          'topic': entity['text'].lower(),
                                          'sentiment': sentiment,
                                          'token': panoptic_token,
                                          'data': 'twitter'
                                      })
                        #Post connection
                        requests.post(panoptic_url + 'connection',
                                      data={
                                          'userid': user_id,
                                          'hashtag': entity['text'].lower(),
                                          'tweetid': tweet_id,
                                          'action': 'mention',
                                          'token': panoptic_token,
                                          'data': 'twitter'
                                      })
                        topics.append(entity['text'])

            else:
                tweet_id = result['tweetID']
                #Update favorite, retweet, quote counts
                requests.update(panoptic_url + 'tweet',
                                data={
                                    'tweetid': tweet_id,
                                    'favorites': tweet['favorite_count'],
                                    'retweets': tweet['retweet_count'],
                                    'quotes': tweet['quote_count'],
                                    'token': panoptic_token
                                })

            return True

        #if tweet is a retweet
        else:
            try:
                #print('RETWEET')
                #print(json.dumps(tweet['retweeted_status'], indent=4, separators=(',', ': ')))
                if (tweet['retweeted_status']['truncated']):
                    try:
                        text = tweet['retweeted_status']['extended_tweet'][
                            'full_text']
                        entities = tweet['retweeted_status']['extended_tweet'][
                            'entities']
                    except:
                        text = tweet['retweeted_status']['text']
                        entities = tweet['retweeted_status']['entities']
                else:
                    text = tweet['retweeted_status']['text']
                    entities = tweet['retweeted_status']['entities']

                result = requests.get(
                    panoptic_url + 'tweet?tweetid=' +
                    tweet['retweeted_status']['id_str']).json()['data']
                if not result:
                    analysis = tb(text)
                    sentiment = analysis.sentiment.polarity
                    #print(sentiment)
                    #Get time
                    created_at = time.strptime(tweet['created_at'],
                                               "%a %b %d %H:%M:%S +0000 %Y")
                    unix = time.strftime('%s', created_at)
                    datetime = time.strftime('%Y-%m-%d %H:%M:00', created_at)

                    #Add tweet
                    tweet_id = requests.post(
                        panoptic_url + 'tweet',
                        data={
                            'statusid':
                            tweet['retweeted_status']['id'],
                            'userid':
                            user_id,
                            'twitterid':
                            tweet['retweeted_status']['user']['id'],
                            'tweet':
                            strip_non_ascii(text),
                            'favorites':
                            tweet['retweeted_status']['favorite_count'],
                            'retweets':
                            tweet['retweeted_status']['retweet_count'],
                            'quotes':
                            tweet['retweeted_status']['quote_count'],
                            'sentiment':
                            sentiment,
                            'unix':
                            unix,
                            'token':
                            panoptic_token
                        }).json()['data']
                else:
                    tweet_id = results['tweetID']
                    sentiment = results['sentiment']
                    requests.update(
                        panoptic_url + 'tweet',
                        data={
                            'tweetid':
                            tweet_id,
                            'favorites':
                            tweet['retweeted_status']['favorite_count'],
                            'retweets':
                            tweet['retweeted_status']['retweet_count'],
                            'quotes':
                            tweet['retweeted_status']['quote_count'],
                            'token':
                            panoptic_token
                        })

                requests.post(
                    panoptic_url + 'connection',
                    data={
                        'userid':
                        user_id,
                        'twitterid':
                        tweet['retweeted_status']['user']['id'],
                        'screenname':
                        tweet['retweeted_status']['user']['screen_name'],
                        'name':
                        tweet['retweeted_status']['user']['name'],
                        'tweetid':
                        tweet_id,
                        'action':
                        'retweet',
                        'token':
                        panoptic_token,
                        'data':
                        'twitter'
                    })

                topics = []
                if entities['hashtags']:
                    for entity in entities['hashtags']:
                        tag = '#' + entity['text'].lower()
                        if tag not in hashtags:
                            requests.post(panoptic_url + 'hashtag',
                                          data={
                                              'topic': entity['text'].lower(),
                                              'token': panoptic_token
                                          })
                            hashtags.append(tag)
                        #Post mention to api
                        requests.post(panoptic_url + 'mention',
                                      data={
                                          'datetime': datetime,
                                          'topic': entity['text'].lower(),
                                          'sentiment': sentiment,
                                          'token': panoptic_token,
                                          'data': 'twitter'
                                      })
                        #Post connection
                        requests.post(panoptic_url + 'connection',
                                      data={
                                          'userid': user_id,
                                          'hashtag': entity['text'].lower(),
                                          'tweetid': tweet_id,
                                          'action': 'mention',
                                          'token': panoptic_token,
                                          'data': 'twitter'
                                      })
                        topics.append(entity['text'])

            except:
                pass

    return True
Example #15
0
        long=GPS.GGA[4]
        long_ew=GPS.GGA[5]
        fix=GPS.GGA[6]
        sats=GPS.GGA[7]
        alt=GPS.GGA[9]
        return [time,fix,sats,alt,lat,lat_ns,long,long_ew]
 
g=GPS()
f=open("gps_data.csv",'w')   #Open file to log the data
f.write("name,latitude,longitude\n")   #Write the header to the top of the file
ind=0
while True:
    try:
        x=g.read()  #Read from GPS
        [t,fix,sats,alt,lat,lat_ns,long,long_ew]=g.vals() #Get the individial values
        print "Time:",t,"Fix status:",fix,"Sats in view:",sats,"Altitude",alt,"Lat:",lat,lat_ns,"Long:",long,long_ew
        s=str(t)+","+str(float(lat)/100)+","+str(float(long)/100)+"\n"   
        #f.write(s)   #Save to file
        
        f.write(s)   #Save to file
        payload = {'Time': t,'Alt': alt,'Lat': lat,'Long': long}
        requests.update(url, data=payload) # send to server
        
        time.sleep(5)
    except IndexError:
        print "Unable to read"
    except KeyboardInterrupt:
        f.close()
        print "Exiting"
        sys.exit(0)
Example #16
0
    def run(self):
        if self.use_token:
            resp = login()
            if resp:
                headers = {
                    'Authorization': 'Bearer %s' % resp.json()['access_token']
                }
                log("API _call_api loaded token.")
            else:
                return False
        else:
            headers = {}

        # Strip password out of payload for printing to QGIS logs
        clean_payload = self.payload.copy()
        if clean_payload.has_key('password'):
            clean_payload['password'] = '******'

        log('API _call_api calling {} with method "{}" and payload: {}'.format(
            self.endpoint, self.method, clean_payload))

        try:
            if self.method == 'get':
                resp = requests.get(API_URL + self.endpoint,
                                    json=self.payload,
                                    headers=headers)
            elif self.method == 'post':
                resp = requests.post(API_URL + self.endpoint,
                                     json=self.payload,
                                     headers=headers)
            elif self.method == 'update':
                resp = requests.update(API_URL + self.endpoint,
                                       json=self.payload,
                                       headers=headers)
            elif self.method == 'delete':
                resp = requests.delete(API_URL + self.endpoint,
                                       json=self.payload,
                                       headers=headers)
            elif self.method == 'patch':
                resp = requests.patch(API_URL + self.endpoint,
                                      json=self.payload,
                                      headers=headers)
            else:
                raise ValueError("Unrecognized method: {}".format(self.method))
        except requests.ConnectionError:
            mb.pushMessage("Error",
                           "Unable to connect to LDMP server.",
                           level=1,
                           duration=5)
            return False

        log('API _call_api response from "{}" request: {}'.format(
            self.method, clean_api_response(resp)))

        if resp.status_code != 200:
            resp = resp.json()
            desc, status = get_error_status(resp)
            mb.pushMessage("Error: {} (status {}).".format(desc, status),
                           level=1,
                           duration=5)
            self.resp = None
        else:
            self.resp = resp.json()