can I time tasks in python without stopping other tasks - python

I request from the football-data.org api and i time it so i have unlimited requests but when i put this code in the main file which is the body of my website the time.sleep() stops other essential functions from executing
is there a way i can run these simultaneously without stopping the essential functions
from flask import Flask, redirect, render_template, flash, request, url_for
from pymongo import MongoClient
import http.client
import json
import time
import requests
app = Flask(__name__)
cluster = MongoClient("mongodb://localhost:27017/footballScoreDB")
db = cluster["footballScorePy"]
collection = db["footballScore"]
def get_matches(league):
status = ["FINISHED", "LIVE", "SCHEDULED"]
x = 0
while(x < 3):
uri = "https://api.football-data.org/v4/competitions/" + league + "/matches?status=" + status[x]
headers = { 'X-Auth-Token': 'api key' }
response = requests.get(uri, headers=headers)
matches = response.json()["matches"]
length = response.json()["resultSet"]["count"]
i=0
collection.delete_many({"league": league , "status": status[x]})
while(i != length):
awayTeam = matches[i]["awayTeam"]["name"]
homeTeam = matches[i]["homeTeam"]["name"]
homeTeamCrest = matches[i]["homeTeam"]["crest"]
awayTeamCrest = matches[i]["awayTeam"]["crest"]
date = matches[i]["utcDate"]
winner = matches[i]["score"]["winner"]
awayScore = matches[i]["score"]["fullTime"]["away"]
awayHalfTimeScore = matches[i]["score"]["halfTime"]["away"]
homeScore = matches[i]["score"]["fullTime"]["home"]
homeHalfTimeScore = matches[i]["score"]["halfTime"]["home"]
if status == "FINISHED":
post = {
"homeTeam": homeTeam,
"awayTeam": awayTeam,
"date": date,
"homeScore": homeScore,
"awayScore": awayScore,
"homeHalfTimeScore": homeHalfTimeScore,
"awayHalfTimeScore": awayHalfTimeScore,
"winner": winner,
"status": status[x],
"league": league,
"awayTeamCrest": awayTeamCrest,
"homeTeamCrest": homeTeamCrest
}
elif status == "LIVE":
post = {
"homeTeam": homeTeam,
"awayTeam": awayTeam,
"date": "LIVE",
"homeScore": homeScore,
"awayScore": awayScore,
"homeHalfTimeScore": 0,
"awayHalfTimeScore": 0,
"winner": 0,
"status": status[x],
"league": league,
"awayTeamCrest": awayTeamCrest,
"homeTeamCrest": homeTeamCrest
}
else :
post = {
"homeTeam": homeTeam,
"awayTeam": awayTeam,
"date": date,
"homeScore": 0,
"awayScore": 0,
"homeHalfTimeScore": 0,
"awayHalfTimeScore": 0,
"winner": 0,
"status": status[x],
"league": league,
"awayTeamCrest": awayTeamCrest,
"homeTeamCrest": homeTeamCrest
}
if collection.find_one(post) == False :
collection.insert_one(post)
i+=1
x+=1
#app.route("/")
def home():
return "testing"
if __name__ == "__main__":
app.run(debug=True, port="4000")
while True:
get_matches("PL")
get_matches("SA")
get_matches("DED")
time.sleep(60)

You can run that request fn in a separate thread (spawn some threads and call get_matches fns in those) , and have Flask's server running on the main one. From here you can get that API requesting thread to just store it's findings in the MongoDB, and use that in your Flask endpoints... There might be some issue like: Say If I call an endpoint when the requesting isn't done yet, obviously you can just check mongo to see if the data is there. I recommend this recourse: Flask Python MongoDB

Related

Not updating my google sheet just getting: { "sheet1": { "id": 2 } }?

