Advertisement
sashaatx

Untitled

Jul 22nd, 2023
1,697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;credit: https://github.com/TheArkive/JXON_ahk2
  2. ;credit: https://github.com/thqby/ahk2_lib
  3.  
  4. class Github
  5. {
  6.     repo_storage := []
  7.     /**
  8.      * with this url as an example:
  9.      * https://github.com/TheArkive/JXON_ahk2
  10.      * @param Github_Username:="TheArkive"
  11.      * @param Repository_Name:="JXON_ahk2"
  12.      * @param Download (path_to_save, url := "optional") using DownloadAsync.ahk
  13.      * @param this.getReleases() returns Map() of all releases, accessed @ this.repo_storage
  14.      * @param this.repo_storage for i in repo_storage returns => i.downloadURL: "", i.version: "", i.change_notes: "", i.date: "",  i.name: ""
  15.      * @param this.searchReleases ("keyword") search through all release names for keyword first, falling back to searching all urls. Returns URL to download for reuse in Download method
  16.      * @param this.details() notes or body for the release with changes.
  17.      * @param this.LatestReleaseMap for releaseName, releaseURL in this.LatestReleaseMap
  18.      * @param this.Version returns "v2.0.1" for example
  19.      */
  20.     __New(Username, Repository_Name) {
  21.         temp := A_ScriptDir "temp.json"
  22.         this.usernamePlusRepo := Trim(Username) "/" Trim(Repository_Name)
  23.         url := "https://api.github.com/repos/" this.usernamePlusRepo "/releases/latest"
  24.         this.source_zip := "https://github.com/" this.usernamePlusRepo "/archive/refs/heads/main.zip"
  25.         data := this.jsonDownload(url)
  26.         data := JXON_Load(&data;)
  27.         this.data := data
  28.         this.fileType := ""
  29.         ;filedelete, "1.json"
  30.         this.FirstAssetDL := data["assets"][1]["browser_download_url"]
  31.         this.AssetJ := data["assets"]
  32.         this.FirstAsset := data["assets"][1]["name"]
  33.         this.ReleaseVersion := data["html_url"]
  34.         this.Version := data["tag_name"]
  35.         this.body := data["body"]
  36.         this.repo_string := StrSplit(this.usernamePlusRepo, "/")[2]
  37.         this.LatestReleaseMap := Map()
  38.         this.AssetList := []
  39.         this.DownloadExtension := ""
  40.         this.olderReleases := Map()
  41.         this.assetProps()
  42.         ;this.Filetype := data["assets"][1]["browser_download_url"]
  43.     }
  44.     jsonDownload(URL) {
  45.         Http := WinHttpRequest()
  46.         Http.Open("GET", URL)
  47.         Http.Send()
  48.         Http.WaitForResponse()
  49.         storage := Http.ResponseText
  50.         return storage ;Set the "text" variable to the response
  51.     }
  52.     Source(Pathlocal){
  53.         this.Download(PathLocal, URL := this.source_zip)
  54.     }
  55.     Download(PathLocal, URL := this.FirstAssetDL) {
  56.         releaseExtension := this.downloadExtensionSplit(URL)
  57.         pathWithExtension := this.handleUserPath(PathLocal, releaseExtension)
  58.         Download(URL, pathWithExtension)
  59.     }
  60.     release() {
  61.         return this.FirstAssetDL
  62.     }
  63.     name() {
  64.         return this.FirstAssetName
  65.     }
  66.     details() {
  67.         return this.body
  68.     }
  69.     assetProps() {
  70.         for k, v in this.AssetJ {
  71.             this.LatestReleaseMap.Set(v["name"], v["browser_download_url"])
  72.         }
  73.         return this.LatestReleaseMap
  74.     }
  75.     emptyRepoMap() {
  76.         repo := {
  77.             downloadURL: "",
  78.             version: "",
  79.             change_notes: "",
  80.             date: "",
  81.             name: ""
  82.         }
  83.         return repo
  84.     }
  85.     getReleases() {
  86.         url := "https://api.github.com/repos/" this.usernamePlusRepo "/releases"
  87.         data := this.jsonDownload(url)
  88.         data := JXON_Load(&data;)
  89.         for igloo in data {
  90.             for alpha in igloo["assets"] {
  91.                 repo := this.emptyRepoMap()
  92.                 repo.version := igloo["tag_name"]
  93.                 repo.change_notes := igloo["body"]
  94.                 repo.name := alpha["name"]
  95.                 repo.date := alpha["created_at"]
  96.                 repo.downloadURL := alpha["browser_download_url"]
  97.                 this.repo_storage.Push(repo)
  98.             }
  99.         }
  100.         return this.repo_storage
  101.     }
  102.     downloadExtensionSplit(DL) {
  103.         Arrays := StrSplit(DL, ".")
  104.         this.filetype := Trim(Arrays[Arrays.Length])
  105.         return this.filetype
  106.     }
  107.     getVersion() {
  108.         url := StrSplit(this.ReleaseVersion, "/")
  109.         version := url[8]
  110.         return version
  111.         ; msgbox % this.j[1].assets.name
  112.         ; return this.j[1].assets.name
  113.     }
  114.     searchReleases(providedSearch) {
  115.         for assetName, DLURL in this.LatestReleaseMap {
  116.             if InStr(assetName, providedSearch) {
  117.                 return DLURL
  118.             }
  119.             else if InStr(DLURL, providedSearch) {
  120.                 return DLURL
  121.             }
  122.         }
  123.     }
  124.  
  125.     handleUserPath(PathLocal, releaseExtension) {
  126.         if InStr(PathLocal, "") {
  127.             pathParts := StrSplit(PathLocal, "")
  128.             FileName := pathParts[pathParts.Length]
  129.         }
  130.         else {
  131.             FileName := PathLocal
  132.             PathLocal := A_ScriptDir "" FileName
  133.             pathParts := StrSplit(PathLocal, "")
  134.         }
  135.         if InStr(FileName, ".") {
  136.             FileNameParts := StrSplit(FileName, ".")
  137.             UserExtension := FileNameParts[FileNameParts.Length]
  138.             if (releaseExtension != userExtension) {
  139.                 newName := ""
  140.                 for key, val in FileNameParts {
  141.                     if (A_Index == FileNameParts.Length) {
  142.                         break
  143.                     }
  144.                     newName .= val
  145.                 }
  146.                 newPath := ""
  147.                 for key, val in pathParts {
  148.                     if (A_Index == pathParts.Length) {
  149.                         break
  150.                     }
  151.                     newPath .= val
  152.                 }
  153.                 pathWithExtension := newPath newName "." releaseExtension
  154.             }
  155.             else {
  156.                 pathWithExtension := PathLocal
  157.             }
  158.         }
  159.         else {
  160.             pathWithExtension := PathLocal "." releaseExtension
  161.         }
  162.         return pathWithExtension
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement