Chrome Crux API - depending when API request is run no data is populated for URLs - python-requests

I've run my code below in the morning (between 8AM EST - 11AM EST) and no data populates for my urls, however in the evening (at no specific time) data populates.
I'm using the Crux API docs here, specifically looking at daily-updates:
https://developer.chrome.com/docs/crux/api/#daily-updates
Which states:
Data is updated daily around 04:00 UTC. There is no service level
agreement for update times; it is run on a best-effort basis every
day.
More importantly it also states:
Data will not differ within the same day after it has been updated
around 04:00 UTC, repeated calls will yield the same results.
Which I feel means should have data always populating for these urls if they had populated for the previous day!
My code:
import requests
import json
x = {"formFactor":"TABLET",
"metrics":["largest_contentful_paint", "experimental_time_to_first_byte",
"first_contentful_paint", "cumulative_layout_shift",
"first_input_delay", "experimental_interaction_to_next_paint"],
"url":"https://www.happy-tests.com/id",
}
url = "https://chromeuxreport.googleapis.com/v1/records:queryRecord?key={key}"
headers = {"Content-Type": "application/json",
"Accept": "application/json"}
response = requests.post(url, json=x, headers=headers)
response_json = response.json()
print(response_json)
'error': {'code': 404, 'message': 'chrome ux report data not found',
'status': 'NOT_FOUND'}}

Related

Troubled Getting Cryptocurrency Data API in R

