Mapbox Geocoder gives index error (internal server error) - python

I'm using mapbox geocoder for finding latitude and longitude from zip-codes.Problem is it sometimes work just fine and somtimes doesn't work at all.When it doesn't work,it return index error out of bounds,but in my terminal also shows internal server error. What can be done it this situation?
My code is bellow:
def get_context_data(self, **kwargs):
context = super(SingleCenterDetailView, self).get_context_data(**kwargs)
zip_code = self.object.center.zip_code
geocoder = Geocoder(access_token=mapbox_access_token)
response = geocoder.forward(str(zip_code))
response = response.geojson()['features'][0]
resp = response.get('center')
geo = [resp[0],resp[1]]
context['geo'] = geo
return context
And the problem is in line response = response.geojson()['features'][0] becouse when it works good,when I print this line it shows dict like this
{'text': '53-334', 'context': [{'text': 'Wrocław', 'wikidata': 'Q1799', 'id': 'place.8365052709251970'}, {'short_code': 'PL-DS', 'text': 'Dolnośląskie', 'wikidata': 'Q54150', 'id': 'region.25860'}, {'short_code': 'pl', 'text': 'Poland', 'wikidata': 'Q36', 'id': 'country.340'}], 'geometry': {'coordinates': [17.026519, 51.096412], 'type': 'Point'}, 'center': [17.026519, 51.096412], 'type': 'Feature', 'relevance': 1, 'bbox': [17.024936, 51.095263, 17.028411, 51.09752], 'place_name': '53-334, Wrocław, Dolnośląskie, Poland', 'place_type': ['postcode'], 'properties': {}, 'id': 'postcode.5334805015388220'}
when it doesn't work it prints nothing (thats this index error)
So my question is,can anything be done to solve this? I mean,if this sometimes work and sometimes does not then its probably mean that for some zip-codes it
cannot find latitude and longitude,but maybe is there some workaround? Or maybe problem is much more trivial and I simply did someting wrong? (I can even imagine it is some Django problem beocuse of this internal server error)

Ok so I have founded solution and it is much simpler than i thought.Sometimes zip code it isn't enough.So instead of zip code,you have to pass to geocoder.forward() entire address,like 'Szaserów Warszawa 04-141`
then it should works pretty well.

Related

"Failed to convert value of type 'java.lang.String' to required 'java.util.List'." Or how to convert Python list to Java.util list?

This one has the better of me, for sure.
I am attempting to use the USDA FoodData Central Rest API for Python, and I have extensively reviewed the related documentation, online materials and SwaggerHub simulations.
To make a long story short, it seems I need to convert a Python list to a 'java.util.List', something not in the USDA documentation, and something I have not been able to make happen. Despite my efforts, I am unable to even attempt to make that conversion happen, because I cannot install the Python dependencies.
It boils down to two questions overall:
How do I convert a Python list to a Java.Util.List if possible?
What can I do about the problems installing the dependencies (e.g. jep) when it appears they are deprecated?
The USDA site sends you to a SwaggerHub simulation. It was helpful, and it gave me the URL pasted below as a model for my API. However, the instructions regarding the required 'fdcIds list' says nothing at all about the need to convert to Java.
To be clear, the 'fdcIds list' is simply the desired Python list of the six-digit numbers that represent all the food items and products in the USDA database. You can request up to 20 at a time and the API returns a complete nutritional profile of each.
You will see what I mean if you follow the SwaggerHub link below
Here is the Swaggerhub model URL with the link below:
https://api.nal.usda.gov/fdc/v1/foods?fdcIds=173567&fdcIds=173565&fdcIds=173571&format=abridged&nutrients=208&nutrients=204&nutrients=205&api_key=xxxxxxxxxxxxxxxx
https://app.swaggerhub.com/apis/fdcnal/food-data_central_api/1.0.0#/info
Here are SwaggerHub instructions for the fdcID list pasted in their entirety:
"fdcIds (reguired)
array[string]
(query)
List of multiple FDC ID's. Should be comma separated list (e.g. fdcIds=534358,373052) or repeating parameters (e.g. fdcIds=534358&fdcIds=373052)."
I think the rest of my code (below) is fine, because otherwise the SwaggerHub simulation produces the desired results.
Here is the complete error message I received afterwards:
'error': 'Bad Request',
'message': "Failed to convert value of type 'java.lang.String' to required "
"type 'java.util.List'; nested exception is "
'java.lang.NumberFormatException: For input string: '
'"[{\'fdcids\':173567},{\'fdcids\':173565},{\'fdcids\':173571},{\'fdcids\':173569},{\'fdcids\':173564}]"',
'path': '/portal-data/api/v1/foods',
'status': 400,
'timestamp': '2022-04-15T21:49:49.609+0000'}
I know Python and JavaScript. I know nothing about Java.
However, I attempted to install the Python packages necessary for this operation, such as jdk and jep, but I ran into additional error messages, as it seems the packages are deprecated(?) - though I am not sure. I forced the jep installation, but it still did not install successfully.
The error messages related to the failed installations are too long to paste here; but I will provide examples that seem to represent the gist of the problem.
error: subprocess-exited-with-error
error: metadata-generation-failed
note: This error originates from a subprocess, and is likely not a problem with pip.
I have spent the past several trying to make this API work. I've been working on the related app for almost a year. I will send a request for technical assistance to the USDA, but I could use some help if anyone has some genuine insights or has resolved similar issues.
The USDA API is a popular one. It apparently works for most developers. I am hoping someone can help. I am a bit mystified by these obstacles.
My operating system is Windows 10 64 bit.
My code, as it stands, is pasted below.
I should note the variables in the URL were originally in a "parameters" dictionary.
But then, I started to receive error messages stating the dictionary did not contain the fields in the requested URL, which was not true. It seemed to work better when I made them standalone variables...except for the conversion issues related to the fdcIds list.
At that point, I started to receive error messages like the one in the title related to my failure to convert the Python list to a Java Util.List. I have reviewed all the online information and videos I could find about Python rest apis and possible solutions to the Java conversion problem, including all related questions on Stackoverflow.
I am at a loss as to how I should proceed.
Your sincere feedback is much appreciated.
Thank you for reviewing my question.
Below, I pasted my code in its entirety. Again, I have also tried to place the variables in the URL inside a parameters dictionary. But it only caused more trouble.
import requests
import pprint
api_key = 'AcAm9H2jcFikw4f5PmJl0hFJSjreMwg9BN621tQD'
pagesize = 5,
fdcIds = [{'fdcids': 173567}, {'fdcids': 173565}, {'fdcids': 173571}, {'fdcids': 173569}, {'fdcids': 173564}]
format = 'abridged'
dataType = 'SR Legacy'
foodNutrients = [208, 204,205]
url = f"https://api.nal.usda.gov/fdc/v1/foods?fdcIds={fdcIds}&format={format}&dataType={dataType}&nutrients={foodNutrients}&pagesize={pagesize}'&api_key={api_key}"
headers = {'Content-Type': 'application/json;charset=utf-8'}
data = requests.get(url, headers).json()
print(requests.status_codes)
pprint.pprint(data)
Change fdcIds as shown below and also foodNutrients:
import requests
import pprint
api_key = 'AcAm9H2jcFikw4f5PmJl0hFJSjreMwg9BN621tQD'
pagesize = 5
# fdcIds = [{'fdcids': 173567}, {'fdcids': 173565}, {'fdcids': 173571}, {'fdcids': 173569}, {'fdcids': 173564}]
fdcIds = "173567,173565,173571,173569,173564"
f = 'abridged'
dataType = 'SR Legacy'
foodNutrients = "208, 204, 205"
url = f"https://api.nal.usda.gov/fdc/v1/foods?fdcIds={fdcIds}&format={f}&dataType={dataType}&nutrients={foodNutrients}&pagesize={pagesize}'&api_key={api_key}"
print(url)
headers = {'Content-Type': 'application/json;charset=utf-8'}
data = requests.get(url, headers).json()
print(requests.status_codes)
pprint.pprint(data)
Result:
[{'dataType': 'SR Legacy',
'description': 'Shortening frying (heavy duty), beef tallow and cottonseed',
'fdcId': 173567,
'foodNutrients': [{'amount': 100,
'name': 'Total lipid (fat)',
'number': '204',
'unitName': 'G'},
{'amount': 0.0,
'name': 'Carbohydrate, by difference',
'number': '205',
'unitName': 'G'},
{'amount': 900,
'derivationCode': 'NC',
'derivationDescription': 'Calculated',
'name': 'Energy',
'number': '208',
'unitName': 'KCAL'}],
'ndbNumber': '4550',
'publicationDate': '2019-04-01'},
{'dataType': 'SR Legacy',
'description': 'Fat, chicken',
'fdcId': 173564,
'foodNutrients': [{'amount': 900,
'derivationCode': 'NC',
'derivationDescription': 'Calculated',
'name': 'Energy',
'number': '208',
'unitName': 'KCAL'},
{'amount': 99.8,
'name': 'Total lipid (fat)',
'number': '204',
'unitName': 'G'},
{'amount': 0.0,
'name': 'Carbohydrate, by difference',
'number': '205',
'unitName': 'G'}],
'ndbNumber': '4542',
'publicationDate': '2019-04-01'},
{'dataType': 'SR Legacy',
'description': 'Fat, turkey',
'fdcId': 173571,
'foodNutrients': [{'amount': 99.8,
'name': 'Total lipid (fat)',
'number': '204',
'unitName': 'G'},
{'amount': 0.0,
'name': 'Carbohydrate, by difference',
'number': '205',
'unitName': 'G'},
{'amount': 900,
'derivationCode': 'NC',
'derivationDescription': 'Calculated',
'name': 'Energy',
'number': '208',
'unitName': 'KCAL'}],
'ndbNumber': '4575',
'publicationDate': '2019-04-01'},
{'dataType': 'SR Legacy',
'description': 'Oil, soybean, salad or cooking, (partially hydrogenated) and '
'cottonseed',
'fdcId': 173565,
'foodNutrients': [{'amount': 100,
'name': 'Total lipid (fat)',
'number': '204',
'unitName': 'G'},
{'amount': 0.0,
'name': 'Carbohydrate, by difference',
'number': '205',
'unitName': 'G'},
{'amount': 884,
'derivationCode': 'NC',
'derivationDescription': 'Calculated',
'name': 'Energy',
'number': '208',
'unitName': 'KCAL'}],
'ndbNumber': '4543',
'publicationDate': '2019-04-01'},
{'dataType': 'SR Legacy',
'description': 'Shortening industrial, soybean (hydrogenated) and cottonseed',
'fdcId': 173569,
'foodNutrients': [{'amount': 0.0,
'name': 'Carbohydrate, by difference',
'number': '205',
'unitName': 'G'},
{'amount': 884,
'derivationCode': 'NC',
'derivationDescription': 'Calculated',
'name': 'Energy',
'number': '208',
'unitName': 'KCAL'},
{'amount': 100,
'derivationCode': 'A',
'derivationDescription': 'Analytical',
'name': 'Total lipid (fat)',
'number': '204',
'unitName': 'G'}],
'ndbNumber': '4554',
'publicationDate': '2019-04-01'}]
There are two ways in which you can pass the fdcIds:
Should be comma separated list (e.g. fdcIds=534358,373052) or repeating parameters (e.g. fdcIds=534358&fdcIds=373052).
This is mentioned here. Try to pass the ids in these formats.

How do I read a yaml file into a Jupyter notebook?

I have a file from an Open API Spec that I have been trying to access in a Jupyter notebook. It is a .yaml file. I was able to upload it into Jupyter and put it in the same folder as the notebook I'd like to use to access it. I am new to Jupyter and Python, so I'm sorry if this is a basic question. I found a forum that suggested this code to read the data (in my file: "openapi.yaml"):
import yaml
with open("openapi.yaml", 'r') as stream:
try:
print(yaml.safe_load(stream))
except yaml.YAMLError as exc:
print(exc)
This seems to bring the data in, but it is a completely unstructured stream like so:
{'openapi': '3.0.0', 'info': {'title': 'XY Tracking API', 'version': '2.0', 'contact': {'name': 'Narrativa', 'url': 'http://link, 'email': '}, 'description': 'The XY Tracking Project collects information from different data sources to provide comprehensive data for the XYs, X-Y. Contact Support:'}, 'servers': [{'url': 'link'}], 'paths': {'/api': {'get': {'summary': 'Data by date range', 'tags': [], 'responses': {'200': {'description': 'OK', 'content': {'application/json': {'schema': {'$ref': '#/components/schemas/covidtata'}}}}}, 'operationId': 'get-api', 'parameters': [{'schema': {'type': 'string', 'format': 'date'}, 'in': 'query', 'name': 'date_from', 'description': 'Date range beginig (YYYY-DD-MM)', 'required': True}, {'schema': {'type': 'string', 'format': 'date'}, 'in': 'query', 'name': 'date_to', 'description': 'Date range ending (YYYY-DD-MM)'}], 'description': 'Returns the data for a specific date range.'}}, '/api/{date}': {'parameters': [{'schema': {'type': 'string', 'format': 'date'}, 'name': 'date', 'in': 'path', 'required': True}], 'get': {'summary': 'Data by date', 'tags': [], 'responses': {'200': {'description': 'OK', 'content': {'application/json': {'schema': {'$ref': '#/components/schemas/data'}}}}}, 'operationId': 'get-api-date', 'description': 'Returns the data for a specific day.'}}, '/api/country/{country}': {'parameters': [{'schema': {'type': 'string', 'example': 'spain'}, 'name': 'country', 'in': 'path', 'required': True, 'example': 'spain'}, {'schema': {'type': 'strin
...etc.
I'd like to work through the data for analysis but can't seem to access it correctly. Any help would be extremely appreciated!!! Thank you so much for reading.
What you're seeing in the output is JSON. This is in a machine-readable format which doesn't need human-readable newlines or indentation. You should be able to work with this data just fine in your code.
Alternatively, you may want to consider another parser/emitter such as ruamel.yaml which can make dealing with YAML files considerably easier than the package you're currently importing. Print statements with this package can preserve lines and indentation for better readability.

Get JSON data with Django

I am playing around with GeoIP2 and requested the following in my view.
g = GeoIP2()
city = g.city('google.com')
tests = Test.objects.all()
args = { 'tests': tests }
return render(request, 'app/home.html', args)
I receive a JSON response with a bunch of data, I am interested in "city" for example.
{'city': None, 'continent_code': 'NA', 'continent_name': 'North America', 'country_code': 'US', 'country_name': 'United States', 'dma_code': None, 'latitude': 37.751, 'longitude': -97.822, 'postal_code': None, 'region': None, 'time_zone': 'America/Chicago'}
My model
# Create your models here.
class Test(models.Model):
city = models.CharField(default='', max_length=100)
def __str__(self):
return self.browser_family
Despite some googling and Youtube videos I am not quite sure how I should grab for example "city" out of the JSON response. I looked at previous threads here but not quite sure it can be applied here, seems like other threads were for more sophisticated stuff.
Any suggestions out there?
SOLVED
city = g.city('google.com')
json = city['city']
You can grab/assign it as follows:
city = JSON_response(‘city‘)
What happens here:
You assign the value of the key "city" of your JSON_reponse to the variable "city.
In your example city will be None

Why get_profile_connections() is returning an empty array when using likedin-api

so I am working on a school project where i'm supposed to get my LinkedIn connections and do some text mining on them. I ran into an API project provided by tomquirk (github) and it was running properly but when i moved my project to another computer it stops working and now when I try to use get_profile_connections() it always returns me an empty array. I don't know what is the problem and I hope i can get some help here.
this is my code:
from linkedin_api import Linkedin
api = Linkedin('linkedintestapi2#gmail.com', '*******')
# GET a profile
profile = api.get_profile('mohamad-arune-a26930176')
# GET a profiles contact info
contact_info = api.get_profile_contact_info('mohamad-arune-a26930176')
# GET all connected profiles (1st, 2nd and 3rd degree) of a given profile
connections = api.get_profile_connections('ACoAACnIxL4B6Ff_-AnBiMDQaXXn2jAvma9NkXI')
print(profile)
print(contact_info)
print(connections)
and i get this as a result:
{'lastName': 'arune', 'locationName': 'Evansville, Indiana Area', 'student': False, 'elt': False, 'firstName': 'mohamad', 'entityUrn': 'urn:li:fs_profile:ACoAACnIxL4B6Ff_-AnBiMDQaXXn2jAvma9NkXI', 'location': {'basicLocation': {'countryCode': 'us', 'postalCode': '47521'}, 'preferredGeoPlace': 'urn:li:fs_region:(us,244)'}, 'headline': 'Student at azazzaz', 'profile_id': 'ACoAACnIxL4B6Ff_-AnBiMDQaXXn2jAvma9NkXI', 'experience': [], 'skills': [], 'education': [{'entityUrn': 'urn:li:fs_education:(ACoAACnIxL4B6Ff_-AnBiMDQaXXn2jAvma9NkXI,568801875)', 'timePeriod': {'endDate': {'year': 2009}, 'startDate': {'year': 2002}}, 'degreeName': 'azazazaz', 'schoolName': 'azazzaz', 'fieldOfStudy': 'azazzaz'}]}
{'email_address': 'linkedintestapi2#gmail.com', 'websites': [], 'phone_numbers': []}
[]

Where I can get payer_id for executing payment (paypal)?

Working with PayPal Python REST SDK. After payment created we need to execute this payment. At https://github.com/paypal/PayPal-Python-SDK/blob/master/samples/payment/execute.py is code sample. But when executing payment
if payment.execute({"payer_id": "DUFRQ8GWYMJXC"}): # return True or False
print("Payment[%s] execute successfully" % (payment.id))
else:
print(payment.error)
we need to write payer_id. But how I can take it? Any ideas or code examples?
The payerid is returned in the host headers on the callback - Alternatively, you can do the following..
In the process of creating a sale, do...
saleinfo_ = {"intent":"sale",
"redirect_urls":{
"return_url":("myurlonsuccess"),
"cancel_url":("myurlonfail")
},
"payer":{"payment_method":"paypal"},
"transactions":[
{"amount":{"total":out_totaltopay_, "details":{"tax":out_taxes_, "subtotal":out_subtotal_}, "currency":symbol_}, "description":description_}
payment_ = Payment(saleinfo_)
if (payment_.create()):
token_ = payment_.id
Then when the return callback arrives, use...
payment_ = Payment.find(token_)
if (payment_ != None):
payerid_ = payment_.payer.payer_info.payer_id
if (payment_.execute({"payer_id":payerid_})):
....
The json data received in the find process is similar to the following
{'payment_method': 'paypal', 'status': 'VERIFIED', 'payer_info': {'shipping_address': {'line1': '1 Main St', 'recipient_name': 'Test Buyer', 'country_code': 'US', 'state': 'CA', 'postal_code': '95131', 'city': 'San Jose'}, 'first_name': 'Test', 'payer_id': '<<SOMEID>>', 'country_code': 'US', 'email': 'testbuyer#mydomain.com', 'last_name': 'Buyer'}}
Hope that helps

Categories