-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinboard-twitter-unshortener.js
executable file
·87 lines (78 loc) · 2.67 KB
/
pinboard-twitter-unshortener.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
// Unshortens/deshortens/lengthens urls in your pinboard account. Simply
// put API token or username/password info into ~/.pinboard.json. For
// example:
// { "token": "blah:123456789ABCEDF" }
// or:
// { "username": "blah", "password": "ABCDEF" }
// Adjust NUM_RECENT below to check more/less bookmarks.
var pinboard_config = require(process.env.HOME + "/.pinboard.json");
var http = require("http");
var pinboard = require("pinboard");
var async = require("async");
const NUM_RECENT = 50;
const SHORTENERS = ["t.co", "bit.ly", "goo.gl", "tinyurl.com", "is.gd", "ow.ly", "tcrn.ch"];
function isShortenedURL(url) {
for (var i = 0; i < SHORTENERS.length; i++) {
var re = new RegExp("^https?:\/\/" + SHORTENERS[i] + "\/");
if (url.search(re) != -1) {
return true;
}
}
return false;
}
function unShorten(url, callback) {
var hostname = url.match(/^https?:\/\/([^\/]+)/)[1];
var path = url.match(/^https?:\/\/[^\/]+(\/.*)$/)[1];
var options = {
hostname: hostname,
method: "HEAD",
path: path
};
var request = http.request(options, function(result) {
// Annoyingly, many shortened links point to other shortened links.
if (isShortenedURL(result.headers.location)) {
unShorten(result.headers.location, callback);
} else {
callback(result.headers.location);
}
});
request.end();
}
function testAndUpdatePosts(posts) {
async.eachSeries(posts, function (post, isDone) {
if (isShortenedURL(post.href)) {
unShorten(post.href, function(newUrl) {
// Add a new bookmark that is the same as the old bookmark.
var oldUrl = post.href;
delete post.href;
post.url = newUrl;
post.dt = post.time; // No idea why input != output
delete post.time;
pinboard.add(post, function () {
// Now delete the old bookmark.
pinboard.destroy(oldUrl, function (result) {
console.log(oldUrl + " --> " + newUrl);
isDone();
});
});
});
} else {
isDone();
}
}, function (error) {
if (error) {
console.log("Error: " + error);
}
});
}
pinboard.config({
token: pinboard_config.token,
username: pinboard_config.username,
password: pinboard_config.password
});
pinboard.get("posts/recent", { count: NUM_RECENT }, function (data) {
if (data && data.posts && data.posts.length > 0) {
testAndUpdatePosts(data.posts);
}
});