I've tried two cryptocurrency API package for R, to name it:
coinmarketcapr package
riingo <- included in tidyquant
I really want to get updated historical data of cryptocurrency, my goal is to predict the thing with some timeseries analysis, but with these packages I kept getting error message, for coinmarketcapr it's understandable because apparently my API subscription plan doesn't cover the historical data, but for the riingo the message shows just like this...
> riingo_crypto_latest("btcusd", resample_frequency = "10min", base_currency = NULL)
Request failed [401]. Retrying in 1.6 seconds...
Request failed [401]. Retrying in 1.5 seconds...
Error: There was an error, but riingo isn't sure why. See Tiingo msg for details.
Tiingo msg) Invalid token.
Can somebody help me? Or maybe suggesting other source for taking cryptocurrency historical data? Thank you in advance for any answer!
P.S. I've already inserted the API key, so it's not the authentication problem.
If you trying to scrape information about new listings at crypto exchanges and some more useful info, you can be interested at using this API:
https://rapidapi.com/Diver44/api/new-cryptocurrencies-listings/
Example request in R:
library(httr)
url <- "https://new-cryptocurrencies-listings.p.rapidapi.com/new_listings"
response <- VERB("GET", url, add_headers(x_rapidapi-host = 'new-cryptocurrencies-listings.p.rapidapi.com', x_rapidapi-key = 'your-api-key', '), content_type("application/octet-stream"))
content(response, "text")
It includes an endpoint with New Listings from the biggest exchanges and a very useful endpoint with information about exchanges where you can buy specific coins and prices for this coin at that exchanges. You can use this information for trading. When currency starts to list at popular exchanges like Binance, KuCoin, Huobi, etc the price increases about 20-30%

WooCommerce REST API - Get Orders using after/before parameters

I'm using the WooCommerce REST API (http://woocommerce.github.io/woocommerce-rest-api-docs/#introduction) and are able to download Customers, Orders etc successfully. I would now like to just download a list of Orders between 2 dates - I can see in the docs for Orders the following parameters:
after string Limit response to resources published after a given ISO8601 compliant date.
before string Limit response to resources published before a given ISO8601 compliant date.
I've modified the URL for the request to this:
https://mywebsite.com/wp-json/wc/v1/orders?after=2016-08-05&before=2016-08-06&page=1
but I'm getting a 400 error with this response:
{
"code": "rest_invalid_param",
"data": {
"params": {
"after": "The date you provided is invalid.",
"before": "The date you provided is invalid."
},
"status": 400
},
"message": "Invalid parameter(s): after, before"
}
The 2 dates are valid ISO8601 compliant dates as far as I can tell. Can't find an example of how this request should look so not sure where to go from here.
It turns out you need to also specify the time, e.g.
after=2016-11-20T13:57:31.2311892-04:00

TastyPie not responding to POSTs until it reaches the keepalive_timeout

This one has been baffling me for almost 24 hours now. For some reason on my linode server, tastypie isn't return a response to POSTs until it reaches the server's keepalive_timeout. It works perfectly for all GETs, PUTs, DELETEs and POSTs handled via override_urls (I'm using 0.9.11 for tastypie), but any standard POST (e.g. not handled override_urls) just sits there until the timeout limit is reached.
At first I thought it was a server config issue, but I'm now starting to suspect it's something to do with tastypie.
Relevant tastypie code:
class MissionResource(ModelResource):
game = fields.ToOneField(GameResource, 'game')
mission_bank = fields.ToOneField('goosechase.website.api.MissionBankResource', 'mission', null = True)
class Meta:
queryset = Mission.objects.all()
resource_name = "mission"
always_return_data = True
list_allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
excludes = ['hint_file_name']
filtering = {
"game" : ('exact'),
}
authentication = ApiKeyAuthentication()
authorization = Authorization()
# Added for testing various configs
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/manually-handled-post/$" %
self._meta.resource_name,
self.wrap_view('generic_post')),
]
# Test method for manually handled posts
def manually_handled_post(self, request, **kwargs):
self._meta.authentication.is_authenticated(request)
self.is_authorized(request)
return HttpResponse()
For some reason, doing a post to the "manually-handled-post/" endpoint works perfectly, as the response comes back in milliseconds, but a typical post doesn't return until it hits the server timeout. The testing code I did:
import requests
import json
# NORMAL POST ATTEMPT
response = requests.get('http://www.example.com/api/web/mission/19/?format=json&api_key=API_KEY&username=USERNAME')
event = json.loads(response.content)
del event['id']
del event['resource_uri']
response = requests.post('http://www.example.com/api/web/mission/?format=json&api_key=API_KEY&username=USERNAME', data=json.dumps(event), headers={'content-type': 'application/json'})
# DELAY UNTIL SERVER keepalive_timeout IS REACHED, THEN <Response [201]> IS RETURNED
# OVERRIDEN POST ATTEMPT
response = requests.post('http://www.example.com/api/web/mission/manually-handled-post/?format=json&api_key=API_KEY&username=USERNAME', data=json.dumps({}), headers={'content-type': 'application/json'})
# INSTANTANEOUS RESPONSE WITH <Response [200]>
I checked that there's no save or obj_create hooks that are causing the delays. I've also verified that the same issues happen on multiple model resources.
Since overriden_urls work perfectly, I'm thinking that there's a bug in tastypie for POSTs, but I haven't found anything online and in the github issues.
Has anyone ran into this before and fixed it without reducing the keepalive_timeout to 0?
EDIT 1:
I started logging with timestamps to figure out where the slowdown occurred. Turns out there was no slowdown in the code, the delay was happening after the response was returned (e.g. return http.HttpCreated(location=location)). When I forced the POST to return a status code of 202 (via http.httpAccepted), it worked.
So now it seems the issue stems from 201 status codes on my server. I have no idea why this would be happening, so if anyone can point me in the right direction I'd really appreciate it.

Google Distance Matrix Api: travel time with traffic

I am trying to get the travel time with traffic between 2 sets of lat/long coordinates. I can call the Google Distance Matrix API, but I am only getting travel time without traffic. The API notes which I have read say to use a parameter called departure_time. Here is the excerpt:
departure_time specifies the desired time of departure as seconds since midnight, January 1, 1970 UTC. The departure time may be specified by Maps for Business customers for to specify the departure_time to receive trip duration considering current traffic conditions. The departure_time must be set to within a few minutes of the current time.
I found this website to give me that time: epochconverter
However I am still getting the same travel time every time. Here is a sample request, the departure_time parameter would need to be updated (not that it matters).
https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial&departure_time=1408046331&origins=37.407585,-122.145287&destinations=37.482890,-122.150235
15 minutes is always returned.
Using "maps dot google dot com" a travel time of 19 mins is returned when traffic is taken into account.
If anyone can help me get the travel time with traffic from the Distance Matrix API, that would be greatly appreciated.
No need for business license, just need an API key from project on https://console.developers.google.com/ with Google distance Matrix enabled.
For results as on google map use traffic_model with values pessimistic,optimistic and do keep in mind "The departure_time must be set to within a few minutes of the current time" without that it will always return 15 minutes.
That feature appears to only be available to Maps for Business customers, according to the docs.
Even with business licence you can only query departure_time 5 minutes from now if you're using traveling mode is driving
https://developers.google.com/maps/documentation/distancematrix/
According to the google docs "departure_time" can only be used if "mode"(travel mode) is set to "Driving"(which is the default travelMode) and an Api KEY is included in your request.
There is also an optional parameter "trafficModel".
Here is an example url with proper parameters.
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.598566%2C-73.7527626&mode=driving&departure_time=now&traffic_model=optimistic&key=YOUR_API_KEY
To use distance matrix api as javascript code use it as mentioned in this doc.
https://developers.google.com/maps/documentation/javascript/distancematrix
**Imp:**There are many limitations with this API. Most of the features are available only for premium users.Read the above doc carefully.
Please try with below code.
var origin = new google.maps.LatLng(detectedLatitude,detectedLongitude);
var destination = new google.maps.LatLng(latitudeVal,langtitudeVal);
var service = new google.maps.DistanceMatrixService();var date = new Date();
date.setDate(date.getDate() + 1);
var DrivingOptions = {
departureTime: date,
trafficModel: 'pessimistic'
};
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: 'DRIVING',
drivingOptions : DrivingOptions,
unitSystem: google.maps.UnitSystem.METRIC,
durationInTraffic: true,
avoidHighways: false,
avoidTolls: false
}, response_data);function response_data(responseDis, status) {
if (status !== google.maps.DistanceMatrixStatus.OK || status != "OK"){
console.log('Error:', status);
// OR
alert(status);
}else{
alert(responseDis.rows[0].elements[0].distance.text);
}});
Please refer this document click here
This API can solve your problem - https://distancematrix.ai/dev
It takes into consideration the traffic conditions, road constructions, and other restrictions when calculating travel time. And if you were using Google's API before for you will be easy, because you don't need to rewrite code.
Regarding the departure time, you will find the following in the documentation:
"departure_time — a desired time of the departure. You can specify the time as an integer in seconds since midnight, January 1, 1970, UTC. Alternatively, you can specify a value of now, which sets the departure time to the current time (correct to the nearest second). If neither time is specified, the departure_time defaults to now (that is, the departure time defaults to the current time). The departure_time must be set to the current time or some time in the future. It cannot be in the past. Results for a given request may vary over time due to the changes in the road network, updated average traffic conditions, and the distributed nature of the service. Results may also vary between nearly-equivalent routes at any time or frequency."
Follow these recommendations, and you won't have this problem. Besides, you can easily contact the developers and ask any questions concerning your situation.
Disclaimer: I work at a company that creates this API.
If you are using golang client, set DepartureTime to "now" in the input parameter DirectionsRequest of the Directions func.