I've posted questions here before for this code, but every time we get past one error I get a new one. I'm not getting an error this time but it's still not updating my google sheet is there an error I'm missing or is this a problem with the Sheety API? Also, I'm coding on PyCharm using a Mac🖥 and the code is from lecture 333, of 100 days of code by Dr. Angela Yu. (don't know if that helps)
#------------New_code------------#
import requests
from datetime import datetime
GENDER = "male"
WEIGHT_KG = 58.740212
HEIGHT_CM = 177.8
AGE = 13
APP_ID = "be2*****"
API_KEY = "4fa82da*************************"
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
sheet_endpoint = "https://api.sheety.co/0a5644021c9c3815973ccd3f25595467/myWorkouts/sheet1"
exercise_text = input("Tell me which exercises you did: ")
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}
parameters = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}
response = requests.post(exercise_endpoint, json=parameters, headers=headers)
result = response.json()
today_date = datetime.now().strftime("%d/%m/%Y")
now_time = datetime.now().strftime("%X")
bearer_headers = {
"Authorization": f"Bearer {'TOKEN'}"
}
for exercise in result["exercises"]:
sheet_inputs = {
"workout": {
"date": today_date,
"time": now_time,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
}
}
sheet_response = requests.post(sheet_endpoint, json={"sheet1": sheet_inputs}, headers=bearer_headers)
print(sheet_response.text)
#------------New_output------------#
Tell me which exercises you did: (I entered: i ran 3 miles)
{
"sheet1": {
"id": 2
}
}
Process finished with exit code 0

Integrate security hub findings with slack using python lambda

I am trying to integrate AWS security hub findings with slack using python lambda function. For this I am following the github code written in node.js - https://github.com/aws-samples/aws-security-hub-workshop/blob/master/lambda/sechub-to-slack.js
By referring above code, I am trying to develop same lambda function using python3.8 (To be honest not having that much expertise in python) but I'm getting attribute error as below;
AttributeError ->
Response
{
"errorMessage": "'dict' object has no attribute 'detail'",
"errorType": "AttributeError",
"requestId": "d9412001-715f-46b7-b8a7-3c9aa5a522a8",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 103, in lambda_handler\n processEvent(event)\n",
" File \"/var/task/lambda_function.py\", line 23, in processEvent\n finding = event.detail.findings[0].Types[0]\n"
]
}
Code ->
import json
import urllib
from urllib.parse import urlparse, parse_qs, urlencode
from urllib.request import Request, urlopen
webhook_url = "https://hooks.slack.com/services/******"
slack_channel = "project-lambda"
def postMessage(message):
body = json.dumps(message).encode('utf-8')
headers = {'Content-Type': 'application/json'}
req = urllib.request.Request(webhook_url, body, headers)
resp = urllib.request.urlopen(req)
response = resp.read()
print (response)
def processEvent(event):
#message = event
consoleUrl = "https://console.aws.amazon.com/securityhub"
finding = event.detail.findings[0].Types[0]
findingDescription = event.detail.findings[0].Description
findingTime = event.detail.findings[0].UpdatedAt
# findingTimeEpoch = math.floor(new Date(findingTime) / 1000)
findingTitle = event.detail.findings[0].Title
account = event.detail.findings[0].AwsAccountId
region = event.detail.findings[0].Resources[0].Region
type = event.detail.findings[0].Resources[0].Type
resourceId = event.detail.findings[0].Resources[0].Id
messageId = event.detail.findings[0].Resources[0].Id
# lastSeen = "<!date^${findingTimeEpoch}^{date} at {time} | ${findingTime}>"
color = "#7CD197"
severity = ""
if 1 <= event.detail.findings[0].Severity.Normalized and event.detail.findings[0].Severity.Normalized <= 39:
severity = 'LOW'
color = '#879596'
elif 40 <= event.detail.findings[0].Severity.Normalized and event.detail.findings[0].Severity.Normalized <= 69:
severity = 'MEDIUM'
color = '#ed7211'
elif 70 <= event.detail.findings[0].Severity.Normalized and event.detail.findings[0].Severity.Normalized <= 89:
severity = 'HIGH'
color = '#ed7211'
elif 90 <= event.detail.findings[0].Severity.Normalized and event.detail.findings[0].Severity.Normalized <= 100:
severity = 'CRITICAL'
color = '#ff0209'
else:
severity = 'INFORMATIONAL'
color = '#007cbc'
attachment = [{
"fallback": finding + " - {consoleUrl}/home?region=" + "{region}#/findings?search=id%3D{messageId}",
"pretext": "*AWS SecurityHub finding in {region} for Account: {account}*",
"title": finding,
"title_link": "{consoleUrl}/home?region={region}#/research",
"text": findingDescription,
"fields": [{
"title": "Title",
"value": findingTitle,
"short": True
},
{
"title": "Severity",
"value": severity,
"short": True
},
{
"title": "Region",
"value": region,
"short": True
},
{
"title": "Resource Type",
"value": type,
"short": True
},
{
"title": "Resource ID",
"value": resourceId,
"short": True
},
{
"title": "Last Seen",
"value": None,
"short": True
}
],
"mrkdwn_in": ["pretext"],
"color": color
}]
slackMessage = {
"channel": slack_channel,
"text": "",
"attachments": attachment,
"username": 'SecurityHub',
"mrkdwn": True,
"icon_url": 'https://raw.githubusercontent.com/aws-samples/amazon-securityhub-to-slack/master/images/gd_logo.png'
}
postMessage(slackMessage)
def lambda_handler(event, context):
processEvent(event)
Am I missing something in event handler?

