DreamDancer

Convert Trello Json File

Oct 24th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' this is still a work in progress, there's features which I want to implement
  2. ' like the ability to use iNet to download individual cards, and convert individual cards
  3. ' currently, you can use this to download the board's .json file
  4. ' and create a recent activity HTM file which will lump all activity in decending order together
  5. ' note, right click the Export JSON menu item and save the file
  6.  
  7. #PBFORMS CREATED V1.51
  8. '------------------------------------------------------------------------------
  9. ' The first line in this file is a PB/Forms metastatement.
  10. ' It should ALWAYS be the first line of the file. Other
  11. ' PB/Forms metastatements are placed at the beginning and
  12. ' end of "Named Blocks" of code that should be edited
  13. ' with PBForms only. Do not manually edit or delete these
  14. ' metastatements or PB/Forms will not be able to reread
  15. ' the file correctly.  See the PB/Forms documentation for
  16. ' more information.
  17. ' Named blocks begin like this:    #PBFORMS BEGIN ...
  18. ' Named blocks end like this:      #PBFORMS END ...
  19. ' Other PB/Forms metastatements such as:
  20. '     #PBFORMS DECLARATIONS
  21. ' are used by PB/Forms to insert additional code.
  22. ' Feel free to make changes anywhere else in the file.
  23. '------------------------------------------------------------------------------
  24.  
  25. #COMPILE EXE
  26. #DIM ALL
  27.  
  28. '------------------------------------------------------------------------------
  29. '   ** Includes **
  30. '------------------------------------------------------------------------------
  31. #PBFORMS BEGIN INCLUDES
  32. %USEMACROS = 1
  33. #IF NOT %DEF(%WINAPI)
  34.     #INCLUDE "WIN32API.INC"
  35. #ENDIF
  36. #IF NOT %DEF(%COMMCTRL_INC)
  37.     #INCLUDE "COMMCTRL.INC"
  38. #ENDIF
  39. #INCLUDE "PBForms.INC"
  40. #PBFORMS END INCLUDES
  41. '------------------------------------------------------------------------------
  42.  
  43. '------------------------------------------------------------------------------
  44. '   ** Constants **
  45. '------------------------------------------------------------------------------
  46. #PBFORMS BEGIN CONSTANTS
  47. %IDD_CONVERTJSON        =  101
  48. %IDLB_CARDS             = 1002
  49. %IDM_FILE_CONVERTJSON   = 1003
  50. %IDM_FILE_RECENTACTS    = 1004
  51. %IDM_FILE_ANALYZE       = 1005
  52. %IDM_FILE_EXIT          = 1006
  53. %IDR_ACCELERATOR1       =  103
  54. %IDR_MENU1              =  102
  55. %IDSB_STATUS            = 1001
  56. #PBFORMS END CONSTANTS
  57. '------------------------------------------------------------------------------
  58.  
  59. '------------------------------------------------------------------------------
  60. '   ** Declarations **
  61. '------------------------------------------------------------------------------
  62.  
  63. '------------------------------------------------------------------------------
  64.  
  65. '------------------------------------------------------------------------------
  66. '   ** Main Application Entry Point **
  67. '------------------------------------------------------------------------------
  68. FUNCTION PBMAIN()
  69.     PBFormsInitComCtls (%ICC_WIN95_CLASSES OR %ICC_DATE_CLASSES OR _
  70.         %ICC_INTERNET_CLASSES)
  71.  
  72.     ShowConverJson %HWND_DESKTOP
  73. END FUNCTION
  74. '------------------------------------------------------------------------------
  75.  
  76. '------------------------------------------------------------------------------
  77. '   ** CallBacks **
  78. '------------------------------------------------------------------------------
  79. CALLBACK FUNCTION ShowConverJsonProc()
  80. LOCAL FileName, FileFilter  AS STRING
  81. LOCAL Work                  AS STRING
  82. LOCAL FileInfo              AS DIRDATA
  83. LOCAL FileDate              AS FILETIME
  84. LOCAL SysTime               AS SYSTEMTIME
  85. LOCAL jFile, tFile          AS QUAD
  86.  
  87.     SELECT CASE AS LONG CB.MSG
  88.         CASE %WM_INITDIALOG
  89.             ' Initialization handler
  90.  
  91.         CASE %WM_SIZE
  92.             ' Dialog has been resized
  93.            LOCAL DX, DY, SX, SY    AS LONG
  94.             CONTROL SEND CB.HNDL, %IDSB_STATUS, CB.MSG, CB.WPARAM, CB.LPARAM
  95.             DIALOG GET CLIENT CB.HNDL TO DX, DY
  96.             CONTROL GET SIZE CB.HNDL, %IDSB_STATUS TO SX, SY
  97.             DY = DY - SY
  98.             CONTROL SET LOC CB.HNDL, %IDLB_CARDS, 0, 0
  99.             CONTROL SET SIZE CB.HNDL, %IDLB_CARDS, DX, DY
  100.  
  101.         CASE %WM_NCACTIVATE
  102.             STATIC hWndSaveFocus AS DWORD
  103.             IF ISFALSE CB.WPARAM THEN
  104.                 ' Save control focus
  105.                hWndSaveFocus = GetFocus()
  106.             ELSEIF hWndSaveFocus THEN
  107.                 ' Restore control focus
  108.                SetFocus(hWndSaveFocus)
  109.                 hWndSaveFocus = 0
  110.             END IF
  111.  
  112.         CASE %WM_COMMAND
  113.             ' Process control notifications
  114.            SELECT CASE AS LONG CB.CTL
  115.  
  116.                 CASE %IDM_FILE_CONVERTJSON, %IDM_FILE_RECENTACTS
  117.                     FileFilter = CHR$("JSON Files", 0, "*.json", 0)
  118.                     DISPLAY OPENFILE CB.HNDL, , , "Select JSON File", EXE.PATH$, _
  119.                     FileFilter, "", "", _
  120.                     %OFN_ENABLESIZING OR %OFN_FILEMUSTEXIST _
  121.                     TO FileName
  122.  
  123.                     IF (LEN(FileName) > 0) THEN
  124.                         Work = DIR$(FileName, TO FileInfo)
  125.                         jFile = FileInfo.LastWriteTime
  126.                         DIR$ CLOSE
  127.                         REPLACE ".json" WITH ".txt" IN Work
  128.                         Work = DIR$(Work, TO FileInfo)
  129.                         IF (LEN(Work) = 0) THEN
  130.                             tFile = jFile
  131.                         ELSE
  132.                             tFile = FileInfo.LastWriteTime
  133.                         END IF
  134.                         DIR$ CLOSE
  135.                         IF (tFile < jFile) THEN
  136.                             ConvertJson CB.HNDL, FileName
  137.                             SplitJson CB.HNDL, FileName
  138.                         END IF
  139.                         SELECT CASE AS LONG CB.CTL
  140.                             CASE %IDM_FILE_CONVERTJSON
  141.                                 CreateCards CB.HNDL, FileName, "actions.mbox", %TRUE
  142.                                 CreateCards CB.HNDL, FileName, "cards.mbox", %FALSE
  143.                         END SELECT
  144.                         ExtractRecentActions CB.HNDL, FileName
  145.                     END IF
  146.  
  147.                 CASE %IDM_FILE_ANALYZE
  148.                     FileFilter = CHR$("JSON Files", 0, "*.json", 0)
  149.                     DISPLAY OPENFILE CB.HNDL, , , "Select JSON File", EXE.PATH$, _
  150.                     FileFilter, "", "", _
  151.                     %OFN_ENABLESIZING OR %OFN_FILEMUSTEXIST _
  152.                     TO FileName
  153.  
  154.                     IF (LEN(FileName) > 0) THEN
  155.                         Work = DIR$(FileName, TO FileInfo)
  156.                         jFile = FileInfo.LastWriteTime
  157.                         DIR$ CLOSE
  158.                         REPLACE ".json" WITH ".txt" IN Work
  159.                         Work = DIR$(Work, TO FileInfo)
  160.                         IF (LEN(Work) = 0) THEN
  161.                             tFile = jFile
  162.                         ELSE
  163.                             tFile = FileInfo.LastWriteTime
  164.                         END IF
  165.                         DIR$ CLOSE
  166.                         IF (tFile < jFile) THEN
  167.                             ConvertJson CB.HNDL, FileName
  168.                             SplitJson CB.HNDL, FileName
  169.                         END IF
  170.                         AnalyzeActions CB.HNDL, FileName
  171.                     END IF
  172.  
  173.                 CASE %IDM_FILE_EXIT
  174.                     DIALOG END CB.HNDL
  175.  
  176.             END SELECT
  177.     END SELECT
  178. END FUNCTION
  179. '------------------------------------------------------------------------------
  180.  
  181.  
  182. '------------------------------------------------------------------------------
  183. '   ** Dialogs **
  184. '------------------------------------------------------------------------------
  185. FUNCTION ShowConverJson(BYVAL hParent AS DWORD) AS LONG
  186.     LOCAL lRslt AS LONG
  187.  
  188. #PBFORMS BEGIN DIALOG %IDD_CONVERTJSON->%IDR_MENU1->%IDR_ACCELERATOR1
  189.     LOCAL hDlg  AS DWORD
  190.  
  191.     DIALOG NEW PIXELS, hParent, "Convert Json", 598, 310, 376, 343, %WS_POPUP _
  192.         OR %WS_BORDER OR %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
  193.         %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR _
  194.         %WS_CLIPCHILDREN OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR _
  195.         %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT _
  196.         OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
  197.     CONTROL ADD STATUSBAR, hDlg, %IDSB_STATUS, "Status", 0, 306, _
  198.         376, 19, %WS_CHILD OR %WS_VISIBLE, %WS_EX_TRANSPARENT OR %WS_EX_LEFT _
  199.         OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
  200.     CONTROL ADD LISTBOX, hDlg, %IDLB_CARDS, , 38, 89, 277, 147
  201.  
  202.     AttachMENU1 hDlg
  203.  
  204.     AttachACCELERATOR1 hDlg
  205. #PBFORMS END DIALOG
  206.  
  207.     DIALOG SHOW MODAL hDlg, CALL ShowConverJsonProc TO lRslt
  208.  
  209. #PBFORMS BEGIN CLEANUP %IDD_CONVERTJSON
  210. #PBFORMS END CLEANUP
  211.  
  212.     FUNCTION = lRslt
  213. END FUNCTION
  214. '------------------------------------------------------------------------------
  215.  
  216. '------------------------------------------------------------------------------
  217. '   ** Menus **
  218. '------------------------------------------------------------------------------
  219. FUNCTION AttachMENU1(BYVAL hDlg AS DWORD) AS DWORD
  220. #PBFORMS BEGIN MENU %IDR_MENU1->%IDD_CONVERTJSON
  221.     LOCAL hMenu   AS DWORD
  222.     LOCAL hPopUp1 AS DWORD
  223.  
  224.     MENU NEW BAR TO hMenu
  225.     MENU NEW POPUP TO hPopUp1
  226.     MENU ADD POPUP, hMenu, "File", hPopUp1, %MF_ENABLED
  227.         MENU ADD STRING, hPopUp1, "Convert Json", %IDM_FILE_CONVERTJSON, %MF_ENABLED
  228.         MENU ADD STRING, hPopUp1, "Extract Recent", %IDM_FILE_RECENTACTS, %MF_ENABLED
  229.         MENU ADD STRING, hPopUp1, "Analyze Actions", %IDM_FILE_ANALYZE, %MF_ENABLED
  230.         MENU ADD STRING, hPopUp1, "-", 0, 0
  231.         MENU ADD STRING, hPopUp1, "Exit" + $TAB + "Alt+F4", %IDM_FILE_EXIT, %MF_ENABLED
  232.  
  233.     MENU ATTACH hMenu, hDlg
  234. #PBFORMS END MENU
  235.     FUNCTION = hMenu
  236. END FUNCTION
  237. '------------------------------------------------------------------------------
  238.  
  239. '------------------------------------------------------------------------------
  240. '   ** Accelerators **
  241. '------------------------------------------------------------------------------
  242. #PBFORMS BEGIN ASSIGNACCEL
  243. FUNCTION ASSIGNACCEL(tAccel AS ACCELAPI, BYVAL wKey AS WORD, BYVAL wCmd AS _
  244.     WORD, BYVAL byFVirt AS BYTE) AS LONG
  245.     tAccel.fVirt = byFVirt
  246.     tAccel.key   = wKey
  247.     tAccel.cmd   = wCmd
  248. END FUNCTION
  249. #PBFORMS END ASSIGNACCEL
  250. '------------------------------------------------------------------------------
  251.  
  252. '------------------------------------------------------------------------------
  253. FUNCTION AttachACCELERATOR1(BYVAL hDlg AS DWORD) AS DWORD
  254. #PBFORMS BEGIN ACCEL %IDR_ACCELERATOR1->%IDD_CONVERTJSON
  255.     LOCAL hAccel   AS DWORD
  256.     LOCAL tAccel() AS ACCELAPI
  257.     DIM   tAccel(1 TO 1) AS ACCELAPI
  258.  
  259.     ASSIGNACCEL tAccel(1), %VK_F4, %IDM_FILE_EXIT, %FVIRTKEY OR %FALT OR %FNOINVERT
  260.  
  261.     ACCEL ATTACH hDlg, tAccel() TO hAccel
  262. #PBFORMS END ACCEL
  263.     FUNCTION = hAccel
  264. END FUNCTION
  265. '------------------------------------------------------------------------------
  266.  
  267.  
  268. SUB ConvertJson(hDlg AS LONG, JsonFile AS STRING)
  269. LOCAL Chunk, Buffer, Work, FileName, Char, sMsg     AS STRING
  270. LOCAL FileIn, FileOut, InQuote, Depth, CiDx, Level  AS LONG
  271.  
  272.  
  273.   FileName = JsonFile
  274.   FileIn = FREEFILE
  275.   OPEN FileName FOR BINARY AS FileIn
  276.   REPLACE ".json" WITH ".txt" IN FileName
  277.   FileOut = FREEFILE
  278.   OPEN FileName FOR OUTPUT AS FileOut
  279.   Level = 1
  280.   Buffer = ""
  281.   InQuote = %FALSE
  282.   CiDx = 1
  283.   DO
  284.     IF (CiDx > LEN(Buffer)) THEN
  285.       Depth = SEEK(FileIn)
  286.       IF (Depth > LOF(FileIn)) THEN EXIT DO
  287.       sMsg = "Converting Block "& FORMAT$(Depth) &" of "& FORMAT$(LOF(FileIn))
  288.       STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, sMsg
  289.       DIALOG DOEVENTS
  290.       ' If (Depth > 32000) Then Exit Do
  291.      Chunk = STRING$(4096, $TAB)
  292.       GET #FileIn, , Chunk
  293.       Buffer = Buffer & Chunk
  294.       REPLACE $TAB WITH "" IN Buffer
  295.       CiDx = 1
  296.     END IF
  297.     Char = MID$(Buffer, CiDx, 1)
  298.     MID$(Buffer, CiDx, 1) = $TAB
  299.     IF (Char <> $TAB) THEN
  300.       IF (InQuote) THEN
  301.         Work = Work & Char
  302.         IF (Char = "\") THEN
  303.           IF (CiDx = LEN(Buffer)) THEN
  304.             ' fetch one more
  305.            Chunk = " "
  306.             GET #FileIn, , Chunk
  307.             Buffer = Buffer & Chunk
  308.           END IF
  309.           Work = Work & MID$(Buffer, CiDx + 1, 1)
  310.           MID$(Buffer, CiDx + 1, 1) = $TAB
  311.           Char = $TAB
  312.         END IF
  313.         IF (Char = CHR$(34)) THEN
  314.           InQuote = %FALSE
  315.         END IF
  316.       ELSE
  317.         IF (Char = "{") THEN
  318.           IF (CiDx = LEN(Buffer)) THEN
  319.             ' fetch one more
  320.            Chunk = " "
  321.             GET #FileIn, , Chunk
  322.             Buffer = Buffer & Chunk
  323.           END IF
  324.           IF (MID$(Buffer, CiDx + 1, 1) = "}") THEN
  325.             Work = Work & "{}"
  326.             MID$(Buffer, CiDx + 1, 1) = $TAB
  327.           ELSE
  328.             IF (LEN(Work) > 0) THEN
  329.               DumpOutput FileOut, Level, Work
  330.               Work = ""
  331.             END IF
  332.             DumpOutput FileOut, Level, "{"
  333.             Level = Level + 1
  334.           END IF
  335.         ELSEIF (Char = "}") THEN
  336.           IF (CiDx = LEN(Buffer)) THEN
  337.             ' fetch one more
  338.            Chunk = " "
  339.             GET #FileIn, , Chunk
  340.             Buffer = Buffer & Chunk
  341.           END IF
  342.           IF (LEN(Work) > 0) THEN
  343.             DumpOutput FileOut, Level, Work
  344.             Work = ""
  345.           END IF
  346.           Level = Level - 1
  347.           IF (MID$(Buffer, CiDx + 1, 1) = ",") THEN
  348.             DumpOutput FileOut, Level, "},"
  349.             MID$(Buffer, CiDx + 1, 1) = $TAB
  350.           ELSE
  351.             DumpOutput FileOut, Level, "}"
  352.           END IF
  353.         ELSEIF (Char = ",") THEN
  354.           Work = Work & Char
  355.           IF (LEN(Work) > 0) THEN
  356.             DumpOutput FileOut, Level, Work
  357.             Work = ""
  358.           END IF
  359.         ELSE
  360.           Work = Work & Char
  361.           IF (Char = CHR$(34)) THEN
  362.             InQuote = %TRUE
  363.           END IF
  364.           IF (Char = "[") THEN
  365.             Level = Level + 1
  366.           END IF
  367.           IF (Char = "]") THEN
  368.             Level = Level - 1
  369.           END IF
  370.         END IF ' big chain
  371.      END IF ' InQuote
  372.    END IF ' Char <> $TAB
  373.    CiDx = CiDx + 1
  374.   LOOP
  375.   CLOSE
  376.   STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Done"
  377. END SUB
  378.  
  379.  
  380. SUB DumpOutput(FileOut AS LONG, Level AS LONG, Work AS STRING)
  381.     ' this is easier to do than over testing in the above subroutine
  382.    IF (RIGHT$(Work, 1) = "[") THEN
  383.         PRINT #FileOut, FORMAT$(Level - 1) & STRING$((Level - 1) * 2, 32) & Work
  384.     ELSE
  385.         PRINT #FileOut, FORMAT$(Level) & STRING$(Level * 2, 32) & Work
  386.     END IF
  387. END SUB
  388.  
  389.  
  390. SUB SplitJson(hDlg AS LONG, JsonFile AS STRING)
  391. LOCAL FileIn, FileOut, Depth    AS LONG
  392. LOCAL LineCount                 AS LONG
  393. LOCAL Work, FileName            AS STRING
  394. LOCAL SectionPath, SectionName  AS STRING
  395. LOCAL StatusText                AS STRING
  396. LOCAL Exported                  AS LONG ' boolean value
  397.  
  398.     SectionPath = PATHNAME$(PATH, JsonFile) & PATHNAME$(NAME, JsonFile)
  399.     IF (ISTRUE ISFOLDER(SectionPath)) THEN
  400.         IF (DIR$(SectionPath &"\*.mbox") <> "") THEN
  401.             KILL SectionPath &"\*.mbox"
  402.         END IF
  403.         DIR$ CLOSE
  404.     ELSE
  405.         MKDIR SectionPath
  406.     END IF
  407.  
  408.     FileIn = FREEFILE
  409.     FileName = PATHNAME$(PATH, JsonFile) & PATHNAME$(NAME, JsonFile) &".txt"
  410.     OPEN FileName FOR INPUT AS FileIn
  411.     ' global text comes first, dispose of first line and open global
  412.    LINE INPUT #FileIn, Work
  413.     FileOut = FREEFILE
  414.     OPEN SectionPath & "\globals.mbox" FOR APPEND AS FileOut
  415.     SectionName = "globals"
  416.     LineCount = 0
  417.  
  418.     DO
  419.         LINE INPUT #FileIn, Work
  420.         Depth = VAL(Work)
  421.         IF (Depth = 1) THEN EXIT DO
  422.         IF (Depth = 3) THEN
  423.             INCR LineCount
  424.             StatusText = "Extracting "& SectionName &" "& FORMAT$(LineCount)
  425.             STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, StatusText
  426.             DIALOG DOEVENTS
  427.         END IF
  428.         Exported = %FALSE
  429.         IF ((Depth = 2) AND (INSTR(Work, "[") > 0)) THEN
  430.             ' new section, close current, open new based on section name
  431.            CLOSE FileOut
  432.             SectionName = PARSE$(TRIM$(MID$(Work, 2)), ":", 1)
  433.             REPLACE CHR$(34) WITH "" IN SectionName
  434.             FileOut = FREEFILE
  435.             OPEN SectionPath & "\" & SectionName & ".mbox" FOR APPEND AS FileOut
  436.             PRINT #FileOut, Work
  437.             Exported = %TRUE
  438.             LineCount = 0
  439.             STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, SectionName
  440.             DIALOG DOEVENTS
  441.         END IF
  442.         IF ((Depth = 2) AND (INSTR(Work, "]") > 0)) THEN
  443.             ' close current and reopen global
  444.            IF (ISFALSE Exported) THEN
  445.                 PRINT #FileOut, Work
  446.             END IF
  447.             CLOSE FileOut
  448.             FileOut = FREEFILE
  449.             OPEN SectionPath & "\globals.mbox" FOR APPEND AS FileOut
  450.             Exported = %TRUE
  451.         END IF
  452.         IF (ISFALSE Exported) THEN
  453.             PRINT #FileOut, Work
  454.         END IF
  455.     LOOP
  456.  
  457.     CLOSE FileIn
  458.     CLOSE FileOut
  459.     STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Done"
  460.  
  461. END SUB
  462.  
  463.  
  464. SUB CreateCards(hDlg AS LONG, JsonFile AS STRING, SectionName AS STRING, kJson AS LONG)
  465. LOCAL FileIn, FileOut, Depth    AS LONG
  466. LOCAL LineCount, wIndex         AS LONG
  467. LOCAL idShort                   AS LONG
  468. LOCAL Work, FileName            AS STRING
  469. LOCAL rFix, nFix                AS STRING
  470. LOCAL SectionPath, CardName     AS STRING
  471. LOCAL Char, Opener, Prefix      AS STRING
  472. LOCAL StatusText, Card          AS STRING
  473. LOCAL Exported                  AS LONG ' boolean value
  474.  
  475.     SectionPath = PATHNAME$(PATH, JsonFile) & PATHNAME$(NAME, JsonFile)
  476.     IF (ISTRUE kJson) THEN
  477.         IF (DIR$(SectionPath &"\*.json") <> "") THEN
  478.             KILL SectionPath &"\*.json"
  479.         END IF
  480.         DIR$ CLOSE
  481.     END IF
  482.  
  483.     FileIn = FREEFILE
  484.     OPEN SectionPath &"\"& SectionName FOR INPUT AS FileIn
  485.     ' dispose of first item, do until depth=2
  486.    LINE INPUT #FileIn, Work
  487.  
  488.     DO
  489.         LINE INPUT #FileIn, Work
  490.         Depth = VAL(Work)
  491.         IF (Depth = 2) THEN EXIT DO
  492.         Work = TRIM$(MID$(Work, 2))
  493.         REPLACE CHR$(34) WITH "|" IN Work
  494.         nFix = PARSE$(Work, "|", 2)
  495.         REPLACE "|"& nFix WITH "|"& Prefix & nFix IN Work
  496.         Char = RIGHT$(Work, 1)
  497.         IF ((Char = ":") OR (Char = "[")) THEN
  498.             Prefix = Prefix & nFix & "."
  499.             Opener = Opener & Char
  500.         END IF
  501.         Char = LEFT$(Work, 1)
  502.         IF ((Char = "]") AND (RIGHT$(Opener, 1) = "[") AND (Depth > 3)) THEN
  503.             IF (LEN(Prefix) > 0) THEN
  504.                 wIndex = PARSECOUNT(Prefix, ".")
  505.                 Char = PARSE$(Prefix, ".", wIndex - 1) &"."
  506.                 REPLACE Char WITH "" IN Prefix
  507.                 Opener = LEFT$(Opener, LEN(Opener) - 1)
  508.             END IF
  509.         END IF
  510.         IF ((Char = "}") AND (RIGHT$(Opener, 1) = ":") AND (Depth > 3)) THEN
  511.             IF (LEN(Prefix) > 0) THEN
  512.                 wIndex = PARSECOUNT(Prefix, ".")
  513.                 Char = PARSE$(Prefix, ".", wIndex - 1) &"."
  514.                 REPLACE Char WITH "" IN Prefix
  515.                 Opener = LEFT$(Opener, LEN(Opener) - 1)
  516.             END IF
  517.         END IF
  518.         Card = Card & $TAB & Work
  519.         IF ((Depth = 3) AND (INSTR(Work, "}") > 0)) THEN
  520.             ' we are only interested in closing brackets
  521.            IF (ISTRUE kJson) THEN
  522.                 idShort = VAL(ExtractItemValue("data.card.idShort", Card))
  523.             ELSE
  524.                 idShort = VAL(ExtractItemValue("idShort", Card))
  525.             END IF
  526.             StatusText = "Updating Card "& FORMAT$(idShort)
  527.             STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, StatusText
  528.             DIALOG DOEVENTS
  529.             FileName = SectionPath & "\card-" & FORMAT$(idShort, "000") & ".json"
  530.             FileOut = FREEFILE
  531.             OPEN FileName FOR APPEND AS FileOut
  532.             PRINT #FileOut, Card
  533.             CLOSE FileOut
  534.             Card = ""
  535.         END IF
  536.     LOOP
  537.     CLOSE FileIn
  538.     STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Done"
  539.  
  540. END SUB
  541.  
  542.  
  543. FUNCTION ExtractItemValue(Hunter AS STRING, CardItem AS STRING) AS STRING
  544. LOCAL TiDx      AS LONG
  545. LOCAL Results   AS STRING
  546.  
  547.     Results = CardItem
  548.     TiDx = INSTR(Results, "|"& Hunter &"|")
  549.     IF (TiDx = 0) THEN
  550.         ExtractItemValue = ""
  551.         EXIT FUNCTION
  552.     END IF
  553.  
  554.     Results = MID$(Results, TiDx)
  555.     Results = PARSE$(Results, $TAB, 1)
  556.     TiDx = INSTR(Results, ":")
  557.     Results = MID$(Results, TiDx + 1)
  558.  
  559.     IF (RIGHT$(Results, 1) = ",") THEN
  560.         Results = LEFT$(Results, LEN(Results) - 1)
  561.     END IF
  562.  
  563.     ExtractItemValue = Results
  564. END FUNCTION
  565.  
  566.  
  567. FUNCTION ExtractDataBlock(Hunter AS STRING, CardItem AS STRING) AS STRING
  568. LOCAL TiDx      AS LONG
  569. LOCAL Results   AS STRING
  570.  
  571.     Results = CardItem
  572.     TiDx = INSTR(Results, "|"& Hunter &"|")
  573.     IF (TiDx = 0) THEN
  574.         ExtractDataBlock = ""
  575.         EXIT FUNCTION
  576.     END IF
  577.     Results = MID$(Results, TiDx)
  578.     TiDx = INSTR(Results, "]")
  579.     IF (TiDx = 0) THEN
  580.         ExtractDataBlock = ""
  581.         EXIT FUNCTION
  582.     END IF
  583.     Results = LEFT$(Results, TiDx)
  584.     ExtractDataBlock = Results
  585. END FUNCTION
  586.  
  587.  
  588. SUB ExtractRecentActions(hDlg AS LONG, JsonFile AS STRING)
  589. LOCAL FileIn, FileOut, Depth    AS LONG
  590. LOCAL LineCount, cIndex, cCount AS LONG
  591. LOCAL ActionIndex, CardIndex    AS LONG
  592. LOCAL ActionCount, CheckIndex   AS LONG
  593. LOCAL idShort, ThisID           AS LONG
  594. LOCAL Cards(), Actions()        AS STRING
  595. LOCAL CheckLists()              AS STRING
  596. LOCAL Work, FileName            AS STRING
  597. LOCAL rFix, nFix, shortUrl      AS STRING
  598. LOCAL SectionPath               AS STRING
  599. LOCAL Char, Opener, Prefix      AS STRING
  600. LOCAL StatusText, Card          AS STRING
  601. LOCAL Extraction                AS LONG ' boolean value
  602.  
  603. LOCAL CardType, CardText, CardUser          AS STRING
  604. LOCAL CardDate, ListBefore, ListAfter       AS STRING
  605. LOCAL CardDesc, CardName, CardData          AS STRING
  606. LOCAL CardAttachment, CardAttachmentURL     AS STRING
  607. LOCAL CardChecklist, CardCheckID            AS STRING
  608. LOCAL CardLabels, DataBlock, CardColor      AS STRING
  609.  
  610.     SectionPath = PATHNAME$(PATH, JsonFile) & PATHNAME$(NAME, JsonFile)
  611.  
  612.     ActionIndex = 0
  613.     REDIM Actions(ActionIndex)
  614.  
  615.     FetchDataSet hDlg, JsonFile, "\actions.mbox", Actions()
  616.  
  617.     IF (UBOUND(Actions) = 0) THEN
  618.         STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Failed Action Extraction"
  619.         EXIT SUB
  620.     END IF
  621.  
  622.     FOR ActionIndex = 0 TO UBOUND(Actions)
  623.         idShort = VAL(ExtractItemValue("idShort", Actions(ActionIndex)))
  624.         Actions(ActionIndex) = ExtractItemValue("date", Actions(ActionIndex))  & Actions(ActionIndex)
  625.     NEXT ActionIndex
  626.  
  627.     ARRAY SORT Actions(), DESCEND
  628.  
  629.  
  630.     CardIndex = 0
  631.     REDIM Cards(CardIndex)
  632.  
  633.     FetchDataSet hDlg, JsonFile, "\cards.mbox", Cards()
  634.  
  635.     IF (UBOUND(Cards) = 0) THEN
  636.         STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Failed Card Extraction"
  637.         EXIT SUB
  638.     END IF
  639.  
  640.     FOR CardIndex = 0 TO UBOUND(Cards)
  641.         idShort = VAL(ExtractItemValue("idShort", Cards(CardIndex)))
  642.         Cards(CardIndex) = FORMAT$(idShort, "000")  & Cards(CardIndex)
  643.     NEXT CardIndex
  644.  
  645.     ARRAY SORT Cards(), ASCEND
  646.  
  647.  
  648.     REDIM CheckLists(0)
  649.     FetchDataSet hDlg, JsonFile, "\checklists.mbox", CheckLists()
  650.  
  651.  
  652.     FileOut = FREEFILE
  653.     FileName = PATHNAME$(PATH, JsonFile) &"actions.txt"
  654.     OPEN FileName FOR OUTPUT AS FileOut
  655.     PRINT #FileOut, Actions()
  656.     CLOSE FileOut
  657.     FileOut = FREEFILE
  658.     FileName = PATHNAME$(PATH, JsonFile) &"cards.txt"
  659.     OPEN FileName FOR OUTPUT AS FileOut
  660.     PRINT #FileOut, Cards()
  661.     CLOSE FileOut
  662.     FileOut = FREEFILE
  663.     FileName = PATHNAME$(PATH, JsonFile) &"checklists.txt"
  664.     OPEN FileName FOR OUTPUT AS FileOut
  665.     PRINT #FileOut, CheckLists()
  666.     CLOSE FileOut
  667.  
  668.  
  669.     ActionCount = 0
  670.     FileOut = FREEFILE
  671.     FileName = JsonFile
  672.     REPLACE ".json" WITH ".htm" IN FileName
  673.     OPEN FileName FOR OUTPUT AS FileOut
  674.     PRINT #FileOut, "<head>"
  675.     PRINT #FileOut, "<title>Recent Activity</title>"
  676.     PRINT #FileOut, "<style>"
  677.     PRINT #FileOut, "<!--"
  678.     PRINT #FileOut, ".green-label { background-color: #34B27D; width: 100% }"
  679.     PRINT #FileOut, ".yellow-label { background-color: #DBDB57; width: 100% }"
  680.     PRINT #FileOut, ".orange-label { background-color: #E09952; width: 100% }"
  681.     PRINT #FileOut, ".red-label   { background-color: #CB4D4D; width: 100% }"
  682.     PRINT #FileOut, ".purple-label { background-color: #9933CC; width: 100% }"
  683.     PRINT #FileOut, ".blue-label  { background-color: #4D77CB; width: 100% }"
  684.     PRINT #FileOut, "-->"
  685.     PRINT #FileOut, "</style>"
  686.     PRINT #FileOut, "</head>"
  687.     PRINT #FileOut, "<body>"
  688.     PRINT #FileOut, FormatHtml("<table border=|1| width=|100%|>")
  689.     DO
  690.         Extraction = %TRUE
  691.         ThisID = 0
  692.         FOR ActionIndex = 0 TO UBOUND(Actions)
  693.             IF (LEN(Actions(ActionIndex)) > 0) THEN
  694.                 CardType = ExtractItemValue("type", Actions(ActionIndex))
  695.                 idShort = VAL(ExtractItemValue("data.card.idShort", Actions(ActionIndex)))
  696.                 IF ((ThisID = 0) OR (ThisID = idShort)) THEN
  697.                     IF (ThisId = 0) THEN
  698.                         PRINT #FileOut, "</table>"
  699.                         PRINT #FileOut, "<hr />"
  700.                         PRINT #FileOut, FormatHtml("<table border=|1| width=|100%|>")
  701.                         FOR CardIndex = 0 TO UBOUND(Cards)
  702.                             ThisId = VAL(ExtractItemValue("idShort", Cards(CardIndex)))
  703.                             IF (ThisID = idShort) THEN
  704.                                 shortUrl = Simple(ExtractItemValue("shortUrl", Cards(CardIndex)))
  705.                                 CardName = PrintPretty(Simple(ExtractItemValue("name", Cards(CardIndex))))
  706.                                 CardDesc = PrintPretty(ExtractItemValue("desc", Cards(CardIndex)))
  707.                                 CardDate = WindowsTime(Simple(ExtractItemValue("dateLastActivity", Cards(CardIndex))))
  708.                                 CardName = "<a href=|" & shortUrl & "| target=|trello|>" & CardName & "</a>"
  709.                                 IF (ExtractItemValue("labels", Cards(CardIndex)) = "[") THEN
  710.                                     DataBlock = ExtractDataBlock("labels", Cards(CardIndex))
  711.                                     CardLabels = "<br />"
  712.                                     cIndex = 1 ' we break on the closing curly bracket
  713.                                    DO
  714.                                         Work = PARSE$(DataBlock, "}", cIndex)
  715.                                         CardColor = Simple(ExtractItemValue("labels.color", Work))
  716.                                         IF (LEN(CardColor) = 0) THEN EXIT DO
  717.                                         CardLabels = CardLabels _
  718.                                             &"<div class=|"& CardColor &"-label|>" _
  719.                                             & Simple(ExtractItemValue("labels.name", Work)) &"</div>"
  720.                                         INCR cIndex
  721.                                     LOOP
  722.                                 END IF
  723.                                 PRINT #FileOut, "<tr>"
  724.                                 PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & CardLabels & "</td>")
  725.                                 PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName & "<br>")
  726.                                 IF (LEN(CardDesc) > 0) THEN
  727.                                     PRINT #FileOut, FormatHtml("Desc: " & CardDesc & "</td>")
  728.                                     PRINT #FileOut, "</tr>"
  729.                                 ELSE
  730.                                     PRINT #FileOut, "</td>"
  731.                                     PRINT #FileOut, "</tr>"
  732.                                 END IF
  733.                                 Cards(CardIndex) = ""
  734.                                 ActionCount = 0
  735.                                 EXIT FOR
  736.                             END IF
  737.                         NEXT CardIndex
  738.                     END IF
  739.                     INCR ActionCount
  740.                     StatusText = "Printing Card "& FORMAT$(idShort) &"."& FORMAT$(ActionCount)
  741.                     STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, StatusText
  742.                     DIALOG DOEVENTS
  743.                     ThisID = idShort
  744.                     Extraction = %FALSE
  745.                     CardType = ExtractItemValue("type", Actions(ActionIndex))
  746.                     CardUser = Simple(ExtractItemValue("memberCreator.fullName", Actions(ActionIndex)))
  747.                     CardName = PrintPretty(Simple(ExtractItemValue("data.card.name", Actions(ActionIndex))))
  748.                     CardDate = WindowsTime(Simple(ExtractItemValue("date", Actions(ActionIndex))))
  749.                     SELECT CASE CardType
  750.                         CASE "|addAttachmentToCard|"
  751.                             CardAttachmentUrl = Simple(ExtractItemValue("data.attachment.url", Actions(ActionIndex)))
  752.                             CardAttachment = Simple(ExtractItemValue("data.attachment.name", Actions(ActionIndex)))
  753.                             Work = "<a href=|" & CardAttachmentUrl & "| target=|trello|>" & CardAttachment & "</a>"
  754.                             PRINT #FileOut, "<tr>"
  755.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  756.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  757.                             IF (LEN(CardAttachmentUrl) > 0) THEN
  758.                                 PRINT #FileOut, FormatHtml("<br>" & CardUser & " attached " & Work & "</td>")
  759.                             ELSE
  760.                                 PRINT #FileOut, FormatHtml("<br>" & CardUser & " attached <strike>" & CardAttachment & "</strike></td>")
  761.                             END IF
  762.                             PRINT #FileOut, "</tr>"
  763.                         CASE "|addChecklistToCard|"
  764.                             CardData = ExtractItemValue("data.checklist.name", Actions(ActionIndex))
  765.                             PRINT #FileOut, "<tr>"
  766.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  767.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  768.                             PRINT #FileOut, FormatHtml("<br>" & CardUser & " Added Checklist "& CardData)
  769.                             CardCheckID = ExtractItemValue("data.checklist.id", Actions(ActionIndex))
  770.                             FOR CheckIndex = 0 TO UBOUND(CheckLists)
  771.                                 IF (ExtractItemValue("id", CheckLists(CheckIndex)) = CardCheckID) THEN
  772.                                     PRINT #FileOut, "<ul>"
  773.                                     cCount = PARSECOUNT(CheckLists(CheckIndex), $TAB)
  774.                                     FOR cIndex = 1 TO cCount
  775.                                         CardCheckList = PARSE$(CheckLists(CheckIndex), $TAB, cIndex)
  776.                                         Work = Simple(ExtractItemValue("checkItems.name", CardCheckList))
  777.                                         IF (LEN(Work) > 0) THEN
  778.                                             PRINT #FileOut, "<li>"& Work
  779.                                         END IF
  780.                                     NEXT cIndex
  781.                                     PRINT #FileOut, "</ul>"
  782.                                     EXIT FOR
  783.                                 END IF
  784.                             NEXT CheckIndex
  785.                             IF (CheckIndex > UBOUND(CheckLists)) THEN
  786.                                 PRINT #FileOut, "<ul>"
  787.                                 PRINT #FileOut, "<li>Check List NOT Found"
  788.                                 PRINT #FileOut, "</ul>"
  789.                             END IF
  790.                             PRINT #FileOut, "</td>"
  791.                             PRINT #FileOut, "</tr>"
  792.                         CASE "|addMemberToBoard|"
  793.                             ' this should not be found
  794.                            PRINT #FileOut, "<tr>"
  795.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  796.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  797.                             PRINT #FileOut, FormatHtml("<br>" & CardUser & " Added Member To Board</td>")
  798.                         CASE "|addMemberToCard|"
  799.                             PRINT #FileOut, "<tr>"
  800.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  801.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  802.                             PRINT #FileOut, FormatHtml("<br>" & CardUser & " joined Card</td>")
  803.                             PRINT #FileOut, "</tr>"
  804.                         CASE "|commentCard|"
  805.                             CardText = PrintPretty(ExtractItemValue("data.text", Actions(ActionIndex)))
  806.                             PRINT #FileOut, "<tr>"
  807.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  808.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName & "<br>")
  809.                             PRINT #FileOut, FormatHtml("Comment: " & CardUser)
  810.                             PRINT #FileOut, FormatHtml("<p>" & CardText & "</p>")
  811.                             PRINT #FileOut, "</td>"
  812.                             PRINT #FileOut, "</tr>"
  813.                         CASE "|createCard|"
  814.                             PRINT #FileOut, "<tr>"
  815.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  816.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  817.                             PRINT #FileOut, FormatHtml("<br>" & CardUser & " Created Card</td>")
  818.                             PRINT #FileOut, "</tr>"
  819.                         CASE "|deleteAttachmentFromCard|"
  820.                             CardAttachment = Simple(ExtractItemValue("data.attachment.name", Actions(ActionIndex)))
  821.                             PRINT #FileOut, "<tr>"
  822.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  823.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  824.                             PRINT #FileOut, FormatHtml("<br>" & CardUser & " deleted " & CardAttachment & "</td>")
  825.                             PRINT #FileOut, "</tr>"
  826.                         CASE "|updateCard|"
  827.                             ListAfter = Simple(ExtractItemValue("data.listAfter.name", Actions(ActionIndex)))
  828.                             IF (LEN(ListAfter) > 0) THEN
  829.                                 ListBefore = Simple(ExtractItemValue("data.listBefore.name", Actions(ActionIndex)))
  830.                                 PRINT #FileOut, "<tr>"
  831.                                 PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  832.                                 PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  833.                                 PRINT #FileOut, FormatHtml("<br>--- " & CardUser & " Moved Card To " & ListAfter & "</td>")
  834.                                 PRINT #FileOut, "</tr>"
  835.                             END IF
  836.                             Work = Simple(ExtractItemValue("data.card.closed", Actions(ActionIndex)))
  837.                             IF (Work = "true") THEN
  838.                                 PRINT #FileOut, "<tr>"
  839.                                 PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  840.                                 PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  841.                                 PRINT #FileOut, FormatHtml("<br>--- " & CardUser & " Closed this card.</td>")
  842.                                 PRINT #FileOut, "</tr>"
  843.                             END IF
  844.                         CASE "|updateCheckItemStateOnCard|"
  845.                             ' we are not going to parse this?
  846.                        CASE "|updateChecklist|"
  847.                             CardData = ExtractItemValue("data.checklist.name", Actions(ActionIndex))
  848.                             PRINT #FileOut, "<tr>"
  849.                             PRINT #FileOut, FormatHtml("<td width=|10%| valign=|top|>Card: " & FORMAT$(idShort) & "</td>")
  850.                             PRINT #FileOut, FormatHtml("<td width=|90%| valign=|top|>" & CardDate & " - " & CardName)
  851.                             PRINT #FileOut, FormatHtml("<br>" & CardUser & " Updated Checklist "& CardData &"</td>")
  852.                             PRINT #FileOut, "</tr>"
  853.                     END SELECT
  854.                     Actions(ActionIndex) = ""
  855.                 END IF
  856.             END IF
  857.         NEXT ActionIndex
  858.         IF (ISTRUE Extraction) THEN EXIT DO
  859.     LOOP
  860.  
  861.     PRINT #FileOut, "</table>"
  862.     PRINT #FileOut, "</body>"
  863.     CLOSE FileOut
  864.  
  865.     STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Done"
  866.  
  867. END SUB
  868.  
  869.  
  870. FUNCTION FormatHtml(Work AS STRING) AS STRING
  871. DIM Results AS STRING
  872.  
  873.   Results = Work
  874.   REPLACE CHR$(34) WITH "&quot;" IN Results
  875.   REPLACE "|" WITH CHR$(34) IN Results
  876.   REPLACE $CRLF WITH "<br />"& $CRLF IN Results
  877.   REPLACE "<p></p>" WITH ""& $CRLF IN Results
  878.  
  879.   FormatHtml = Results
  880. END FUNCTION
  881.  
  882.  
  883. FUNCTION Simple(CardItem AS STRING) AS STRING
  884. LOCAL Results       AS STRING
  885.     Results = CardItem
  886.     REPLACE "<" WITH "&lt;" IN Results
  887.     REPLACE ">" WITH "&gt;" IN Results
  888.     REPLACE "|" WITH "" IN Results
  889.     Simple = Results
  890. END FUNCTION
  891.  
  892.  
  893. FUNCTION PrintPretty(CardItem AS STRING) AS STRING
  894. ' this is where all the parsing and making pretty happen for those cards that need it
  895. LOCAL Results       AS STRING
  896.  
  897.     ' strip trailing comma
  898.    Results = CardItem
  899.     IF (RIGHT$(Results, 1) = ",") THEN
  900.         Results = LEFT$(Results, LEN(Results) - 1)
  901.     END IF
  902.     ' convert escaped elements
  903.    IF (INSTR(Results, "\") > 0) THEN
  904.         REPLACE "\n" WITH $CRLF IN Results
  905.         REPLACE "\|" WITH CHR$(34) IN Results
  906.         REPLACE "\\`" WITH "`" IN Results
  907.         REPLACE "\\@" WITH "@" IN Results
  908.         REPLACE "\\#" WITH "#" IN Results
  909.         REPLACE "\\*" WITH "*" IN Results
  910.         REPLACE "\\" WITH "\" IN Results
  911.     END IF
  912.     PrintPretty = Simple(Results)
  913. END FUNCTION
  914.  
  915.  
  916. FUNCTION WindowsTime(CardItem AS STRING) AS STRING
  917. LOCAL DateString, TimeString        AS STRING
  918.  
  919.     DateString = PARSE$(CardItem, "T", 1)
  920.     TimeString = PARSE$(CardItem, "T", 2)
  921.     TimeString = PARSE$(TimeString, ".", 1)
  922.     WindowsTime = DateString &" "& TimeString
  923.  
  924. END FUNCTION
  925.  
  926.  
  927. FUNCTION EnumerateItemNames(Hunter AS STRING, CardItem AS STRING, PriorHunter AS STRING) AS STRING
  928. LOCAL Work  AS STRING
  929. LOCAL TiDx  AS LONG
  930.  
  931.     Work = CardItem
  932.     IF (LEN(PriorHunter) > 0) THEN
  933.         TiDx = INSTR(Work, PriorHunter)
  934.         Work = MID$(Work, TiDx + LEN(PriorHunter))
  935.     END IF
  936.     TiDx = INSTR(Work, "|"& Hunter)
  937.     IF (TiDx = 0) THEN
  938.         EnumerateItemNames = ""
  939.         EXIT FUNCTION
  940.     END IF
  941.  
  942.     Work = MID$(Work, TiDx)
  943.     Work = PARSE$(Work, $TAB, 1)
  944.     TiDx = INSTR(Work, ":")
  945.     Work = LEFT$(Work, TiDx - 1)
  946.  
  947.     EnumerateItemNames = Work
  948. END FUNCTION
  949.  
  950.  
  951. SUB AnalyzeActions(hDlg AS LONG, JsonFile AS STRING)
  952. LOCAL FileOut                   AS LONG
  953. LOCAL ActionIndex               AS LONG
  954. LOCAL ActionNames()             AS STRING
  955. LOCAL Work, FileName            AS STRING
  956. LOCAL ActionTypes, Samples      AS STRING
  957. LOCAL StatusText                AS STRING
  958.  
  959. LOCAL CardType, Hunter, PriorHunter     AS STRING
  960.  
  961.     ActionIndex = 0
  962.     REDIM ActionNames(ActionIndex)
  963.     FetchDataSet hDlg, JsonFile, "\actions.mbox", ActionNames()
  964.  
  965.     IF (UBOUND(ActionNames) = 0) THEN
  966.         STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Failed Action Extraction"
  967.     END IF
  968.  
  969.     FOR ActionIndex = 0 TO UBOUND(ActionNames)
  970.         StatusText = "Analyzing Action "& FORMAT$(ActionIndex)
  971.         STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, StatusText
  972.         DIALOG DOEVENTS
  973.         CardType = ExtractItemValue("type", ActionNames(ActionIndex))
  974.         IF (INSTR(ActionTypes, CardType) = 0) THEN
  975.             ActionTypes = ActionTypes & CardType
  976.         END IF
  977.         IF (CardType = "|updateCard|") THEN
  978.             Hunter = "data"
  979.             PriorHunter = ""
  980.             DO
  981.                 Work = EnumerateItemNames(Hunter, ActionNames(ActionIndex), PriorHunter)
  982.                 IF (LEN(Work) = 0) THEN EXIT DO
  983.                 PriorHunter = Work
  984.                 IF (INSTR(Samples, Work) = 0) THEN
  985.                     Samples = Samples & Work &":"& ExtractItemValue(Simple(Work), ActionNames(ActionIndex)) & $TAB
  986.                 END IF
  987.             LOOP
  988.         END IF
  989.     NEXT ActionIndex
  990.  
  991.     FileOut = FREEFILE
  992.     FileName = PATHNAME$(PATH, JsonFile) &"analyze.txt"
  993.     OPEN FileName FOR OUTPUT AS FileOut
  994.     REPLACE "||" WITH "|"& $TAB &"|" IN ActionTypes
  995.     ActionIndex = 1
  996.     DO
  997.         Work = PARSE$(ActionTypes, $TAB, ActionIndex)
  998.         IF (LEN(Work) = 0) THEN EXIT DO
  999.         PRINT #FileOut, Work
  1000.         INCR ActionIndex
  1001.     LOOP
  1002.  
  1003.     ActionIndex = 1
  1004.     DO
  1005.         Work = PARSE$(Samples, $TAB, ActionIndex)
  1006.         IF (LEN(Work) = 0) THEN EXIT DO
  1007.         PRINT #FileOut, Work
  1008.         INCR ActionIndex
  1009.     LOOP
  1010.  
  1011.     CLOSE FileOut
  1012.     STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, "Done"
  1013.  
  1014. END SUB
  1015.  
  1016.  
  1017. SUB FetchDataSet(hDlg AS DWORD, JsonFile AS STRING, BoxName AS STRING, BYREF DataItems() AS STRING)
  1018. LOCAL FileIn, DataIndex, Depth      AS LONG
  1019. LOCAL wIndex, idShort               AS LONG
  1020. LOCAL StatusText                    AS STRING
  1021. LOCAL SectionPath, Work, Card       AS STRING
  1022. LOCAL nFix, Prefix, Char, Opener    AS STRING
  1023.  
  1024.     SectionPath = PATHNAME$(PATH, JsonFile) & PATHNAME$(NAME, JsonFile)
  1025.     FileIn = FREEFILE
  1026.     OPEN SectionPath & BoxName FOR INPUT AS FileIn
  1027.     ' dispose of first item, do until depth=2
  1028.    LINE INPUT #FileIn, Work
  1029.  
  1030.     DataIndex = 0
  1031.     REDIM DataItems(DataIndex)
  1032.  
  1033.     DO
  1034.         LINE INPUT #FileIn, Work
  1035.         Depth = VAL(Work)
  1036.         IF (Depth = 2) THEN EXIT DO
  1037.         Work = TRIM$(MID$(Work, 2))
  1038.         REPLACE CHR$(34) WITH "|" IN Work
  1039.         nFix = PARSE$(Work, "|", 2)
  1040.         REPLACE "|"& nFix WITH "|"& Prefix & nFix IN Work
  1041.         Char = RIGHT$(Work, 1)
  1042.         IF ((Char = ":") OR (Char = "[")) THEN
  1043.             Prefix = Prefix & nFix & "."
  1044.             Opener = Opener & Char
  1045.         END IF
  1046.         Char = LEFT$(Work, 1)
  1047.         IF ((Char = "]") AND (RIGHT$(Opener, 1) = "[") AND (Depth > 3)) THEN
  1048.             IF (LEN(Prefix) > 0) THEN
  1049.                 wIndex = PARSECOUNT(Prefix, ".")
  1050.                 Char = PARSE$(Prefix, ".", wIndex - 1) &"."
  1051.                 REPLACE Char WITH "" IN Prefix
  1052.                 Opener = LEFT$(Opener, LEN(Opener) - 1)
  1053.             END IF
  1054.         END IF
  1055.         IF ((Char = "}") AND (RIGHT$(Opener, 1) = ":") AND (Depth > 3)) THEN
  1056.             IF (LEN(Prefix) > 0) THEN
  1057.                 wIndex = PARSECOUNT(Prefix, ".")
  1058.                 Char = PARSE$(Prefix, ".", wIndex - 1) &"."
  1059.                 REPLACE Char WITH "" IN Prefix
  1060.                 Opener = LEFT$(Opener, LEN(Opener) - 1)
  1061.             END IF
  1062.         END IF
  1063.         Card = Card & $TAB & Work
  1064.         IF ((Depth = 3) AND (INSTR(Work, "}") > 0)) THEN
  1065.             ' we are only interested in closing brackets
  1066.            StatusText = "Reading "& BoxName &": "& FORMAT$(DataIndex)
  1067.             STATUSBAR SET TEXT hDlg, %IDSB_STATUS, 1, 0, StatusText
  1068.             DIALOG DOEVENTS
  1069.             REDIM PRESERVE DataItems(DataIndex)
  1070.             DataItems(DataIndex) = Card
  1071.             INCR DataIndex
  1072.             Card = ""
  1073.         END IF
  1074.     LOOP
  1075.  
  1076.     CLOSE FileIn
  1077. END SUB
Advertisement
Add Comment
Please, Sign In to add comment