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
Related
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': []}
[]
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.
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
Been beating my head against the wall for longer than I care to admit. I have a list of dicts that I need to pass to something as an iterable. I assume I need to take the contents of these dicts and get them into a properly-formatted new dict. I can't figure out how to do it. Here's what my list "contacts" looks like:
[{'City': 'Boston',
'Unique_Id': '123456-789',
'FullName': 'Smith, Joe',
'other_id': '0987654',
'area': 'Area 1'},
{'City': 'San Fransisco',
'Unique_Id': '654321-987',
'FullName': 'Brown, John',
'other_id': '7654321',
'area': 'Area 2'},
{'City': 'New York',
'Unique_Id': '7890123-456',
'FullName': 'Incomplete, Guy',
'other_id': None,
'area': None}]
I'm trying to pass it to this: https://github.com/plumdog/flask_table
Where the relevant section is:
class Item(object):
def __init__(self, name, description):
self.name = name
self.description = description
...
...
items = [dict(name='Name1', description='Description1'),
dict(name='Name2', description='Description2'),
dict(name='Name3', description='Description3')]
So I need to get my list "contacts" in some kind of shape to be iterated through by that function, presumably into a new dict. I feel like I need a list comprehension to transform it into a dictionary but I just can't grasp the concept well enough to understand how to do that. I need this to be passed to the function so that it can convert the json list/dict values to an HTML table to output in a little SPA. There's a hard limit of 5 dicts in the list so it shouldn't ever get very long. I assume I want to end up with something like:
items = [contacts(name='City', description='Their City'),
contacts(name='Unique_Id', description='GUID'),
contacts(name='FullName', description='Name')
...and so on
]
I have no idea how though. Any help would be greatly appreciated.
You dont need to change anything in your dictionary. You just need to update attributes in both classes:
# Declare your table
class ItemTable(Table):
City = Col('City')
Unique_Id = Col('Unique_Id')
FullName = Col('FullName')
other_id = Col("other_id")
area = Col("area")
# Get some objects
class Item(object):
def __init__(self, City, Unique_Id,FullName,other_id,area):
self.City = City
self.Unique_Id = Unique_Id
self.FullName = FullName
self.other_id=other_id
self.area = area
and then simply as per from your reference URL:
myTable = [{'City': 'Boston',
'Unique_Id': '123456-789',
'FullName': 'Smith, Joe',
'other_id': '0987654',
'area': 'Area 1'},
{'City': 'San Fransisco',
'Unique_Id': '654321-987',
'FullName': 'Brown, John',
'other_id': '7654321',
'area': 'Area 2'},
{'City': 'New York',
'Unique_Id': '7890123-456',
'FullName': 'Incomplete, Guy',
'other_id': None,
'area': None}]
table = ItemTable(myTable)
print(table.__html__())
The output is as below:
<table>
<thead><tr><th>City</th><th>Unique_Id</th><th>FullName</th><th>other_id</th><th>area</th></tr></thead>
<tbody>
<tr><td>Boston</td><td>123456-789</td><td>Smith, Joe</td><td>0987654</td><td>Area 1</td></tr>
<tr><td>San Fransisco</td><td>654321-987</td><td>Brown, John</td><td>7654321</td><td>Area 2</td></tr>
<tr><td>New York</td><td>7890123-456</td><td>Incomplete, Guy</td><td></td><td></td></tr>
</tbody>
</table>
I want to extract the full address from the webpage and I'm using BeautifulSoup and JSON.
Here's my code:
import bs4
import json
from bs4 import BeautifulSoup
import requests
url = 'xxxxxxxxxxxxxxxxx'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
for i in soup.find_all('div', attrs={'data-integration-name':'redux-container'}):
info = json.loads(i.get('data-payload'))
I printed 'info' out:
{'storeName': None, 'props': {'locations': [{'dirty': False, 'updated_at': '2016-05-05T07:57:19.282Z', 'country_code': 'US', 'company_id': 106906, 'longitude': -74.0001954, 'address': '5 Crosby St 3rd Floor', 'state': 'New York', 'full_address': '5 Crosby St 3rd Floor, New York, 10013, New York, USA', 'country': 'United States', 'id': 17305, 'to_params': 'new-york-us', 'latitude': 40.719753, 'region': '', 'city': 'New York', 'description': '', 'created_at': '2015-01-19T01:32:16.317Z', 'zip_code': '10013', 'hq': True}]}, 'name': 'LocationsMapList'}
What I want is the "full_address" under "location" so my code was:
info = json.loads(i.get('data-payload'))
for i in info['props']['locations']:
print (i['full_address'])
But I got this error:
----> 5 for i in info['props']['locations']:
KeyError: 'locations'
I want to print the full address out, which is '5 Crosby St 3rd Floor, New York, 10013, New York, USA'.
Thanks a lot!
The data you are parsing seem to be inconsistent, the keys are not in all objects.
If you still want to perform a loop, you need to use a try/except statement to catch an exception, or the method get to set a fallback when you're looking for a key in a dictionary that could be not here.
info = json.loads(i.get('data-payload'))
for item in info['props'].get('locations', []):
print (item.get('full_address', 'no address'))
get('locations', []) : returns an empty list if the key location doesn't exist, so the loop doesn't run any iteration.
get('full_address', 'no address') : returns "no adress" in case there is no such key
EDIT :
The data are inconsistent (never trust data). Some JSON objects have a key props with a null /None value. The next fix should correct that :
info = json.loads(i.get('data-payload'))
if info.get('props'):
for item in info['props'].get('locations', []):
print (item.get('full_address', 'no address'))
Your first object is fine, but it's clear that your second object has no locations key anywhere, nor full_address.