add params to url in python

I would like to pass two parameters to my url(status code & parent id). The json response of the url request is such :
{
"page": 1,
"per_page": 10,
"total": 35,
"total_pages": 4,
"data": [
{
"id": 11,
"timestamp": 1565193225660,
"status": "RUNNING",
"operatingParams": {
"rotorSpeed": 2363,
"slack": 63.07,
"rootThreshold": 0
},
"asset": {
"id": 4,
"alias": "Secondary Rotor"
},
"parent": {
"id": 2,
"alias": "Main Rotor Shaft"
}
}]
I would like to know how to pass the two parameters in the url. Passing ?status=RUNNING gives the response of all the devices which have running as status (thats pretty straightforward).
For now I have tried this:
import requests
resp = requests.get('https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING')
q = resp.json()
print(q)
How should I pass in parentid=2, so it returns a response with devices which have their parent id=2.Thank you.
It's plainly documented under "Passing Parameters in URLs" in the Requests docs.
resp = requests.get(
'https://jsonmock.hackerrank.com/api/iot_devices/search',
params={
'status': 'RUNNING',
'parentid': 2,
},
)
To add a second get parameter, use the & separator :
import requests
resp = requests.get('https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING&parentid=2')
q = resp.json()
print(q)
If you want to send data via get request the process is straight forward note how different values are seperated with '&'.
url?name1=value1&name2=value2
If you are using Flask for backend then you can access these parameters like.
para1=request.args.get("name1")
para2=request.args.get("name2")
On the front end you can use ajax to send the request
var xhttp=new XMLHttpRequest();
var url="url?name1=value1&name2=value2"
xhttp.open("GET",url,true)
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
console.log(this.responseText);
}
};
xhttp.send();

Python Django App Moving Through Pagination In Api Response

