OS/2, eCS & ArcaOS - Technical > Programming

JSon to Rexx (stem)

<< < (3/6) > >>

lmotaku:
Probably reviving a dead topic, but I'm looking to do something similar, however, I am looking to parse the request through my own server before fetching the result. I'm going to fetch a url over http, and use my servers response as a txt file. Each line being something distinct to give back on the desktop.

I'm thinking of using http://rexxcurl.sf.net to pull it all together, since I know more php than I do Rexx.

Jan-Erik Lärka:
Hello Larry,

I'm adjusting the code somewhat for the rexx openweather script, but am not finished yet.

Do you need to fetch alot of data since you consider curl?
If not, create your own script and set defaults¹, copy some of the functions² in this script and use³ them.

¹) Code skeleton for a FAKE URL, replace with an actual working URL to make this work.

--- Code: ---/* MyScript.cmd - Begin with the comment that identifies it as a rexx script */

/*Load the Libraries you need */
/* Initialize socket package */
if RxFuncQuery( "SockLoadFuncs" ) then
do
   rc = RxFuncAdd( "SockLoadFuncs", "RxSock", "SockLoadFuncs" )
   rc = SockLoadFuncs()
end

/* Place your preparation code here ===START=== */
system_language = VALUE( 'LANGUAGE',, 'OS2ENVIRONMENT' )
target_language = 'EN' /* Translate from English to */
retrieve_this_url = 'http://translate.google.com/...?from_lang='||system_language||'&to_lang='||target_language||'&text='||ARG(1)
/* Place your preparation code here ===END=== */

CALL rxURLComponents 'stemname.', 
CALL rxURLConnect()
IF rxURLRetrieve() < 0 THEN
DO
   SAY 'A problem occurred!'
   RETURN 1
END
/* Place code here to use/interpret the received data ===START=== */
SAY text
/*stem = 'myjson.'
 IF fromJSon( text ) = 0 THEN
DO i = 1 TO myjson.0
   SAY VALUE( myjson.i )
END*/
CALL CHAROUT 'output.txt', text
/* Place code here to use/interpret the received data ===END=== */

RETURN 0

/* Add copied functions, see ² below */

--- End code ---

²) Useful functions
rxURLConnect - Establish the initial connection with the server
rxURLRetrieve - Fetch data from the net
rxURLComponents - Place split up URL into an array (called stem in rexx) for rxURLConnect to use
GetURLComponents - Does the actual split into manageable pieces as separate variables

In what format do you get the reply?
fromJSon  - Interpret the JSon format and place the data in a stem
parse_xml_tags - Interpret the XML/HTML format, and extract individual tags, just ask and I'll post one I've written
   
CHANGESTR - My own useful function to replace a string with another, available in ORexx, but not in Classic Rexx

³) Start a command line and test the code
[c:\temp]myscript This is a text in English to translate into my own language

//Jan-Erik

Jan-Erik Lärka:
Adjusted the function "parse_xml_tags" to handle a case when similar tag names where used.


--- Code: ---parse_xml_tags: PROCEDURE /* xmlString, tag <<, tag_value>, selection> */
    prev_trace = TRACE( 'O' )
    PARSE UPPER ARG xmlString, tag, val, selection
    start_tag = '<'||tag
    INTERPRET "PARSE VALUE xmlString WITH pre'"||start_tag||val||"'extra'>'post"
    SELECT
        WHEN selection = 1 THEN /* tag */
        DO
            IF LENGTH( extra ) > 0 THEN
            DO
                PARSE VALUE SUBSTR( ARG(1), ABS( LENGTH( xmlString ) - LENGTH( post ) + 1 ) ) WITH pre'<'.
                RETURN pre
            END
        END
        WHEN selection = 2 THEN /* tag value */
            RETURN SUBSTR( ARG(1), ABS( LENGTH( xmlString ) - LENGTH( post ) - LENGTH( extra ) ), LENGTH( extra ) - ( RIGHT( extra, 1 ) = '/' ) )
        WHEN RIGHT( extra, 1 ) <> '/' THEN
        DO
            end_tag = '</'||tag||'>'
            next = LENGTH( xmlString ) - LENGTH( post ) + 1
            open_tag = next
            INTERPRET "PARSE VALUE post WITH pre'"||end_tag||"'post"
            close_tag = LENGTH( xmlString ) - LENGTH( post ) - LENGTH( end_tag ) + 1
            count = COUNTSTR( start_tag||'>', xmlString, open_tag, close_tag ) + COUNTSTR( start_tag||' ', xmlString, open_tag, close_tag )
            DO WHILE count > 0
                next = close_tag + LENGTH( end_tag )
                close_tag = POS( end_tag, xmlString, next )
                IF close_tag = 0 THEN
                    close_tag = LENGTH( xmlString )
                post = SUBSTR( xmlString, next, close_tag - next )
               
                INTERPRET "PARSE VALUE post WITH .'"||start_tag||"'extra'/>'post"
               
                DO WHILE LENGTH( post ) > 0
                    next = close_tag - LENGTH( post )
                    INTERPRET "PARSE VALUE post WITH .'"||start_tag||"'extra'/>'post"
                END
                count = COUNTSTR( start_tag||'>', xmlString, open_tag, close_tag ) + COUNTSTR( start_tag||' ', xmlString, open_tag, close_tag ) - 1
            END
            IF close_tag = 0 THEN close_tag = LENGTH( ARG(1) ) - open_tag
            ELSE close_tag = close_tag - open_tag
            RETURN SUBSTR( ARG(1), open_tag, MAX( 1, close_tag ) )
        END
        OTHERWISE NOP
    END
    CALL TRACE prev_trace
RETURN ''
   
COUNTSTR: PROCEDURE /* needle, haystack< <, startpos>, endpos> */
    IF ARG() < 2 THEN RETURN -1
    IF DATATYPE( ARG(3), 'W' ) THEN
        next = ARG(3)
    ELSE
        next = 1
    needle = ARG(1)
    haystack = ARG(2)
    IF DATATYPE( ARG(4), 'W' ) THEN
        haystack = SUBSTR( haystack, next, ABS( ARG(4) - next ) )
    next = 1
    count = 0
    DO WHILE next > 0
        next = POS( needle, haystack, next )
        IF next > 0 THEN DO
            next = next + LENGTH( needle )
            count = count + 1
        END
    END
RETURN count
--- End code ---

//Jan-Erik

Fr4nk:
Hello,

some years ago I made a Rexx-based WPS Gadget called WorkPlaceWeather. It creates a WPS Object with weather icon and informations as object text.

http://subsys.de/WPWeather/

I used wetter.com to get the weather informations and forecasts, and now I want to change to Open Weather Map.

So I just found your json parser and would be happy to use it to change my program. Thanks a lot this saved me some hours :) I'll come back when its done.

Jan-Erik Lärka:
Hello,

That would be awsome Fr4nk, I want it!  :D

I've tested the parsing on various JSon files, but do tell me if you run into problems and want me to look into it!

The script should give you a general idea of how to do it and an XCenter Widget would be so nice to have!

You may have to improve checks within your script somewhat compared to this script to verify what gets returned as the openweather service (or is it the site http://geoip.prototypeapp.com) seem to not return the expected data during updates with the current code.

//Jan-Erik

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version