Skip to content

Commit

Permalink
downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
miyako committed Mar 8, 2024
1 parent 4dcf08d commit 54f64be
Show file tree
Hide file tree
Showing 15 changed files with 570 additions and 140 deletions.
9 changes: 7 additions & 2 deletions SAMPLE/Project/Sources/Methods/TEST.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
*/

var $rezept : cs:C1710.ssk.Rezept
cs:C1710.ssk.GitHub.new() //最新のマスターを非同期ダウンロードする

var $rezept : cs:C1710.ssk.Rezept
$rezept:=cs:C1710.ssk.Rezept.new()

$info:=$rezept.getInfo()
If (False:C215)
$rezept.switch($info.data[1]) //マスターを切り替える
End if

$コメント:=$rezept.get("コメント"; "810000001")

$単位:=$rezept.get("単位"; "1")
Expand All @@ -20,5 +26,4 @@ $診療行為:=$rezept.get("診療行為"; "160230050")
$医薬品:=$rezept.医薬品.query("後発品.項目.同一剤形・規格の後発医薬品がある先発医薬品 == :1"; "○")
$医薬品:=$rezept.get("医薬品"; "610406079")


$公費:=$rezept.公費().parse("9947")
144 changes: 144 additions & 0 deletions rezept/Project/Sources/Classes/GitHub.4dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
Class constructor

This:C1470.endpoint:="https://api.github.com/repos/miyako/4d-component-ssk-master/releases"

$options:=OB Copy:C1225(This:C1470)

$options.dataType:="text"
$options.automaticRedirections:=True:C214

CALL WORKER:C1389(1; This:C1470._fetch; $options)

Function _fetch($options : Object)

$request:=4D:C1709.HTTPRequest.new($options.endpoint; $options)

Function onData($request : 4D:C1709.HTTPRequest; $event : Object)

Function onError($request : 4D:C1709.HTTPRequest; $event : Object)

Function onHeaders($request : 4D:C1709.HTTPRequest; $event : Object)

Function onResponse($request : 4D:C1709.HTTPRequest; $event : Object)

If ($request.response.status=200)

Case of
: ($request.dataType="text")

var $bodyText : Text
$bodyText:=$request.response.body

var $releases : Collection
$releases:=JSON Parse:C1218($bodyText; Is collection:K8:32)
$releases:=$releases.extract("tag_name"; "tag"; "name"; "name"; "assets"; "assets"; "published_at"; "published_at").orderBy("published_at desc")

For each ($release; $releases)
This:C1470.get($release)
End for each

: ($request.dataType="blob")

var $body : 4D:C1709.Blob
$body:=$request.response.body
$folder:=This:C1470.asset.folder
$tempFolder:=Folder:C1567(Temporary folder:C486; fk platform path:K87:2).folder(Generate UUID:C1066)
$tempFolder.create()
$file:=$tempFolder.file(This:C1470.name+".zip")
$file.setContent($body)

$zip:=ZIP Read archive:C1637($file)
$files:=$zip.root.files()

If ($files.length#0)

$files[0].copyTo(This:C1470.folder; This:C1470.name+".data")
$names:=["記載事項等"; "単位"; "医薬品"; "傷病名"; "地方公費"; "特定器材"; "修飾語"; "診療行為"; "コメント"]
$files:=This:C1470.folder.files(fk ignore invisible:K87:22).query("extension == :1 and name in :2"; ".data"; $names)

If ($files.length=$names.length)

$manifest:={name: This:C1470.release.name; tag: This:C1470.release.tag; published_at: This:C1470.release.published_at}

var $export : cs:C1710._Export
$export:=cs:C1710._Export.new()

$export.setManifest(This:C1470.folder; $manifest)

End if
End if

End case

End if

Function onTerminate($request : 4D:C1709.HTTPRequest; $event : Object)

Function _download($options : Object)

For each ($asset; $options.release.assets)
Case of
: ($asset.name="data.@.zip")

$options:=OB Copy:C1225($options)
$options.asset:=$asset

Case of
: ($asset.name="data.1.zip")
$options.name:="コメント"
: ($asset.name="data.2.zip")
$options.name:="医薬品"
: ($asset.name="data.3.zip")
$options.name:="記載事項等"
: ($asset.name="data.4.zip")
$options.name:="修飾語"
: ($asset.name="data.5.zip")
$options.name:="傷病名"
: ($asset.name="data.6.zip")
$options.name:="診療行為"
: ($asset.name="data.7.zip")
$options.name:="単位"
: ($asset.name="data.8.zip")
$options.name:="地方公費"
: ($asset.name="data.9.zip")
$options.name:="特定器材"
Else
continue
End case

$request:=4D:C1709.HTTPRequest.new($asset.browser_download_url; $options)

End case
End for each

Function _getDataFolder($tag : Text)

return cs:C1710._Export.new()._getDataFolder().folder($tag)

Function get($release : Object)

var $folder : 4D:C1709.Folder
$folder:=This:C1470._getDataFolder($release.tag)
If (Not:C34($folder.exists))
$folder.create()
$options:=OB Copy:C1225(This:C1470)
$options.folder:=$folder
$options.release:=$release
$options.dataType:="blob"
$options.automaticRedirections:=True:C214
CALL WORKER:C1389(1; This:C1470._download; $options)
End if

Function fetch() : cs:C1710.GitHub

$url:=This:C1470.endpoint

var $response : Text
$status:=HTTP Get:C1157($url; $response)

If ($status=200)
$releases:=JSON Parse:C1218($response; Is collection:K8:32)
This:C1470.releases:=$releases.extract("tag_name"; "tag"; "name"; "name"; "assets"; "assets"; "published_at"; "published_at").orderBy("published_at desc")
End if

return This:C1470
113 changes: 98 additions & 15 deletions rezept/Project/Sources/Classes/_export.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,79 @@ Class constructor($dataClassName : Text)
This:C1470.code:={}
This:C1470.file:=Null:C1517

Function _getDataFolder() : 4D:C1709.Folder
$folder:=This:C1470._getDataFolder()

This:C1470._dataFolder:=Folder:C1567("/RESOURCES/")

var $manifest : Object
$manifest:=This:C1470.getManifest()
If ($manifest#Null:C1517) && ($manifest.file.exists)
This:C1470._dataFolder:=$manifest.file.parent
End if

Function getInfo() : Object

var $manifests; $manifest : Collection
$manifests:=This:C1470._scanDataFolder()

$info:={data: $manifests}

$manifest:=$manifests.query("active == true")
If ($manifest.length#0)
$info.active:=$manifest[0]
End if

return $info

Function getManifest() : Object

$manifests:=This:C1470._scanDataFolder().query("active == true")

If ($manifests.length#0)
return $manifests[0]
End if

Function setManifest($folder : 4D:C1709.Folder; $manifest : Object) : cs:C1710._Export

This:C1470._unsetManifest()

$manifest.active:=True:C214

$folder.file("manifest.json").setText(JSON Stringify:C1217($manifest; *))

return This:C1470

Function _unsetManifest() : cs:C1710._Export

$manifests:=This:C1470._getManifests()

For each ($file; $manifests)
$manifest:=$file.getText()
$json:=JSON Parse:C1218($manifest)
$json.active:=False:C215
$manifest:=JSON Stringify:C1217($json)
$file.setText($manifest)
End for each

return This:C1470

Function _getManifests() : Collection

return This:C1470._getDataFolder().files(fk ignore invisible:K87:22 | fk recursive:K87:7).query("fullName == :1"; "manifest.json")

Function _scanDataFolder() : Collection

$manifests:=[]

For each ($file; This:C1470._getManifests())
$manifest:=JSON Parse:C1218($file.getText())
$manifest.file:=$file
$manifests.push($manifest)
End for each

return $manifests

Function _getDataFolder() : 4D:C1709.Folder

var $folder : 4D:C1709.Folder
$folder:=Folder:C1567(fk user preferences folder:K87:10).parent
Expand All @@ -20,14 +90,6 @@ Function _isComponent() : Boolean

return (Folder:C1567(fk database folder:K87:14).platformPath#Folder:C1567(fk database folder:K87:14; *).platformPath)

Function _loadCollection($folder : 4D:C1709.Folder; $name : Text) : Collection

$json:=$folder.file($name+".json").getText("utf-8"; Document with LF:K24:22)

$col:=JSON Parse:C1218($json; Is collection:K8:32)

return $col

Function _link($dataClassName : Text; $code : Text; $collection : Collection; $data : Collection) : Boolean

var $elements : Collection
Expand Down Expand Up @@ -56,7 +118,15 @@ Function _link($dataClassName : Text; $code : Text; $collection : Collection; $d

End case

Function str_trim($src : Text) : Text
Function _loadCollection($folder : 4D:C1709.Folder; $name : Text) : Collection

$json:=$folder.file($name+".json").getText("utf-8"; Document with LF:K24:22)

$col:=JSON Parse:C1218($json; Is collection:K8:32)

return $col

Function _trim($src : Text) : Text

$dst:=$src

Expand All @@ -72,6 +142,8 @@ Function str_trim($src : Text) : Text

return $dst

//MARK:setup

Function setup_k($診療行為 : Collection; $特定器材 : Collection; $コメント : Collection)

$dataClassName:=This:C1470.dataClassName
Expand Down Expand Up @@ -314,7 +386,7 @@ Function setup_k($診療行為 : Collection; $特定器材 : Collection; $コメ
$診療行為名称等:=Delete string:C232($診療行為名称等; $pos{1}; $len{1})
End while

$診療行為名称等:=This:C1470.str_trim($診療行為名称等)
$診療行為名称等:=This:C1470._trim($診療行為名称等)

$o.診療行為名称:=$診療行為名称等

Expand Down Expand Up @@ -646,14 +718,16 @@ Function setup_k($診療行為 : Collection; $特定器材 : Collection; $コメ
$CLI.print($asset.path; "244").LF()
$CLI.print("size: "; "bold").print(String:C10($file.size); "39").LF()

$file.copyTo(Folder:C1567("/RESOURCES/"); fk overwrite:K87:5)

End if
End if

End if

Else

$file:=This:C1470._getDataFolder().file($dataClassName+".data")
$file:=This:C1470._dataFolder.file($dataClassName+".data")

If ($file#Null:C1517) && ($file.exists)

Expand Down Expand Up @@ -851,13 +925,16 @@ Function setup_t()
$CLI.print($asset.path; "244").LF()
$CLI.print("size: "; "bold").print(String:C10($file.size); "39").LF()

$file.copyTo(Folder:C1567("/RESOURCES/"); fk overwrite:K87:5)

Else

$file:=This:C1470._getDataFolder().file($dataClassName+".data")
$file:=This:C1470._dataFolder.file($dataClassName+".data")

If ($file#Null:C1517) && ($file.exists)

$data:=$file.getContent()

BLOB TO VARIABLE:C533($data; $object)

This:C1470[$dataClassName]:=$object[$dataClassName]
Expand Down Expand Up @@ -941,15 +1018,18 @@ Function setup_i()
$CLI.print($asset.path; "244").LF()
$CLI.print("size: "; "bold").print(String:C10($file.size); "39").LF()

$file.copyTo(Folder:C1567("/RESOURCES/"); fk overwrite:K87:5)

Else

$file:=This:C1470._getDataFolder().file($dataClassName+".data")
$file:=This:C1470._dataFolder.file($dataClassName+".data")

If ($file#Null:C1517) && ($file.exists)

$data:=$file.getContent()

BLOB TO VARIABLE:C533($data; $object)

$object:=OB Copy:C1225($object; ck shared:K85:29; This:C1470)

This:C1470[$dataClassName]:=$object[$dataClassName]
Expand Down Expand Up @@ -1048,13 +1128,16 @@ Function setup()
$CLI.print($asset.path; "244").LF()
$CLI.print("size: "; "bold").print(String:C10($file.size); "39").LF()

$file.copyTo(Folder:C1567("/RESOURCES/"); fk overwrite:K87:5)

Else

$file:=This:C1470._getDataFolder().file($dataClassName+".data")
$file:=This:C1470._dataFolder.file($dataClassName+".data")

If ($file#Null:C1517) && ($file.exists)

$data:=$file.getContent()

BLOB TO VARIABLE:C533($data; $object)

This:C1470[$dataClassName]:=$object[$dataClassName]
Expand Down
Loading

0 comments on commit 54f64be

Please sign in to comment.