Commit 842b574ea8edb049a6403e9b4434d95823a39caa
Add option to only seed for people/channels I follow
Anders Rune Jensen committed on 9/19/2017, 1:18:08 PMParent: 1b9093405deadf26da6cab9aefb5cdf7ec500421
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -6,43 +6,109 @@ | ||
6 | 6 | var pull = require('pull-stream') |
7 | 7 | |
8 | 8 | program |
9 | 9 | .option('-f, --folder [value]', 'Folder for sharing') |
10 | + .option('-i, --only-people-i-follow', 'Only seed urls from people or channels I follow') | |
10 | 11 | .parse(process.argv); |
11 | 12 | |
12 | -require('ssb-client')((err, sbot) => { | |
13 | +function extractLinksAndSeed(err, logs) { | |
13 | 14 | if (err) throw err; |
14 | 15 | |
15 | - console.log("Looking for dat links") | |
16 | + console.log("Found " + logs.length) | |
16 | 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 | + | |
17 | 39 | pull( |
18 | 40 | sbot.createLogStream({ reverse: true, limit: 10000 }), |
19 | 41 | pull.filter((msg) => { |
20 | 42 | return !msg.value || |
21 | 43 | msg.value.content.type == 'post' && |
22 | 44 | typeof msg.value.content.text == "string" && |
23 | 45 | msg.value.content.text.indexOf("dat://") != -1 |
24 | 46 | }), |
25 | - pull.collect((err, log) => { | |
26 | - if (err) throw err; | |
47 | + pull.collect(extractLinksAndSeed) | |
48 | + ) | |
49 | +} | |
27 | 50 | |
28 | - console.log("Found " + log.length) | |
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 | +} | |
29 | 67 | |
30 | - log.forEach(msg => { | |
31 | - uri.withinString(msg.value.content.text, (datLink) => { | |
32 | - if (!datLink.startsWith("dat://")) return | |
68 | +function getFromPeopleIFollow(sbot) { | |
69 | + var following = [] | |
70 | + var channelSubscriptions = [] | |
33 | 71 | |
34 | - console.log("Saving to:", program.folder + "/" + datLink.substring(6)) | |
72 | + console.log("Looking for dat links in people i follow") | |
35 | 73 | |
36 | - Dat(program.folder + "/" + datLink.substring(6), { | |
37 | - key: datLink | |
38 | - }, function (err, dat) { | |
39 | - if (err) throw err | |
40 | - | |
41 | - console.log("sharing:", datLink) | |
42 | - dat.joinNetwork() | |
43 | - }) | |
44 | - }) | |
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) | |
45 | 102 | }) |
46 | - }) | |
47 | - ) | |
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) | |
48 | 114 | }) |
Built with git-ssb-web