wpps 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'twitter'
5
+ require 'pastie-api'
6
+ require 'optparse'
7
+ require 'nokogiri'
8
+ require 'wputils'
9
+ require 'whiplash/version'
10
+
11
+ usage = "Usage: pwrslv [OPTIONS]"
12
+
13
+ options = { :maxtweets => 1,
14
+ :interative => false,
15
+ :verbose => 0
16
+ }
17
+ OptionParser.new do |opt|
18
+ opt.banner = usage
19
+ opt.version = Whiplash::VERSION
20
+ opt.separator ""
21
+ opt.separator "Specific options:"
22
+ opt.on('-i',
23
+ '--interactive',
24
+ 'This version is not yet implemented'
25
+ ) { |o| options [:interactive] = true }
26
+ opt.on('-m',
27
+ '--max MAX',
28
+ Integer,
29
+ 'Maximum number of tweets retrieve from',
30
+ 'a specific TAG'
31
+ ) { |o| options[:maxtweets] = o.to_i }
32
+ opt.on('-t',
33
+ '--tag TAG',
34
+ String,
35
+ 'A TAG that identifies a group',
36
+ 'of persons interested on the message'
37
+ ) { |o| options[:tag] = o }
38
+ opt.on('--verbose LEVEL') do |o|
39
+ options[:verbose] = o.to_i
40
+ unless 0 <= options[:verbose] and options[:verbose] <= 2
41
+ puts opt.help
42
+ exit 1
43
+ end
44
+ end
45
+ opt.on('-v', '--version') do |o|
46
+ puts 'pwrslv version "' + opt.version + '"'
47
+ exit 0
48
+ end
49
+ opt.on('-h', '--help') do
50
+ puts opt.help
51
+ exit 0
52
+ end
53
+ end.parse!
54
+
55
+ unless ARGV.empty? then
56
+ puts usage
57
+ exit 1
58
+ end
59
+
60
+ usertwitter=ARGV[0]
61
+
62
+ yamlconf = getPScfgFile()
63
+ client = get_tweeter_client(yamlconf)
64
+
65
+ begin
66
+ tag = getWPTag(options)
67
+ tweets = client.search(tag)
68
+
69
+ nro = 0
70
+ tweets.each do |tw|
71
+ nro += 1
72
+ # puts "Tweet text: #{tw.text}"
73
+ uri = /http:\/\/t.co\/.*/.match(tw.text)
74
+ unless uri == nil
75
+ # puts "Getting uri: #{uri[0]} short uri:"
76
+ unless uri[0] == nil
77
+ ruri = resolve_redirects(uri[0])
78
+ idinfo = /http:\/\/pastie\.org\/(\d+)/.match(ruri)
79
+ # puts "Processing idinfo: #{idinfo[0]}"
80
+ unless idinfo[1] == nil
81
+ # puts "Getting Pastie: #{idinfo[1]}"
82
+ p = Pastie.get(idinfo[1])
83
+ # puts "Pastie: #{p}"
84
+ unless p == nil
85
+ doc = Nokogiri::HTML(p.content)
86
+ src = doc.at('body').at('pre').
87
+ inner_html.gsub("<br>", "\n").gsub("<br/>", "\n").
88
+ gsub("&lt;","<").gsub("&gt;",">").gsub("&amp;", "&")
89
+ lines = src.split(/\n/)
90
+ lastLine = lines[lines.length - 1]
91
+ psflnm = /.*filename: (.*)/.match(lines[lines.length - 1])
92
+ if psflnm[1] == nil
93
+ puts "Cannot get filename from pastie link. Process ends"
94
+ exit 1
95
+ end
96
+ filename = psflnm[1]
97
+ print "Start writing #{filename}"
98
+ $stdout.flush
99
+ begin
100
+ file = File.open(filename, "w+")
101
+ nwlines = lines.first(lines.length - 1)
102
+ nwlines.each do |line|
103
+ file.puts(line)
104
+ end
105
+ rescue Exception => e
106
+ puts "\nError writting #{filename} due: #{e}"
107
+ exit 1
108
+ end
109
+ puts " End writing #{filename}"
110
+ end
111
+ end
112
+ end
113
+ end
114
+ break if nro == options[:maxtweets]
115
+ end
116
+ rescue Exception => e
117
+ puts e
118
+ exit 1
119
+ end
data/bin/wp ADDED
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'twitter'
5
+ require 'pastie-api'
6
+ require 'optparse'
7
+ require 'extensions'
8
+ require 'wputils'
9
+ require 'whiplash/version'
10
+
11
+ usage = "Usage: wp [OPTIONS]"
12
+
13
+ options = {}
14
+ OptionParser.new do |opt|
15
+ opt.banner = usage
16
+ opt.version = Whiplash::VERSION
17
+ opt.separator ""
18
+ opt.separator "Specific options:"
19
+ opt.on('-m',
20
+ '--message MESSAGE',
21
+ String,
22
+ 'A MESSAGE to be sent') { |o| options[:message] = o }
23
+ opt.on('-t',
24
+ '--tag TAG',
25
+ String,
26
+ 'A TAG that identifies a group',
27
+ 'of persons interested on the message'
28
+ ) { |o| options[:tag] = o }
29
+ opt.on('-v', '--version') do |o|
30
+ puts 'wp version "' + opt.version + '"'
31
+ exit 0
32
+ end
33
+ opt.on('-h', '--help') do |o|
34
+ puts opt.help
35
+ exit
36
+ end
37
+ end.parse!
38
+
39
+ if ARGV.empty? or ARGV.length > 1 then
40
+ puts usage
41
+ exit 1
42
+ end
43
+
44
+ sendfile=ARGV[0]
45
+ unless options[:message] == nil
46
+ user_message = options[:message]
47
+ else
48
+ user_message = ""
49
+ end
50
+
51
+ yamlconf = getWPcfgFile()
52
+
53
+ # Reading a file
54
+ unless File::exist? sendfile or File::readable? sendfile
55
+ puts "Error: file #{sendfile} does not exist or not readable"
56
+ exit 1
57
+ end
58
+
59
+ file = File.new(sendfile, "r")
60
+ conts = ""
61
+ file.each_line do |line|
62
+ conts += line
63
+ end
64
+ file.close
65
+
66
+ # Create a new paste
67
+ le = LangExtensions.instance
68
+ lang = le.getLangDesc(File.extname sendfile)
69
+ exts = File.extname sendfile
70
+ comm = lang.prodComment('filename: ' + (File.basename sendfile))
71
+ conts = conts + comm
72
+ p = Pastie.create(conts, false, lang.name)
73
+
74
+ # Getting environment variables that contains the tag info
75
+ tag = getWPTag(options)
76
+
77
+ # Setting twitter client
78
+ client = get_tweeter_client(yamlconf)
79
+
80
+ # Send a twitter with new file info
81
+ begin
82
+ tweet_message = tag + " " + user_message + " " + p.link
83
+ if tweet_message.length > 240
84
+ puts "wp cannot send message: #{tweet_message} because too long: #{tweet_emssage.length}"
85
+ exit 1
86
+ end
87
+ tweet = client.update(tweet_message)
88
+ puts "Tweet id: #{tweet.id}"
89
+ rescue Exception => e
90
+ puts "wp could not sent message send due: #{e.message}"
91
+ exit 1
92
+ end
@@ -0,0 +1,138 @@
1
+ require 'singleton'
2
+
3
+ LangDescr = Class.new do
4
+ attr_accessor :name, :ext
5
+ attr_writer :genComment
6
+
7
+ def prodComment(str)
8
+ @genComment.call(str)
9
+ end
10
+ end
11
+
12
+ $hashComment = Proc.new do |str|
13
+ "# " + str + "\n"
14
+ end
15
+
16
+ $dashComment = Proc.new do |str|
17
+ "-- " + str + "\n"
18
+ end
19
+
20
+ $backComment = Proc.new do |str|
21
+ "// " + str + "\n"
22
+ end
23
+
24
+ $backAstComment = Proc.new do |str|
25
+ "/* " + str + " */" + "\n"
26
+ end
27
+
28
+ $smlComment = Proc.new do |str|
29
+ "(* " + str + " *)" + "\n"
30
+ end
31
+
32
+ $plainComment = Proc.new do |str|
33
+ ">> " + str + "\n"
34
+ end
35
+
36
+ LangExtensions = Class.new do
37
+ include Singleton
38
+
39
+ def initialize()
40
+ rubyLang = LangDescr.new.tap do |r|
41
+ r.name = "ruby"
42
+ r.ext = [".rb"]
43
+ r.genComment = $hashComment
44
+ end
45
+
46
+ haskellLang = LangDescr.new.tap do |h|
47
+ h.name = "Haskell"
48
+ h.ext = [".hs", ".lhs"]
49
+ h.genComment = $dashComment
50
+ end
51
+
52
+ cLang = LangDescr.new.tap do |c|
53
+ c.name = "c"
54
+ c.ext = [".c"]
55
+ c.genComment = $backComment
56
+ end
57
+
58
+ cppLang = LangDescr.new.tap do |cpp|
59
+ cpp.name = "c"
60
+ cpp.ext = [".cc", ".cpp", ".cxx", ".C"]
61
+ cpp.genComment = $backComment
62
+ end
63
+
64
+ prologLang = LangDescr.new.tap do |pl|
65
+ pl.name = "prolog"
66
+ pl.ext = [".pl"]
67
+ pl.genComment = $backAstComment
68
+ end
69
+
70
+ smlLang = LangDescr.new.tap do |sml|
71
+ sml.name = "Standard sml"
72
+ sml.ext = [".sml"]
73
+ sml.genComment = $smlComment
74
+ end
75
+
76
+ javaLang = LangDescr.new.tap do |j|
77
+ j.name = "java"
78
+ j.ext = [".java"]
79
+ j.genComment = $backComment
80
+ end
81
+
82
+ eweLang = LangDescr.new.tap do |e|
83
+ e.name = "ewe"
84
+ e.ext = [".ewe"]
85
+ e.genComment = $hashComment
86
+ end
87
+
88
+ tclLang = LangDescr.new.tap do |t|
89
+ t.name = "tcl"
90
+ t.ext = [".tcl"]
91
+ t.genComment = $hashComment
92
+ end
93
+
94
+ bashLang = LangDescr.new.tap do |b|
95
+ b.name = "bash"
96
+ b.ext = [".bs"]
97
+ b.genComment = $hashComment
98
+ end
99
+
100
+ textLang = LangDescr.new.tap do |t|
101
+ t.name = "plain text"
102
+ t.ext = [".txt"]
103
+ t.genComment = $plainComment
104
+ end
105
+
106
+ # puts "Lenguaje: #{rubyLang.ext[0]} objeto: #{rubyLang}"
107
+
108
+ @filexts = { rubyLang.ext[0] => rubyLang,
109
+ haskellLang.ext[0] => haskellLang,
110
+ haskellLang.ext[1] => haskellLang,
111
+ cLang.ext[0] => cLang,
112
+ cppLang.ext[0] => cppLang,
113
+ cppLang.ext[1] => cppLang,
114
+ cppLang.ext[2] => cppLang,
115
+ cppLang.ext[3] => cppLang,
116
+ prologLang.ext[0] => prologLang,
117
+ smlLang.ext[0] => smlLang,
118
+ bashLang.ext[0] => bashLang,
119
+ eweLang.ext[0] => eweLang,
120
+ javaLang.ext[0] => javaLang,
121
+ textLang.ext[0] => textLang
122
+ }
123
+ end
124
+
125
+ def getLangDesc(ext)
126
+ if @filexts[ext] == nil
127
+ ext = ".txt"
128
+ end
129
+ @filexts[ext]
130
+ end
131
+ end
132
+
133
+ # Testing
134
+ # le = LangExtensions.instance
135
+ # lang = le.getLangDesc('.hs')
136
+ # comm = lang.prodComment('Hola mundo')
137
+ #
138
+ # puts "extensions"
@@ -0,0 +1,3 @@
1
+ class Whiplash
2
+ VERSION = '0.2.3'
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'twitter'
3
+ require 'yaml'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+
7
+ def resolve_redirects(url)
8
+ response = fetch_response(url, method= :head)
9
+ if response
10
+ return response.to_hash[:url].to_s
11
+ else
12
+ return nil
13
+ end
14
+ end
15
+
16
+ def fetch_response(url, method= :get)
17
+ conn = Faraday.new do |b|
18
+ b.use FaradayMiddleware::FollowRedirects;
19
+ b.adapter :net_http
20
+ end
21
+ return conn.send method, url
22
+ rescue Faraday::Error, Faraday::Error::ConnectionFailed => e
23
+ return nil
24
+ end
25
+
26
+ def getWPcfgFile()
27
+ filepath=ENV["HOME"] + "/" + ".whiplash"
28
+ YAML.load_file(filepath)
29
+ end
30
+
31
+ def getPScfgFile()
32
+ filepath=ENV["HOME"] + "/" + ".powerslave"
33
+ YAML.load_file(filepath)
34
+ end
35
+
36
+ def get_tweeter_client(yamlconf)
37
+ client = Twitter::REST::Client.new do |config|
38
+ config.consumer_key = yamlconf['consumer_key']
39
+ config.consumer_secret = yamlconf['consumer_secret']
40
+ config.access_token = yamlconf['access_token']
41
+ config.access_token_secret = yamlconf['access_token_secret']
42
+ end
43
+ end
44
+
45
+ def getWPTag(options)
46
+ pTag="NOTAGDEF"
47
+ if options[:tag] != nil
48
+ pTag = options[:tag]
49
+ elsif ENV["WPTAG"] != nil
50
+ pTag = ENV["WPTAG"]
51
+ end
52
+ return ("#" + pTag)
53
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wpps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Juan Francisco Cardona McCormick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: twitter
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 5.14.0
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: 5.14.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 5.14.0
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: 5.14.0
36
+ - !ruby/object:Gem::Dependency
37
+ name: pastie-api
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.2.1
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 0.2.1
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.1
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 0.2.1
58
+ - !ruby/object:Gem::Dependency
59
+ name: faraday
60
+ requirement: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.9.1
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.1
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.9.1
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 0.9.1
80
+ - !ruby/object:Gem::Dependency
81
+ name: faraday_middleware
82
+ requirement: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 0.10.0
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: 0.10.0
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 0.10.0
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 0.10.0
102
+ - !ruby/object:Gem::Dependency
103
+ name: nokogiri
104
+ requirement: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.6.6.2
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: 1.6.6.2
113
+ type: :runtime
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ~>
119
+ - !ruby/object:Gem::Version
120
+ version: 1.6.6.2
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: 1.6.6.2
124
+ description: Share snippets of code with a group of persons by twitter
125
+ email: jfcmacro@gmail.com
126
+ executables:
127
+ - wp
128
+ - pwrslv
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - lib/extensions.rb
133
+ - lib/wputils.rb
134
+ - lib/whiplash/version.rb
135
+ - bin/wp
136
+ - bin/pwrslv
137
+ homepage: https://github.com/jfcmacro/whiplash
138
+ licenses:
139
+ - ARTISTIC
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.23
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: The whip hand
162
+ test_files: []