I made an API call to a restful service that by default comes with 100 paginated pages.
I have been working with my response and even built search functions into the app but I cannot figure out how to use the pagination.
Would someone please explain how I should move through the pagination of the response?
From the api
The pagination info is included in the Link header. It is important
to follow these Link header values instead of constructing your own
URLs.
And this is the current view I am working with. I have a plus and minus button in my search controls. I was hoping to allow people to move forward or backwards through the pagination with these controls. Is this not just a url I need to hit to move forward?
When I add rel="next" to the end of the url it just reloads the same results.
here is my view for reference.
def graphs(request):
mtg_url = 'https://api.deckbrew.com/mtg/cards'
if request.user.is_authenticated():
if request.method == 'POST':
data = []
mtg_colors = request.POST.get('colors')
mtg_type = request.POST.get('type')
mtg_supertypes = request.POST.get('supertypes')
mtg_subtypes = request.POST.get('subtypes')
mtg_name = request.POST.get('mtgName')
type_search = '?color='+mtg_colors+'&type='+mtg_type+'&supertypes='+mtg_supertypes+'&subtypes='+mtg_subtypes
nameSearch = '/typeahead?q='+mtg_name
if mtg_name == "":
r = requests.get(mtg_url+type_search)
print(r)
else:
r = requests.get(mtg_url+nameSearch)
jsonList = r.json()
for cards in jsonList:
data.append(cards)
print(data)
return render(request, 'graphs/graphs.html', {'data': data})
else:
data = []
r = requests.get(mtg_url)
jsonList = r.json()
for cards in jsonList:
data.append(cards)
return render(request, 'graphs/graphs.html', {'data': data})
else:
return redirect('index')
Response in JSON
[
{
"name": "_____",
"id": "_____",
"url": "https://api.deckbrew.com/mtg/cards/_____",
"store_url": "http://store.tcgplayer.com/magic/unhinged/_____?partner=DECKBREW",
"types": [
"creature"
],
"subtypes": [
"shapeshifter"
],
"colors": [
"blue"
],
"cmc": 2,
"cost": "{1}{U}",
"text": "{1}: This card's name becomes the name of your choice. Play this ability anywhere, anytime.",
"power": "1",
"toughness": "1",
"formats": {},
"editions": [
{
"set": "Unhinged",
"set_id": "UNH",
"rarity": "uncommon",
"artist": "Ron Spears",
"multiverse_id": 74252,
"flavor": "{1}: This card's flavor text becomes the flavor text of your choice. (This ability doesn't work because it's flavor text, not rules text (but neither does this reminder text, so you figure it out).)",
"number": "23",
"layout": "normal",
"price": {
"low": 0,
"median": 0,
"high": 0
},
"url": "https://api.deckbrew.com/mtg/cards?multiverseid=74252",
"image_url": "https://image.deckbrew.com/mtg/multiverseid/74252.jpg",
"set_url": "https://api.deckbrew.com/mtg/sets/UNH",
"store_url": "http://store.tcgplayer.com/magic/unhinged/_____?partner=DECKBREW"
}
]
},

Using timestamp, json e python

This script makes the requisition fligts google every 1h, using time.sleep (3600) and generates a txt file with all phrases
he rolled over a day and a half.
I want do this properly using TIMESTAMP. Someone can help me?
import urllib
import urllib2
import json
import time
while 1:
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyA3758yM14aTX7aI9_v5AvKI2X1m56HszI"
code = {
"request": {
"passengers": {
"adultCount": 1,
"childCount": 1
},
"slice": [
{
"origin": "SSA",
"destination": "GRU",
"date": "2015-06-19",
"permittedDepartureTime":
{
"kind": "qpxexpress#timeOfDayRange",
"earliestTime": "22:00",
"latestTime": "23:00"
}
},
{
"origin": "GRU",
"destination": "SSA",
"date": "2015-06-30",
"permittedDepartureTime":
{
"kind": "qpxexpress#timeOfDayRange",
"earliestTime": "05:00",
"latestTime": "12:00"
}
}
],
"solutions": 3
}
}
#hoje = "%s" % (time.strftime("%Y_%m_%d"))
jsonreq = json.dumps(code, encoding = 'utf-8')
req = urllib2.Request(url, jsonreq, {'Content-Type': 'application/json'})
flight = urllib2.urlopen(req)
response = flight.read()
flight.close()
#print(response)
print("----------------")
texto=(response)
v_file= open("ssaGRU.json" ,"a")
#hora = time.strftime("%H:%M:%S %Z")
v_file.write(texto)
#v_file.write("[%s] Hora do json.\r\n" % (hora))
v_file.close()
time.sleep(15)
current_time = time.strftime("%H:%M", time.localtime())
v_file = open("ssaGRU.json", "a")
v_file.write(str(current_time) + ': ')
v_file.write(texto + '\n')
v_file.close()
This will print your current time before every line inputted, and adds a an empty line at the end so your data from different times doesn't stay on one line.
You can also add %m.%d.%y to current_time if you need. In case texto isn't a string, make sure you add str(texto).

Categories