Files: 842b574ea8edb049a6403e9b4434d95823a39caa / index.js
2984 bytesRaw
1 | |
2 | |
3 | var Dat = require('dat-node') |
4 | var program = require('commander'); |
5 | var uri = require('urijs') |
6 | var pull = require('pull-stream') |
7 | |
8 | program |
9 | .option('-f, --folder [value]', 'Folder for sharing') |
10 | .option('-i, --only-people-i-follow', 'Only seed urls from people or channels I follow') |
11 | .parse(process.argv); |
12 | |
13 | function extractLinksAndSeed(err, logs) { |
14 | if (err) throw err; |
15 | |
16 | console.log("Found " + logs.length) |
17 | |
18 | logs.forEach(msg => { |
19 | uri.withinString(msg.value.content.text, (datLink) => { |
20 | if (!datLink.startsWith("dat://")) return |
21 | |
22 | console.log("Saving to:", program.folder + "/" + datLink.substring(6)) |
23 | |
24 | Dat(program.folder + "/" + datLink.substring(6), { |
25 | key: datLink |
26 | }, function (err, dat) { |
27 | if (err) throw err |
28 | |
29 | console.log("sharing:", datLink) |
30 | dat.joinNetwork() |
31 | }) |
32 | }) |
33 | }) |
34 | } |
35 | |
36 | function getAll(sbot) { |
37 | console.log("Looking for dat links in all feeds") |
38 | |
39 | pull( |
40 | sbot.createLogStream({ reverse: true, limit: 10000 }), |
41 | pull.filter((msg) => { |
42 | return !msg.value || |
43 | msg.value.content.type == 'post' && |
44 | typeof msg.value.content.text == "string" && |
45 | msg.value.content.text.indexOf("dat://") != -1 |
46 | }), |
47 | pull.collect(extractLinksAndSeed) |
48 | ) |
49 | } |
50 | |
51 | function messagesFromPeopleIFollow(sbot, following, channelSubscriptions) { |
52 | console.log("users:", following) |
53 | console.log("channels:", channelSubscriptions) |
54 | pull( |
55 | sbot.createLogStream({ reverse: true, limit: 10000 }), |
56 | pull.filter((msg) => { |
57 | return !msg.value || |
58 | ((msg.value.author in following || |
59 | msg.value.content.channel in channelSubscriptions) |
60 | && msg.value.content.type == 'post' && |
61 | typeof msg.value.content.text == "string" && |
62 | msg.value.content.text.indexOf("dat://") != -1) |
63 | }), |
64 | pull.collect(extractLinksAndSeed) |
65 | ) |
66 | } |
67 | |
68 | function getFromPeopleIFollow(sbot) { |
69 | var following = [] |
70 | var channelSubscriptions = [] |
71 | |
72 | console.log("Looking for dat links in people i follow") |
73 | |
74 | sbot.whoami((err, feed) => { |
75 | pull( |
76 | sbot.createUserStream({ id: feed.id }), |
77 | pull.filter((msg) => { |
78 | return !msg.value || |
79 | msg.value.content.type == 'contact' || |
80 | (msg.value.content.type == 'channel' && |
81 | typeof msg.value.content.subscribed != 'undefined') |
82 | }), |
83 | pull.collect(function (err, msgs) { |
84 | msgs.forEach((msg) => { |
85 | if (msg.value.content.type == 'contact') |
86 | { |
87 | if (msg.value.content.following) |
88 | following[msg.value.content.contact] = 1 |
89 | else |
90 | delete following[msg.value.content.contact] |
91 | } |
92 | else // channel subscription |
93 | { |
94 | if (msg.value.content.subscribed) |
95 | channelSubscriptions[msg.value.content.channel] = 1 |
96 | else |
97 | delete channelSubscriptions[msg.value.content.channel] |
98 | } |
99 | }) |
100 | |
101 | messagesFromPeopleIFollow(sbot, following, channelSubscriptions) |
102 | }) |
103 | ) |
104 | }) |
105 | } |
106 | |
107 | require('ssb-client')((err, sbot) => { |
108 | if (err) throw err; |
109 | |
110 | if (program.onlyPeopleIFollow) |
111 | getFromPeopleIFollow(sbot) |
112 | else |
113 | getAll(sbot) |
114 | }) |
115 |
Built with git-ssb-web