Advertisement
sashaatx

Untitled

Jul 22nd, 2023
1,787
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. /*
  5.     @source https://github.com/samfisherirl/Github.ahk-API-for-AHKv2
  6.     @method Github.latest(Username,Repository_Name)
  7.  
  8.     return {
  9.         downloadURLs: [
  10.             "http://github.com/release.zip",
  11.             "http://github.com/release.rar"
  12.                     ],
  13.         version: "",
  14.         change_notes: "",
  15.         date: "",
  16.         }
  17.        
  18.     @method Github.historicReleases(Username,Repository_Name)
  19.         array of objects => [{
  20.             downloadURL: "",
  21.             version: "",
  22.             change_notes: "",
  23.             date: ""
  24.         }]
  25.     @func this.Download(url,path)
  26.         improves on download():
  27.         - if user provides wrong extension, function will apply proper extension
  28.         - allows user to provide directory
  29.         example:    Providing A_ScriptDir to Download will throw error
  30.                     Providing A_ScriptDir to Github.Download() will supply Download() with release name
  31. */
  32. class Github
  33. {
  34.     static source_zip := ""
  35.     static url := false
  36.     static usernamePlusRepo := false
  37.     static storage := {
  38.         repo: "",
  39.         source_zip: ""
  40.     }
  41.     static data := false
  42.  
  43.     static build(Username, Repository_Name) {
  44.         Github.usernamePlusRepo := Trim(Username) "/" Trim(Repository_Name)
  45.         Github.source_zip := "https://github.com/" Github.usernamePlusRepo "/archive/refs/heads/main.zip"
  46.         return "https://api.github.com/repos/" Github.usernamePlusRepo "/releases"
  47.         ;filedelete, "1.json"
  48.         ;this.Filetype := data["assets"][1]["browser_download_url"]
  49.     }
  50.     /*
  51.     return {
  52.         downloadURLs: [
  53.             "http://github.com/release.zip",
  54.             "http://github.com/release.rar"
  55.                     ],
  56.         version: "",
  57.         change_notes: "",
  58.         date: "",
  59.         }
  60.     */
  61.     static latest(Username, Repository_Name) {
  62.         url := Github.build(Username, Repository_Name)
  63.         data := Github.processRepo(url)
  64.         return Github.latestProp(data)
  65.     }
  66.     /*
  67.     static processRepo(url) {
  68.         Github.source_zip := "https://github.com/" Github.usernamePlusRepo "/archive/refs/heads/main.zip"
  69.         Github.data := Github.jsonDownload(url)
  70.         data := Github.data
  71.         return Jsons.Loads(&data)
  72.     }
  73.     */
  74.     static processRepo(url) {
  75.         Github.source_zip := "https://github.com/" Github.usernamePlusRepo "/archive/refs/heads/main.zip"
  76.         data := Github.jsonDownload(url)
  77.         return Jsons.Loads(&data)
  78.     }
  79.     /*
  80.     @example
  81.     repoArray := Github.historicReleases()
  82.         repoArray[1].downloadURL => string | link
  83.         repoArray[1].version => string | version data
  84.         repoArray[1].change_notes => string | change notes
  85.         repoArray[1].date => string | date of release
  86.    
  87.     @returns (array of release objects) => [{
  88.         downloadURL: "",
  89.         version: "",
  90.         change_notes: "",
  91.         date: ""
  92.         }]
  93.     */
  94.     static historicReleases(Username, Repository_Name) {
  95.         url := Github.build(Username, Repository_Name)
  96.         data := Github.processRepo(url)
  97.         repo_storage := []
  98.         url := "https://api.github.com/repos/" Github.usernamePlusRepo "/releases"
  99.         data := Github.jsonDownload(url)
  100.         data := Jsons.Loads(&data)
  101.         for release in data {
  102.             for asset in release["assets"] {
  103.                 repo_storage.Push(Github.repoDistribution(release, asset))
  104.             }
  105.         }
  106.         return repo_storage
  107.     }
  108.     static latestProp(data) {
  109.         for i in data {
  110.             baseData := i
  111.             assetMap := i["assets"]
  112.             date := i["created_at"]
  113.             if i["assets"].Length > 0 {
  114.                 length := i["assets"].Length
  115.                 releaseArray := Github.distributeReleaseArray(length, assetMap)
  116.                 break
  117.             }
  118.             else {
  119.                 releaseArray := ["https://github.com/" Github.usernamePlusRepo "/archive/" i["tag_name"] ".zip"]
  120.                 ;source url = f"https://github.com/{repo_owner}/{repo_name}/archive/{release_tag}.zip"
  121.                 break
  122.             }
  123.         }
  124.         ;move release array to first if
  125.         ;then add source
  126.         return {
  127.             downloadURLs: releaseArray,
  128.             version: baseData["tag_name"],
  129.             change_notes: baseData["body"],
  130.             date: date
  131.         }
  132.     }
  133.     /*
  134.     loop releaseURLCount {
  135.         assetArray.Push(JsonData[A_Index]["browser_download_url"])
  136.     }
  137.     return => assetArray[]
  138.     */
  139.     /*
  140.     loop releaseURLCount {
  141.         assetMap.Set(jsonData[A_Index]["name"], jsonData[A_Index]["browser_download_url"])
  142.     }
  143.     return => assetMap()
  144.     */
  145.     static jsonDownload(URL) {
  146.         Http := WinHttpRequest()
  147.         Http.Open("GET", URL)
  148.         Http.Send()
  149.         Http.WaitForResponse()
  150.         storage := Http.ResponseText
  151.         return storage ;Set the "text" variable to the response
  152.     }
  153.     static distributeReleaseArray(releaseURLCount, Jdata) {
  154.         assetArray := []
  155.         if releaseURLCount {
  156.             if (releaseURLCount > 1) {
  157.                 loop releaseURLCount {
  158.                     assetArray.Push(Jdata[A_Index]["browser_download_url"])
  159.                 }
  160.             }
  161.             else {
  162.                 assetArray.Push(Jdata[1]["browser_download_url"])
  163.             }
  164.             return assetArray
  165.         }
  166.     }
  167.     /*
  168.     download the latest main.zip source zip
  169.     */
  170.     static Source(Username, Repository_Name, Pathlocal := A_ScriptDir) {
  171.         url := Github.build(Username, Repository_Name)
  172.         data := Github.processRepo(url)
  173.  
  174.         Github.Download(URL := Github.source_zip, PathLocal)
  175.     }
  176.     /*
  177.     benefit over download() => handles users path, and applies appropriate extension.
  178.     IE: If user provides (Path:=A_ScriptDir "\download.zip") but extension is .7z, extension is modified for the user.
  179.     If user provides directory, name for file is applied from the path (download() will not).
  180.     Download (
  181.         @param URL to download
  182.         @param Path where to save locally
  183.     )
  184.     */
  185.     static Download(URL, PathLocal := A_ScriptDir) {
  186.         releaseExtension := Github.downloadExtensionSplit(URL)
  187.         pathWithExtension := Github.handleUserPath(PathLocal, releaseExtension)
  188.         try {
  189.             Download(URL, pathWithExtension)
  190.         } catch as e {
  191.             MsgBox(e.Message . "`nURL:`n" URL)
  192.         }
  193.     }
  194.     static emptyRepoMap() {
  195.         repo := {
  196.             downloadURL: "",
  197.             version: "",
  198.             change_notes: "",
  199.             date: "",
  200.             name: ""
  201.         }
  202.         return repo
  203.     }
  204.  
  205.     static repoDistribution(release, asset) {
  206.         return {
  207.             downloadURL: asset["browser_download_url"],
  208.             version: release["tag_name"],
  209.             change_notes: release["body"],
  210.             date: asset["created_at"],
  211.             name: asset["name"]
  212.         }
  213.     }
  214.     static downloadExtensionSplit(DL) {
  215.         Arrays := StrSplit(DL, ".")
  216.         filetype := Trim(Arrays[Arrays.Length])
  217.         return filetype
  218.     }
  219.  
  220.     static handleUserPath(PathLocal, releaseExtension) {
  221.         if InStr(PathLocal, "\") {
  222.             pathParts := StrSplit(PathLocal, "\")
  223.             FileName := pathParts[pathParts.Length]
  224.         }
  225.         else {
  226.             FileName := PathLocal
  227.             PathLocal := A_ScriptDir "\" FileName
  228.             pathParts := StrSplit(PathLocal, "\")
  229.         }
  230.         if InStr(FileName, ".") {
  231.             FileNameParts := StrSplit(FileName, ".")
  232.             UserExtension := FileNameParts[FileNameParts.Length]
  233.             if (releaseExtension != userExtension) {
  234.                 newName := ""
  235.                 for key, val in FileNameParts {
  236.                     if (A_Index == FileNameParts.Length) {
  237.                         break
  238.                     }
  239.                     newName .= val
  240.                 }
  241.                 newPath := ""
  242.                 for key, val in pathParts {
  243.                     if (A_Index == pathParts.Length) {
  244.                         break
  245.                     }
  246.                     newPath .= val
  247.                 }
  248.                 pathWithExtension := newPath newName "." releaseExtension
  249.             }
  250.             else {
  251.                 pathWithExtension := PathLocal
  252.             }
  253.         }
  254.         else {
  255.             pathWithExtension := PathLocal "." releaseExtension
  256.         }
  257.         return pathWithExtension
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement