git ssb

2+

arj / dat-share



Commit cf7e8c012199e2e0fe63032b824cdd36e6990015

Refactor into lib and make it a ssb plugin as well

Anders Rune Jensen committed on 9/22/2017, 6:52:52 PM
Parent: 842b574ea8edb049a6403e9b4434d95823a39caa

Files changed

README.mdchanged
index.jschanged
bin.jsadded
lib.jsadded
README.mdView
@@ -4,11 +4,13 @@
44
55 Usage (assuming you want to share from the dats folder):
66
77 ```
8-node index.js -f ./dats
8 +node bin.js -f ./dats
99 ```
1010
11 +Can also take a -i option to only share links from people you follow.
12 +
1113 Gives:
1214
1315 ```
1416 Looking for dat links
index.jsView
@@ -1,114 +1,18 @@
1-#!/usr/bin/env node
1 +var lib = require('./lib');
22
3-var Dat = require('dat-node')
4-var program = require('commander');
5-var uri = require('urijs')
6-var pull = require('pull-stream')
3 +exports.name = 'dat-share'
4 +exports.version = require('./package.json').version
5 +exports.manifest = {}
76
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);
7 +exports.init = function (ssb, config) {
8 + var conf = config.datShare || {}
9 + var onlyPeopleIFollow = conf.onlyPeopleIFollow || false
10 + var shareFolder = conf.folder || '/tmp'
1211
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- })
12 + console.log("Sharing dat links from: ", shareFolder)
13 +
14 + if (onlyPeopleIFollow)
15 + lib.getFromPeopleIFollow(ssb, shareFolder)
16 + else
17 + lib.getAll(ssb, shareFolder)
3418 }
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-})
bin.jsView
@@ -1,0 +1,23 @@
1 +#!/usr/bin/env node
2 +
3 +var lib = require('./lib');
4 +
5 +var program = require('commander');
6 +
7 +program
8 + .option('-f, --folder [value]', 'Folder for sharing')
9 + .option('-i, --only-people-i-follow', 'Only seed urls from people or channels I follow')
10 + .parse(process.argv);
11 +
12 +require('ssb-client')((err, sbot) => {
13 + if (err) throw err;
14 +
15 + var shareFolder = program.folder || '/tmp'
16 +
17 + console.log("Sharing dat links from: ", shareFolder)
18 +
19 + if (program.onlyPeopleIFollow)
20 + lib.getFromPeopleIFollow(sbot, shareFolder)
21 + else
22 + lib.getAll(sbot, shareFolder)
23 +})
lib.jsView
@@ -1,0 +1,105 @@
1 +var Dat = require('dat-node')
2 +var uri = require('urijs')
3 +var pull = require('pull-stream')
4 +
5 +var self = module.exports = {
6 + extractLinksAndSeed: function(err, logs, shareFolder) {
7 + if (err) throw err;
8 +
9 + console.log("Found " + logs.length)
10 +
11 + logs.forEach(msg => {
12 + uri.withinString(msg.value.content.text, (datLink) => {
13 + if (!datLink.startsWith("dat://")) return
14 +
15 + console.log("Saving to:", shareFolder + "/" + datLink.substring(6))
16 +
17 + Dat(shareFolder + "/" + datLink.substring(6), {
18 + key: datLink
19 + }, function (err, dat) {
20 + if (err) throw err
21 +
22 + console.log("sharing:", datLink)
23 + dat.joinNetwork()
24 + })
25 + })
26 + })
27 + },
28 +
29 + getAll: function(sbot, shareFolder) {
30 + console.log("Looking for dat links in all feeds")
31 +
32 + pull(
33 + sbot.createLogStream({ reverse: true, limit: 10000 }),
34 + pull.filter((msg) => {
35 + return !msg.value ||
36 + msg.value.content.type == 'post' &&
37 + typeof msg.value.content.text == "string" &&
38 + msg.value.content.text.indexOf("dat://") != -1
39 + }),
40 + pull.collect((err, logs) => self.extractLinksAndSeed(err, logs,
41 + shareFolder))
42 + )
43 + },
44 +
45 + messagesFromPeopleIFollow: function(sbot, following,
46 + channelSubscriptions,
47 + shareFolder) {
48 + console.log("users:", following)
49 + console.log("channels:", channelSubscriptions)
50 + pull(
51 + sbot.createLogStream({ reverse: true, limit: 10000 }),
52 + pull.filter((msg) => {
53 + return !msg.value ||
54 + ((msg.value.author in following ||
55 + msg.value.content.channel in channelSubscriptions)
56 + && msg.value.content.type == 'post' &&
57 + typeof msg.value.content.text == "string" &&
58 + msg.value.content.text.indexOf("dat://") != -1)
59 + }),
60 + pull.collect((err, logs) => self.extractLinksAndSeed(err, logs,
61 + shareFolder))
62 + )
63 + },
64 +
65 + getFromPeopleIFollow: function(sbot, shareFolder) {
66 + var following = []
67 + var channelSubscriptions = []
68 +
69 + console.log("Looking for dat links in people i follow")
70 +
71 + sbot.whoami((err, feed) => {
72 + pull(
73 + sbot.createUserStream({ id: feed.id }),
74 + pull.filter((msg) => {
75 + return !msg.value ||
76 + msg.value.content.type == 'contact' ||
77 + (msg.value.content.type == 'channel' &&
78 + typeof msg.value.content.subscribed != 'undefined')
79 + }),
80 + pull.collect(function (err, msgs) {
81 + msgs.forEach((msg) => {
82 + if (msg.value.content.type == 'contact')
83 + {
84 + if (msg.value.content.following)
85 + following[msg.value.content.contact] = 1
86 + else
87 + delete following[msg.value.content.contact]
88 + }
89 + else // channel subscription
90 + {
91 + if (msg.value.content.subscribed)
92 + channelSubscriptions[msg.value.content.channel] = 1
93 + else
94 + delete channelSubscriptions[msg.value.content.channel]
95 + }
96 + })
97 +
98 + self.messagesFromPeopleIFollow(sbot, following,
99 + channelSubscriptions,
100 + shareFolder)
101 + })
102 + )
103 + })
104 + }
105 +}

Built with git-ssb-web