events (as a json feed), start end parameters unix timestamp, are different if I change my OS time zone

I'm using the fullcalendar plugin and would appreciate if someone can give me a hand.
I am getting json events through a PHP URL.
something like this:
$('#calendar').fullCalendar({ events: "/myfeed.php" });
So in my php page that returns the events, I am getting 3 GET parameters:
'_'
'start'
'end'
The start and end parameter, indicate the date in UNIX timestamp.
So far so good, the problem that occurs is that if I change the time zone on my OS. also change these parameters start and end, for the same query in the same day in the calendar.
the weirdest part is that it only happens in Mozilla Firefox.
in Google Chrome, this problem does not occur.
e.g.
I have set my time zone ((UTC-04: 00) Santiago)
I'm referring to the day 09.09.2012 on the agenda,
firebug shows me that these parameters are being sent to my php page
_ 1347245953581
end 1347246000
start 1347159600
but if I change the time zone from my OS to ((UTC-03: 00) Buenos Aires)
consulting on 09.09.2012 on the agenda,
are other parameters which are now sent to the PHP page.
_ 1347246338047
end 1347332400
start 1347246000
Being that it is the same day, are other start and end parameters which are sent to check for events.
There is an ignoreTimezone option on the fullcalendar that might help. I'm not sure if it affects the start/end time passed to the feeds.
http://arshaw.com/fullcalendar/docs/event_data/ignoreTimezone/
Another option is to convert the passed timestamp to a Date object and get the local data from the Date object afterwards and use that in your queries.
Convert a Unix timestamp to time in JavaScript
I know it is not the exact answer, but it might help you out a bit.
Here is a sample piece of PHP code to convert the passed timestamp into a local formatted date:
$startts = $_REQUEST["start"]; // original timestamp
$startdt = new DateTime('now', new DateTimeZone('Europe/Oslo') ); // setup a local datetime
$startdt->setTimestamp($startts); // Set the date based on timestamp
echo $startdt->format('Y-m-d H:i:s'); // Output local date and time

Resources