doing-plugin-twitter-import 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b9df02efee1e4b0d68c8fca88eafbeb138673d5028b32e0699ecd7721b02d060
4
+ data.tar.gz: a868f4d998d03c6631b0456c7dd111f251eb2d89a9b2e5404325a6179b5b7f36
5
+ SHA512:
6
+ metadata.gz: 70a5b493b836db48bbd477212587880865096ba30168c828e959bca8d188a8487a453c6b3d5d66a904bb4b4c82820e1a8a1118f0e363046b1c1d6c1284881feb
7
+ data.tar.gz: 509cc0119a2099e9cd2f8b0f3a1afa94b608b59f4c8ef3889f6a1b5001082a2926a41122443f2f43e3ae6a3c5542b90f62966ed364cfa0e9b64cad8cd4bc0341
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ # title: Twitter Import
4
+ # description: Import entries from a Twitter timeline
5
+ # author: Brett Terpstra
6
+ # url: https://brettterpstra.com
7
+ module Doing
8
+ # Capture Thing import plugin
9
+ class TwitterImport
10
+ require 'time'
11
+ require 'twitter'
12
+
13
+ include Doing::Util
14
+ include Doing::Errors
15
+
16
+ def self.settings
17
+ {
18
+ trigger: '^tw(?:itter)?$',
19
+ config: { 'api_key' => 'xxxxx', 'api_secret' => 'xxxxx', 'user' => 'xxxxx' }
20
+ }
21
+ end
22
+
23
+ ##
24
+ ## Imports a Capture Thing folder
25
+ ##
26
+ ## @param wwid [WWID] WWID object
27
+ ## @param path [String] Path to Capture Thing folder
28
+ ## @param options [Hash] Additional Options
29
+ ##
30
+ def self.import(wwid, path, options: {})
31
+ key = Doing.setting('plugins.twitter.api_key')
32
+ secret = Doing.setting('plugins.twitter.api_secret')
33
+
34
+ raise PluginException, 'Please add API key/secret to config' if key =~ /^xxxxx/
35
+
36
+ user = Doing.setting('plugins.twitter.user')
37
+
38
+ @client = Twitter::REST::Client.new do |config|
39
+ config.consumer_key = key
40
+ config.consumer_secret = secret
41
+ end
42
+
43
+ options[:no_overlap] = true
44
+ options[:autotag] ||= wwid.auto_tag
45
+
46
+ tags = options[:tag] ? options[:tag].split(/[ ,]+/).map { |t| t.sub(/^@?/, '') } : []
47
+ options[:tag] = nil
48
+ prefix = options[:prefix] || ''
49
+
50
+ @old_items = wwid.content
51
+
52
+ new_items = load_timeline(user)
53
+
54
+ total = new_items.count
55
+
56
+ options[:count] = 0
57
+
58
+ new_items = wwid.filter_items(new_items, opt: options)
59
+
60
+ skipped = total - new_items.count
61
+ Doing.logger.debug('Skipped:' , %(#{skipped} items that didn't match filter criteria)) if skipped.positive?
62
+
63
+ imported = []
64
+
65
+ new_items.each do |item|
66
+ next if duplicate?(item)
67
+
68
+ title = "#{prefix} #{item.title} @done"
69
+ tags.each do |tag|
70
+ if title =~ /\b#{tag}\b/i
71
+ title.sub!(/\b#{tag}\b/i, "@#{tag}")
72
+ else
73
+ title += " @#{tag}"
74
+ end
75
+ end
76
+ title = wwid.autotag(title) if options[:autotag]
77
+ title.gsub!(/ +/, ' ')
78
+ title.strip!
79
+ section = options[:section] || item.section
80
+ section ||= wwid.config['current_section']
81
+
82
+ new_item = Item.new(item.date, title, section)
83
+ new_item.note = item.note
84
+
85
+ imported.push(new_item)
86
+ end
87
+
88
+ dups = new_items.count - imported.count
89
+ Doing.logger.info('Skipped:', %(#{dups} duplicate items)) if dups.positive?
90
+
91
+ imported = wwid.dedup(imported, no_overlap: !options[:overlap])
92
+ overlaps = new_items.count - imported.count - dups
93
+ Doing.logger.debug('Skipped:', "#{overlaps} items with overlapping times") if overlaps.positive?
94
+
95
+ imported.each do |item|
96
+ wwid.content.add_section(item.section)
97
+ wwid.content.push(item)
98
+ end
99
+
100
+ Doing.logger.info('Imported:', "#{imported.count} items")
101
+ end
102
+
103
+ def self.duplicate?(item)
104
+ @old_items.each do |oi|
105
+ return true if item.equal?(oi)
106
+ end
107
+
108
+ false
109
+ end
110
+
111
+ def self.load_timeline(user)
112
+ tweets = @client.user_timeline(user, {
113
+ count: 200,
114
+ include_rts: true,
115
+ exclude_replies: true
116
+ }).map { |t| { date: t[:created_at], title: t[:text], id: t[:id] } }
117
+
118
+ items = []
119
+
120
+ tweets.each do |tweet|
121
+ date = tweet[:date]
122
+ text = tweet[:title].dup
123
+ text = text.force_encoding('utf-8') if text.respond_to? :force_encoding
124
+ input = text.split("\n")
125
+ title = input[0]
126
+ note = Note.new(input.slice(1, input.count))
127
+
128
+ new_entry = Item.new(date, title, nil, note)
129
+ items << new_entry if new_entry
130
+ end
131
+
132
+ items
133
+ end
134
+
135
+ Doing::Plugins.register 'twitter', :import, self
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doing-plugin-twitter-import
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brett Terpstra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: twitter
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description: Imports entries from the Twitter timeline to Doing
28
+ email: me@brettterpstra.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/doing-plugin-twitter-import.rb
34
+ homepage: https://brettterpstra.com
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.2.16
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Twitter timeline import for Doing
57
+ test_files: []