Skip to content

Instantly share code, notes, and snippets.

@kameit00
Last active October 26, 2018 08:22
Show Gist options
  • Save kameit00/8159115 to your computer and use it in GitHub Desktop.
Save kameit00/8159115 to your computer and use it in GitHub Desktop.
Renames all your Pinboard.in tags to lower case. My little groovy playground for today...
// Extend the String class with utility methods
String.metaClass.encodeURL = {
java.net.URLEncoder.encode(delegate as String, "UTF-8")
}
String.metaClass.isLowerCase = {
delegate.equals(delegate.toLowerCase())
}
def getTags(String uri) {
new XmlParser().parse(uri)
}
def renameTag(String uri, String oldTag, String newTag) {
println("Renaming: $oldTag (${oldTag.encodeURL()}) to $newTag (${newTag.encodeURL()})")
new URL(uri + "&old=${oldTag.encodeURL()}" + "&new=${newTag.encodeURL()}")
.getText()
}
// authToken = "username:some_numbers_and_characters"
// -> your token from pinboard website (https://pinboard.in/settings/password)
def authToken =
def authParameter = "auth_token=$authToken"
def allTagsUrl = "https://api.pinboard.in/v1/tags/get&$authParameter"
def renameTagUrl = "https://api.pinboard.in/v1/tags/rename&$authParameter"
def timeBetweenApiCalls = 3000
def allTagsFromApiCall = getTags("$allTagsUrl?$authParameter").tag.@tag
def upperCaseTags = allTagsFromApiCall.findAll {
!it.isLowerCase()
}
println(upperCaseTags)
// Rename tags to lower case
def i = 0;
def apiWaitTimeDoubled = 0
for (tag in upperCaseTags) {
i++
println("$i of ${upperCaseTags.size()}")
def result = renameTag(renameTagUrl, tag, tag.toLowerCase())
def message = new XmlSlurper().parseText(result).toString()
if (message.contains("429 Too Many Requests")) {
timeBetweenApiCalls = timeBetweenApiCalls.multiply(2)
apiWaitTimeDoubled++
}
println "API call result: " + message
// Only call API every 3 seconds (see also '429 Too Many Requests')
Thread.sleep(timeBetweenApiCalls)
}
println "Api Wait time has been doubled $apiWaitTimeDoubled times."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment