forked from com-lihaoyi/mill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.sc
52 lines (41 loc) · 1.29 KB
/
upload.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env amm
import ammonite.ops._
import scalaj.http._
@main
def shorten(longUrl: String) = {
println("shorten longUrl " + longUrl)
val shortUrl = Http("https://git.io")
.postForm(Seq("url" -> longUrl))
.asString
.headers("Location")
.head
println("shorten shortUrl " + shortUrl)
shortUrl
}
@main
def apply(uploadedFile: Path,
tagName: String,
uploadName: String,
authKey: String): String = {
val body = Http("https://api.github.com/repos/lihaoyi/mill/releases/tags/" + tagName)
.header("Authorization", "token " + authKey)
.asString.body
val parsed = upickle.json.read(body)
println(body)
val snapshotReleaseId = parsed("id").num.toInt
val uploadUrl =
s"https://uploads.github.com/repos/lihaoyi/mill/releases/" +
s"$snapshotReleaseId/assets?name=$uploadName"
val res = Http(uploadUrl)
.header("Content-Type", "application/octet-stream")
.header("Authorization", "token " + authKey)
.timeout(connTimeoutMs = 5000, readTimeoutMs = 60000)
.postData(read.bytes! uploadedFile)
.asString
println(res.body)
val longUrl = upickle.json.read(res.body)("browser_download_url").str.toString
println("Long Url " + longUrl)
val shortUrl = shorten(longUrl)
println("Short Url " + shortUrl)
shortUrl
}