elephrame 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99301911c71cb6fe44225ba4ae6071886359ae6c667471f8a9198daf4a202ef8
4
- data.tar.gz: 98944310ca826e8d5432df0d699cb7eb7ed47c8997b19ef75331954e40b20e47
3
+ metadata.gz: 1c1ed54678e256a816845886696ca58289d035a7351deb2ec8180915a36f2471
4
+ data.tar.gz: 15cf11fd775bf2ec0716aa524af6a35ff7996ad724b311cccac89a087039eb5f
5
5
  SHA512:
6
- metadata.gz: da91f48e6e1502a97fec68ada9d66e0b89c33e231fc884318549af623217468e0b0e393b466496ba19520435f6bcb8ba5bdbd20e111c3a2009c07b0c8606898b
7
- data.tar.gz: fffdcd359ea544c6b7ff4bbe91b00c93f5629ffa51144df1ae10256499b7885d6a486e93e3e5bfbdcb26480a18fc6c5993ba99073f2f4e4e6ffe930e61d3a721
6
+ metadata.gz: 3664bc68bad91273c4b99084729f01f21e2a7ab77f1e98397eb4f32b7c3906522c0105b049954d64bce0e4b5d1b83af7be548bf3ad1659f597e31dfe496c418e
7
+ data.tar.gz: 57063548c113a5819ad8c73a94f07082fd2071ac7acc3ffaa70b09eaf0cd9a72caf163911f03ec9cdf6358a9e478fc5b54c5d8d0e384ad055006525bc024859d
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elephrame (0.4.5)
4
+ elephrame (0.4.6)
5
5
  moostodon (~> 0.4.0)
6
6
  rufus-scheduler
7
+ tracery (~> 0.7)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
@@ -34,12 +35,13 @@ GEM
34
35
  buftok (~> 0)
35
36
  http (~> 3.0)
36
37
  oj (~> 3.3)
37
- oj (3.7.9)
38
+ oj (3.7.10)
38
39
  public_suffix (3.0.3)
39
40
  raabro (1.1.6)
40
41
  rake (10.5.0)
41
42
  rufus-scheduler (3.5.2)
42
43
  fugit (~> 1.1, >= 1.1.5)
44
+ tracery (0.7.7)
43
45
  tzinfo (2.0.0)
44
46
  concurrent-ruby (~> 1.0)
45
47
  unf (0.1.4)
data/README.md CHANGED
@@ -45,13 +45,15 @@ Check the [examples](https://github.com/theZacAttacks/elephrame/tree/master/exam
45
45
 
46
46
  ### Bot Types
47
47
 
48
- So far the framework support 5 bot types: Periodic, Interact, PeroidInteract, Reply, Command
48
+ So far the framework support 6 bot types: Periodic, Interact, PeroidInteract, Reply, Command, Watcher
49
49
 
50
50
  - `Periodic` supports posting on a set schedule
51
51
  - `Interact` supports callbacks for each type of interaction (favorites, boosts, replies, follows)
52
52
  - `PeriodInteract` supports both of the above (I know, this isn't a good name)
53
53
  - `Reply` only supports replying to mentions
54
54
  - `Command` supports running code when mentioned with commands it recognizes
55
+ - `Watcher` supports watching streaming timelines (including lists, hashtags, public timeline, local timeline, and user timeline)
56
+ - `TraceryBot` supports automatically loading tracery rules. Overloads post method to automatically expand any text passed through it. This bot type provides certain options that ease the use of tracery grammar rules, please see examples and/or documentation for more explanation.
55
57
 
56
58
  The string passed to `Periodic` and `PeroidInteract` must be either a 'Duration' string or a 'Cron' string, as parsable by [fugit](https://github.com/floraison/fugit)
57
59
 
@@ -74,6 +76,7 @@ Exposed methods from bot object:
74
76
  - `find_ancestor(id, depth = 10, stop_at = 1)` looks backwards through reply chains for the most recent post the bot made starting at post `id` until it hits `depth` number of posts, or finds `stop_at` number of it's own posts
75
77
  - `no_bot?(account_id)` returns true if user with `account_id` has some form of "#NoBot" in their bio or their profile fields
76
78
  - `fetch_list_id(name)` return the id of a list with given name `name`
79
+ - `fetch_account_id(account_name)` return the id of an account with given handle `account_name`
77
80
 
78
81
  (See RubyDocs for source code documentation)
79
82
 
@@ -42,4 +42,5 @@ Gem::Specification.new do |spec|
42
42
 
43
43
  spec.add_dependency 'rufus-scheduler'
44
44
  spec.add_dependency 'moostodon', '~> 0.4.0'
45
+ spec.add_dependency 'tracery', '~> 0.7'
45
46
  end
@@ -0,0 +1,38 @@
1
+ require 'elephrame'
2
+ require 'time'
3
+
4
+ bot = Elephrame::Bots::TraceryBot.new('1h', 'tracery_files2')
5
+
6
+ # this overrides the default behavior for responding to mentions
7
+ bot.on_reply do |bot, post|
8
+ bot.reply_with_mentions("#greeting#, the current hour is #{Time.now}",
9
+ rules: 'reply')
10
+ end
11
+
12
+ bot.run do |bot|
13
+ # get the current hour
14
+ hour = Time.now.hour
15
+
16
+ case hour
17
+
18
+ when (20..23)
19
+ when (0..5)
20
+ bot.post("The moon is #phase#! It's so spooky :O",
21
+ rules: 'moon')
22
+
23
+ when (6..9)
24
+ bot.post("Gotta get up and #activity#!",
25
+ rules: 'morning')
26
+
27
+ when (10..16)
28
+ bot.post("Can't wait to #activity# when I get home",
29
+ rules: 'afternoon')
30
+
31
+ when (17..19)
32
+ bot.post("Time to start getting ready for #activity#",
33
+ rules: 'night')
34
+
35
+ else
36
+ next
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ {
2
+ "origin": "#greeting#, World! I'm #sexuality#!",
3
+ "greeting": [ "Hello",
4
+ "Greetings",
5
+ "Salutations" ],
6
+ "sexuality": [ "gay",
7
+ "pan",
8
+ "bi",
9
+ "ace",
10
+ "aro"]
11
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "default": "#greeting#",
3
+ "greeting": ["Salutations", "Hello", "How's it going", "Yo"]
4
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "activity": [ "put on PJs", "REET lOUDLY", "pet my cat", "sleeep" ]
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "phase": [ "waning", "waxing", "new", "full", "half", "gibbous" ]
3
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "activity": [ "brush my teeth", "eat breakfast", "get dressed",
3
+ "blow up balloons" ]
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "activity": ["bed", "sleeeeeeeeeeeEEEEEP", "curling up",
3
+ "building a blanket fort", "playing some videO jaMES"]
4
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "greeting": ["Salutations", "Hello", "How's it going", "Yo"]
3
+ }
@@ -0,0 +1,18 @@
1
+ require 'elephrame'
2
+
3
+ bot = Elephrame::Bots::TraceryBot.new('10s', 'tracery_files')
4
+
5
+ # this code happens automatically in the framework
6
+ # when there's a rule file for 'reply'
7
+ #bot.on_reply do |bot|
8
+ # bot.reply_with_mentions("#default#", rules: 'reply')
9
+ #end
10
+
11
+
12
+ # because there's a tracery file named "default" the framework loads
13
+ # it automatically! TraceryBot overloads the default 'post' method
14
+ # and makes sure it automatically expands our tracery text using our
15
+ # loaded grammar
16
+ bot.run do |bot|
17
+ bot.post('#greeting#, World! I\'m #sexuality#', visibility: 'unlisted')
18
+ end
@@ -115,6 +115,37 @@ module Elephrame
115
115
  lists[name]
116
116
  end
117
117
 
118
+ ##
119
+ # Gets the ID of an account given the user's handle or username
120
+ #
121
+ # @param account_name [String] either the user's full handle or
122
+ # their username. E.g., zac@computerfox.xyz, zac
123
+ # @return [String] ID of account
124
+
125
+ def fetch_account_id(account_name)
126
+ name = account_name.reverse.chomp('@').reverse
127
+ search = @client.search("@#{name}")
128
+
129
+ accounts = {}
130
+ search.accounts.each do |acct|
131
+ accounts[acct.acct] = acct.id
132
+ accounts[acct.username] = acct.id
133
+ end
134
+
135
+ accounts[name]
136
+ end
137
+
138
+ ##
139
+ # A helper method that is a wrapper around alias_method
140
+ # (just to make some code easier to read)
141
+ #
142
+ # @param method [Symbol] symbol with the name of a method
143
+ # @param new_name [Symbol] symbol with the new name for the method
144
+
145
+ def self.backup_method(method, new_name)
146
+ alias_method new_name, method
147
+ end
148
+
118
149
  private
119
150
 
120
151
  ##
@@ -137,7 +168,6 @@ module Elephrame
137
168
  end
138
169
  return true
139
170
  end
140
-
141
171
  end
142
172
  end
143
173
  end
@@ -1,5 +1,6 @@
1
1
  require_relative '../rest/rest'
2
2
  require_relative '../streaming/streaming'
3
+ require_relative './tracery'
3
4
  require_relative '../bot'
4
5
 
5
6
  module Elephrame
@@ -41,5 +42,53 @@ module Elephrame
41
42
  run_interact
42
43
  end
43
44
  end
45
+
46
+ ##
47
+ # a bot that posts things on an interval but can also respond
48
+ # to interactions, providing a grammer object and a few helper
49
+ # methods to easily create bots with tracery.
50
+ #
51
+ # set on_reply before running, otherwise the bot
52
+ # won't react to mentions
53
+
54
+ class TraceryBot < BaseBot
55
+ backup_method :post, :actually_post
56
+
57
+ include Elephrame::Streaming
58
+ include Elephrame::Scheduler
59
+ include Elephrame::Reply
60
+ include Elephrame::Trace
61
+
62
+ ##
63
+ # create a new TraceryBot
64
+ # @param interval [String] a string representing the interval to post
65
+ # @param tracery_dir [String] a string with the path to the directory
66
+ # containing all of the tracery grammer rules.
67
+ # @return [Elephrame::Bots::TraceryBot]
68
+
69
+ def initialize interval, tracery_dir
70
+ super()
71
+
72
+ # set up our bot stuff
73
+ setup_scheduler interval
74
+ setup_streaming
75
+ setup_tracery tracery_dir
76
+ end
77
+
78
+ ##
79
+ # Runs the bot. requires a block for periodic post logic, but relies on
80
+ # on_* functions for interaction logic. See Elephrame::AllInteractions
81
+ # for more details.
82
+
83
+ def run
84
+ run_scheduled &Proc.new
85
+
86
+ unless @on_reply.nil?
87
+ run_reply
88
+ else
89
+ @scheduler.join
90
+ end
91
+ end
92
+ end
44
93
  end
45
94
  end
@@ -0,0 +1,65 @@
1
+ require 'tracery'
2
+ require 'json'
3
+
4
+ module Elephrame
5
+ module Trace
6
+ include Tracery
7
+
8
+ # grammar is a hash { FILENAME => TRACERY RULES }
9
+ attr_accessor :grammar
10
+
11
+ ##
12
+ # loads all of our tracery files into our +files+ hash
13
+ # if a file is named 'default' then we load that into +grammar+
14
+ #
15
+ # @param dir [String] path to the directory containing the tracery rules
16
+
17
+ def setup_tracery dir_path
18
+ raise "Provided path not a directory" unless Dir.exist?(dir_path)
19
+
20
+ @grammar = {}
21
+ Dir.open(dir_path) do |dir|
22
+ dir.each do |file|
23
+ # skip our current and parent dir
24
+ next if file =~ /^\.\.?$/
25
+
26
+ # read the rule file into the files hash
27
+ @grammar[file.split('.').first] =
28
+ createGrammar(JSON.parse(File.read("#{dir_path}/#{file}")))
29
+ end
30
+ end
31
+
32
+ # go ahead and makes a default mention-handler
33
+ # if we have a reply rule file
34
+ unless @grammar['reply'].nil?
35
+ on_reply { |bot|
36
+ bot.reply_with_mentions('#default#', rules: 'reply')
37
+ }
38
+ end
39
+ end
40
+
41
+
42
+ ##
43
+ # a shortcut fuction for expanding text with tracery before posting
44
+ #
45
+ # @param text [String] the tracery text to expand before posting
46
+ # @param options [Hash] a hash of arguments to pass to post
47
+ # @option options rules [String] the grammar rules to load
48
+ # @option options visibility [String] visibility level
49
+ # @option options spoiler [String] text to use as content warning
50
+ # @option options reply_id [String] id of post to reply to
51
+ # @option options hide_media [Bool] should we hide media?
52
+ # @option options media [Array<String>] array of file paths
53
+
54
+ def expand_and_post(text, *options)
55
+ opts = Hash[*options]
56
+ rules = opts.fetch(:rules, 'default')
57
+ actually_post(@grammar[rules].flatten(text),
58
+ **opts.reject {|k|
59
+ k == :rules
60
+ })
61
+ end
62
+
63
+ alias_method :post, :expand_and_post
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module Elephrame
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elephrame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-18 00:00:00.000000000 Z
11
+ date: 2019-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.4.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: tracery
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.7'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.7'
83
97
  description:
84
98
  email:
85
99
  - thezacattacks@gmail.com
@@ -105,10 +119,20 @@ files:
105
119
  - examples/notbot_check.rb
106
120
  - examples/periodic.rb
107
121
  - examples/reply.rb
122
+ - examples/tracery_adv.rb
123
+ - examples/tracery_files/default.json
124
+ - examples/tracery_files/reply.json
125
+ - examples/tracery_files2/afternoon.json
126
+ - examples/tracery_files2/moon.json
127
+ - examples/tracery_files2/morning.json
128
+ - examples/tracery_files2/night.json
129
+ - examples/tracery_files2/reply.json
130
+ - examples/tracery_simple.rb
108
131
  - examples/watcher.rb
109
132
  - lib/elephrame.rb
110
133
  - lib/elephrame/bot.rb
111
134
  - lib/elephrame/mix/bots.rb
135
+ - lib/elephrame/mix/tracery.rb
112
136
  - lib/elephrame/rest/bots.rb
113
137
  - lib/elephrame/rest/rest.rb
114
138
  - lib/elephrame/streaming/bots.rb