|
// 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." |