Getting the users country

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the user's country

Post by richmond62 » Tue Aug 08, 2023 10:49 am

Interestingly enough this:

Code: Select all

put JSONToArray(URL ("https://ipleak.net/json/")) into xArray
combine xArray using return
put xArray into fld "ff"
gave me this:

20
12829
1691487887
Manole
EU
Europe
BG
Bulgaria

(rest not shown)


What worries me is that from my list the country code and/or name may be in different lines for different users.

Where have all the 'headers' in the double quotes gone?
Last edited by richmond62 on Tue Aug 08, 2023 1:55 pm, edited 1 time in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the users country

Post by stam » Tue Aug 08, 2023 11:08 am

On that note, if you wanted to use the other service I linked above, it just needs 1 extra line to set the httpHeaders with your public API key (when registering free, you get a private and public key for both live and test servers, and its free for up to 100,000 API calls/month). The public key has 'pk' in it:

Code: Select all

set the httpHeaders to "Authorization: " & "prj_live_pk_<your unique key here>"
put url ("https://api.radar.io/v1/geocode/ip") into field 1
The return is a bit different - annoyingly it returns a part of London I'm close to as the 'city' - but on the plus side it gives the icon of the flag (visible when used as an array) and it also returns an HTTP code which may be useful for debugging:
"ugly JSON" wrote:{"meta":{"code":200},"address":{"countryCode":"GB","country":"United Kingdom","countryFlag":"🇬🇧","state":"England","stateCode":"EN","city":"Pimlico","latitude":51.485,"longitude":-0.1355,"layer":"locality","geometry":{"type":"Point","coordinates":[-0.1355,51.485]}},"proxy":false,"ip":"161.17.0.123"}
If you use FerrusLogic's excellent PhotonJSON library (https://github.com/Ferruslogic/PhotonJSON), you can make this more readable:

Code: Select all

start using stack "community.ferruslogic.library.photonjson"
put beautifyJSON(field 1) into field 1

Code: Select all

{
    {
    "meta": {"code": 200},
    "address": {
        "countryCode": "GB",
        "country": "United Kingdom",
        "countryFlag": "\u0086\u0087\u0086\u0087",
        "state": "England",
        "stateCode": "EN",
        "city": "Pimlico",
        "latitude": 51.485,
        "longitude": -0.1355,
        "layer": "locality",
        "geometry": {"type": "Point", "coordinates": [-0.1355, 51.485]}
    },
    "proxy": false,
    "ip": "161.17.0.123"
}
}
Last edited by stam on Tue Aug 08, 2023 11:22 am, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the user's country

Post by richmond62 » Tue Aug 08, 2023 11:13 am

This works, and gives me (and, possibly, the OP) what I want:

Code: Select all

put JSONToArray(URL ("https://ipleak.net/json/")) into xArray
put xArray["country_code"] into fld "ff"
-
Pimlico.jpg
Last edited by richmond62 on Tue Aug 08, 2023 1:54 pm, edited 2 times in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the users country

Post by stam » Tue Aug 08, 2023 11:16 am

richmond62 wrote:
Tue Aug 08, 2023 10:49 am
gave me this:

20
12829
1691487887
Manole
EU
Europe
BG
Bulgaria
...


What worries me is that from my list the country code and/or name may be in different lines for different users.

Where have all the 'headers' in the double quotes gone?
Not sure what you are trying to achieve? You have key:value pairs in the JSON. You have key:value pairs in the array
What do you gain by turning into text? Surely you just want selected information? Why not just use the array?

In any case, you're doing it wrong ;) The correct syntax of what you are trying to do is:

Code: Select all

combine xArray using return and ": "
for me this returns:
accuracy_radius: 10
as_number: 15404
cache: 1691489642
city_name: London
continent_code: EU
continent_name: Europe
country_code: GB
country_name: United Kingdom
ip: 161.17.0.123
isp_name: COLT Technology Services Group Limited
latitude: 51.5088
level: min
longitude: -0.093
metro_code: null
postal_code: null
postal_confidence: null
query_date: 1691489642
query_text: 161.17.0.123
query_type: myip
region_code: ENG
region_name: England
reverse:
time_zone: Europe/London

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the users country

Post by stam » Tue Aug 08, 2023 11:29 am

richmond62 wrote:
Tue Aug 08, 2023 10:39 am
Richmond's ignorance in matters such as JSON.
It is worth knowing a bit about JSON as it's really handy. It's basically a text version of an array and you can use this for many things, not just javascript.
At its heart it's just key : value pairs, much like a livecode array. A key:value pair is an 'object' and objects can include arrays of objects etc, so it's very much like a livecode array. Some simple text conventions using {} and [].
Nice tutorial here: https://www.w3schools.com/js/js_json_intro.asp

There are built-in methods for using JSON, FerrusLogic have a nice library as well (linked in previous post)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the user's country

Post by richmond62 » Tue Aug 08, 2023 11:51 am

Thank you very much for that link. 8)

That link seems to imply that a JSON text file is a sort of database.

In fact a LiveCode table field could be exported to a JSON document WITHOUT any libraries, just a simple routine to insert some double quotes and colons, and something to 'wrap' it into an HTML document format.
Last edited by richmond62 on Tue Aug 08, 2023 7:24 pm, edited 4 times in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the user's country

Post by richmond62 » Tue Aug 08, 2023 1:53 pm

SShot 2023-08-08 at 15.50.01.png
Attachments
Originating Country.livecode.zip
Stack.
(835 Bytes) Downloaded 118 times

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the user's country

Post by stam » Tue Aug 08, 2023 7:53 pm

richmond62 wrote:
Tue Aug 08, 2023 11:51 am
That link seems to imply that a JSON text file is a sort of database.
Well, as much as a livecode array is a database… you can certainly use both for data store and retrieval. I guess it’s up to the individual use-case whether that’s the right choice…

As for tables and the like: it’s a much better choice than TSV/CSV as you can have delimiters inside each object (field) and easily maintain the table structure. Gotta wonder why this hasn’t just outright replaced CSV…

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the users country

Post by richmond62 » Tue Aug 08, 2023 8:45 pm

Gotta wonder why this hasn’t just outright replaced CSV
Possibly because not all of us are overly enthusiastic about Java.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the users country

Post by stam » Wed Aug 09, 2023 12:28 am

richmond62 wrote:
Tue Aug 08, 2023 8:45 pm
Gotta wonder why this hasn’t just outright replaced CSV
Possibly because not all of us are overly enthusiastic about Java.
What's JSON got to do with java, or even javascript (which incidentally has nothing to do with java ;) )
It was created for javascript but is a format but is eminently adaptable to any situation and any language that can read text.

Plenty use JSON without touching javascript...! I recently had to convert a large dataset from a database (~150,000 records) to ND-JSON (where each record is it's own complete JSON separated with a return - http://ndjson.org) to feed a machine learning platform. Had I exported the database in TSV or CSV that would have been an impossible nightmare to parse properly as the fields contained both tabs, returns and commas. SO much simpler.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the users country

Post by richmond62 » Wed Aug 09, 2023 9:31 am

Presumably ONE advantage of a CSV/TSV over a JSON file is that one is, generally, confined to one type of delimiter.

Importing text files into LC that contain multiple delimiter types (as well as accounting for potential delimiter collision)
is a 'bother'.

Of course, that LC already contains the wherewithall to cope with JSON files makes life a whole lot easier.
What's JSON got to do with java, or even javascript (which incidentally has nothing to do with java ;) )
Well: the format did originate with javascript.

My objections to JSON files (only from the viewpoint of using them as files to import into LC without using the inbuilt JSON parser):

1. Curly brackets.
2. Spaces. Although these could be removed as the file is imported.

My objections to CSV/TSV files (only from the viewpoint of using them as files to import into LC):

1. Office suites (I generally use LibreOffice) do not 'naturally' save documents in delimited formats, so one has to make a point
of setting one's chosen delimiter and exporting one's spreadsheet in that format . . .

1.1. This means that if a client lobs one a 'standard' spreadsheet file (from, say, MicroSoft Excel) one has to turn it into a delimited text file BEFORE LC can import the thing.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the users country

Post by richmond62 » Wed Aug 09, 2023 10:17 am

OK: I apologise:
-
SShot 2023-08-09 at 12.16.16.png
-
LibreOffice, at least, does 'do' CSV/TSV.
-
Fioch:
-
SShot 2023-08-09 at 12.21.31.png
SShot 2023-08-09 at 12.21.31.png (26.8 KiB) Viewed 3647 times
-
https://en.brunner.bi/post/how-to-open- ... ower-query

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the users country

Post by stam » Wed Aug 09, 2023 1:39 pm

That’s all fine. But I challenge you to correctly import text files where fields contain multiple carriage returns.

The fact that you think this is OK tells me you haven’t done this. Yes, single line TSV imports are fine, but as as there is a return in a field things get inordinately complicated.

Easy enough to test: Create a spreadsheet, populate some lines and edit the cells to add some carriage returns inside the cells and then export TSV. Pretty sure you’ll have difficulty importing it correctly.

I’m complaining exactly about the fact that no office apps cater for JSON exports. Presumably the feeling is that this will be “scary” to common folk. But really it’s not significantly different from TSV and let’s face it: most users either don’t need to know what’s in the TSV file or if they are programming a solution, can manage JSON…

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the users country

Post by richmond62 » Wed Aug 09, 2023 4:57 pm

Setting up a spreadsheet which has carriage returns in cells is a pain in the bum in its own right:

I ended up copy-pasting into LibreOffice spreadsheet cells from a text editor.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Getting the users country

Post by richmond62 » Thu Aug 10, 2023 9:32 am

Rich Siegel at Bare Bones software has sent me a preview copy of BBEdit [14.6.7] as version 14.6.6 was crashing on attempting to read the contents of an .ODS 'file' on macOS 14 beta 5, and THAT allows the 'content.xml' bit to be opened in a reasonably readable format, that would be easy enough (although fairly tedious) to parse in LiveCode directly . . .

BUT, as we all know [or should] LC 'sees' an .ODS 'file' as something monolithic rather than the cryptic folder it really is, leaving the 'content.xml' file ungettable at.
-
SShot 2023-08-10 at 11.32.33.png
-
http://www.barebones.com/

Post Reply

Return to “iOS Deployment”