userscripts_org 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 / 2008-01-11
2
+
3
+ * initial release
4
+
data/README ADDED
@@ -0,0 +1,36 @@
1
+
2
+ = userscripts_org
3
+
4
+
5
+ == Description
6
+
7
+ This is manipulating library for userscripts.org.
8
+
9
+
10
+ == Installation
11
+
12
+ === Archive Installation
13
+
14
+ rake install
15
+
16
+ === Gem Installation
17
+
18
+ gem install userscripts_org
19
+
20
+
21
+ == Features/Problems
22
+
23
+
24
+ == Synopsis
25
+
26
+ This gem install "userjs" command for manipulating.
27
+
28
+ See:
29
+ $ userjs help
30
+ $ userjs help subcommand
31
+
32
+ == Copyright
33
+
34
+ Author:: cho45 <cho45@lowreal.net>
35
+ Copyright:: Copyright (c) 2008 cho45
36
+ License:: Ruby's
data/Rakefile ADDED
@@ -0,0 +1,136 @@
1
+ require 'rubygems'
2
+ require "shipit"
3
+ require 'rake'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rake/packagetask'
7
+ require 'rake/gempackagetask'
8
+ require 'rake/rdoctask'
9
+ require 'rake/contrib/rubyforgepublisher'
10
+ require 'rake/contrib/sshpublisher'
11
+ require 'fileutils'
12
+ include FileUtils
13
+
14
+ $LOAD_PATH.unshift "lib"
15
+ require "userscripts_org"
16
+
17
+ NAME = "userscripts_org"
18
+ AUTHOR = "cho45"
19
+ EMAIL = "cho45@lowreal.net"
20
+ DESCRIPTION = "Manipulate userscripts.org"
21
+ RUBYFORGE_PROJECT = "lowreal"
22
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
23
+ BIN_FILES = %w( userjs )
24
+ VERS = "0.0.0"
25
+
26
+ REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
27
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
28
+ RDOC_OPTS = [
29
+ '--title', "#{NAME} documentation",
30
+ "--charset", "utf-8",
31
+ "--opname", "index.html",
32
+ "--line-numbers",
33
+ "--main", "README",
34
+ "--inline-source",
35
+ ]
36
+
37
+ task :default => [:test]
38
+ task :package => [:clean]
39
+
40
+ Rake::TestTask.new("test") do |t|
41
+ t.libs << "test"
42
+ t.pattern = "test/**/*_test.rb"
43
+ t.verbose = true
44
+ end
45
+
46
+ spec = Gem::Specification.new do |s|
47
+ s.name = NAME
48
+ s.version = VERS
49
+ s.platform = Gem::Platform::RUBY
50
+ s.has_rdoc = true
51
+ s.extra_rdoc_files = ["README", "ChangeLog"]
52
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
53
+ s.summary = DESCRIPTION
54
+ s.description = DESCRIPTION
55
+ s.author = AUTHOR
56
+ s.email = EMAIL
57
+ s.homepage = HOMEPATH
58
+ s.executables = BIN_FILES
59
+ s.rubyforge_project = RUBYFORGE_PROJECT
60
+ s.bindir = "bin"
61
+ s.require_path = "lib"
62
+ s.autorequire = ""
63
+ s.test_files = Dir["test/test_*.rb"]
64
+
65
+ s.add_dependency('pit')
66
+ s.add_dependency('mechanize')
67
+ #s.add_dependency('activesupport', '>=1.3.1')
68
+ #s.required_ruby_version = '>= 1.8.2'
69
+
70
+ s.files = %w(README ChangeLog Rakefile) +
71
+ Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
72
+ Dir.glob("ext/**/*.{h,c,rb}") +
73
+ Dir.glob("examples/**/*.rb") +
74
+ Dir.glob("tools/*.rb")
75
+
76
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
77
+ end
78
+
79
+ Rake::GemPackageTask.new(spec) do |p|
80
+ p.need_tar = true
81
+ p.gem_spec = spec
82
+ end
83
+
84
+ task :install do
85
+ name = "#{NAME}-#{VERS}.gem"
86
+ sh %{rake package}
87
+ sh %{sudo gem install pkg/#{name}}
88
+ end
89
+
90
+ task :uninstall => [:clean] do
91
+ sh %{sudo gem uninstall #{NAME}}
92
+ end
93
+
94
+
95
+ Rake::RDocTask.new do |rdoc|
96
+ rdoc.rdoc_dir = 'html'
97
+ rdoc.options += RDOC_OPTS
98
+ rdoc.template = "resh"
99
+ #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
100
+ if ENV['DOC_FILES']
101
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
102
+ else
103
+ rdoc.rdoc_files.include('README', 'ChangeLog')
104
+ rdoc.rdoc_files.include('lib/**/*.rb')
105
+ rdoc.rdoc_files.include('ext/**/*.c')
106
+ end
107
+ end
108
+
109
+ desc "Publish to RubyForge"
110
+ task :rubyforge => [:rdoc, :package] do
111
+ require 'rubyforge'
112
+ @local_dir = "html"
113
+ @host = "cho45@rubyforge.org"
114
+ @remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{NAME}"
115
+ sh %{rsync -r --delete --verbose #{@local_dir}/ #{@host}:#{@remote_dir}}
116
+ end
117
+
118
+ Rake::ShipitTask.new do |s|
119
+ s.Step.new {
120
+ system("svn", "up")
121
+ }.and {}
122
+ s.Task :rubyforge
123
+ s.Step.new {
124
+ raise "changelog-with-hatenastar.rb is not found" unless system("which", "changelog-with-hatenastar.rb")
125
+ }.and {
126
+ system("changelog-with-hatenastar.rb")
127
+ }
128
+ s.ChangeVersion "lib/userscripts_org.rb", "VERSION"
129
+ s.Commit
130
+ s.Skip
131
+ s.Task :clean, :package
132
+ s.RubyForge
133
+ s.Tag
134
+ s.Twitter
135
+ end
136
+
data/bin/userjs ADDED
@@ -0,0 +1,205 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:ft=ruby:
3
+
4
+ require "optparse"
5
+ require "pathname"
6
+ require "userscripts_org"
7
+
8
+ class UserjsCommand
9
+ VERSION = UserscriptsOrg::VERSION
10
+ NAME = "user.js"
11
+
12
+ def self.run(argv)
13
+ new(argv.dup).run
14
+ end
15
+
16
+ def initialize(argv)
17
+ @argv = argv
18
+
19
+ @subparsers = {
20
+ "help" => OptionParser.new { |opts|
21
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
22
+ Usage: #{NAME} help <subcommand>
23
+
24
+ Show help of subcommand.
25
+ EOB
26
+ },
27
+
28
+ "list" => OptionParser.new { |opts|
29
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
30
+ Usage: #{NAME} list
31
+
32
+ List your scripts.
33
+ EOB
34
+ },
35
+
36
+ "update" => OptionParser.new { |opts|
37
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
38
+ Usage: #{NAME} update <filename>
39
+
40
+ Update <filename> script.
41
+ EOB
42
+ },
43
+
44
+ "create" => OptionParser.new { |opts|
45
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
46
+ Usage: #{NAME} create <filename>
47
+
48
+ Create entry on userscripts.org with <filename> script.
49
+ EOB
50
+ },
51
+
52
+ "install" => OptionParser.new { |opts|
53
+ opts.banner = <<-EOB.gsub(/^\t+/, "")
54
+ Usage: #{NAME} install [opts] <filename>
55
+
56
+ Install <filename> script.
57
+ EOB
58
+
59
+ opts.separator ""
60
+
61
+ opts.separator "Options:"
62
+ opts.on('-l', '--symlink', "Replace installed file with symlink.") do
63
+ @symlink = true
64
+ end
65
+ },
66
+
67
+ }
68
+
69
+ @parser = OptionParser.new do |parser|
70
+ parser.banner = <<-EOB.gsub(/^\t+/, "")
71
+ Usage: #{NAME} <subcommand> <args>
72
+
73
+ EOB
74
+
75
+ parser.separator ""
76
+
77
+ parser.separator "Subcommands:"
78
+ @subparsers.keys.sort.each do |k|
79
+ parser.separator "#{parser.summary_indent} #{k}"
80
+ end
81
+
82
+ parser.separator ""
83
+
84
+ parser.separator "Options:"
85
+ parser.on('--version', "Show version string `#{VERSION}'") do
86
+ puts VERSION
87
+ exit
88
+ end
89
+ end
90
+ end
91
+
92
+ def run
93
+ @parser.order!(@argv)
94
+ if @argv.empty?
95
+ puts @parser.help
96
+ exit
97
+ else
98
+ @subcommand = @argv.shift
99
+ method_name = "cmd_#{@subcommand}"
100
+ if self.respond_to?(method_name)
101
+ @subparsers[@subcommand].parse!(@argv)
102
+ self.send(method_name)
103
+ else
104
+ puts "Not implemented subcommand: `#{@subcommand}'."
105
+ puts
106
+ puts @parser.help
107
+ end
108
+ end
109
+ end
110
+
111
+ def cmd_help
112
+ subcommand, = @argv
113
+ if subcommand
114
+ if @subparsers.key? subcommand
115
+ puts @subparsers[subcommand].help
116
+ else
117
+ puts "No such subcommand `#{subcommand}'"
118
+ puts @parser.help
119
+ end
120
+ else
121
+ puts @parser.help
122
+ end
123
+ end
124
+
125
+ def cmd_list
126
+ usorg = login()
127
+ usorg.list.each do |s|
128
+ puts "%8d: %s" % [s.uid, s.name]
129
+ end
130
+ end
131
+
132
+ def cmd_update
133
+ abort @subparsers[@subcommand].help if @argv.empty?
134
+ content = File.read(@argv.first)
135
+ name = content[/@name\s+(.+)\s*/, 1]
136
+
137
+ usorg = login()
138
+ warn "Update..."
139
+ usorg.update(name, content)
140
+ end
141
+
142
+ def cmd_create
143
+ abort @subparsers[@subcommand].help if @argv.empty?
144
+ content = File.read(@argv.first)
145
+
146
+ usorg = login()
147
+ warn "Update..."
148
+ usorg.create(content)
149
+ end
150
+
151
+ def cmd_install
152
+ abort @subparsers[@subcommand].help if @argv.empty?
153
+ file = Pathname.new(@argv.first).realpath
154
+ prev = scan
155
+ install_userjs(file)
156
+ installed = catch(:break) {
157
+ loop do
158
+ scan.each do |f,time|
159
+ if prev.key?(f)
160
+ throw :break, f unless prev[f] == time
161
+ else
162
+ throw :break, f
163
+ end
164
+ end
165
+ sleep 1
166
+ end
167
+ }
168
+ puts "#{file} was installed as #{installed}."
169
+ puts "#{installed} will be replaced with symlink to #{file}."
170
+
171
+ FileUtils.symlink(file, installed, :force => true)
172
+ puts "done"
173
+ end
174
+
175
+
176
+ private
177
+ def login
178
+ warn "Logging in..."
179
+ UserscriptsOrg.new(Pit.get("userscripts.org", :require => {
180
+ "username" => "username",
181
+ "password" => "password"
182
+ })).login
183
+ end
184
+
185
+ def profile_dir
186
+ home = Pathname.new ENV["HOME"]
187
+ home + "Library/Application Support/Firefox/Profiles"
188
+ end
189
+
190
+ def scan
191
+ Pathname.glob(profile_dir + "*/gm_scripts/**/*.user.js").inject({}) {|r,i|
192
+ r[i] = i.mtime
193
+ r
194
+ }
195
+ end
196
+
197
+ def install_userjs(path)
198
+ system("open", "-a", "Firefox", path.to_s)
199
+ puts "Complete the install on Firefox"
200
+ end
201
+ end
202
+
203
+
204
+ UserjsCommand.run(ARGV)
205
+
@@ -0,0 +1,103 @@
1
+
2
+ require "rubygems"
3
+ require "mechanize"
4
+ require "pit"
5
+ require "yaml"
6
+ require "pathname"
7
+ require "ostruct"
8
+ require "uri"
9
+
10
+ class UserscriptsOrg
11
+ VERSION = "0.0.1"
12
+
13
+ class UserscriptsOrgError < StandardError; end
14
+ class NotLoggedinError < UserscriptsOrgError; end
15
+
16
+ BASE = URI("http://userscripts.org/")
17
+
18
+ def initialize(config)
19
+ @config = OpenStruct.new(config)
20
+ @agent = WWW::Mechanize.new
21
+ @uid = nil
22
+ end
23
+
24
+ def logined?
25
+ !!@uid
26
+ end
27
+
28
+ def login
29
+ # Login
30
+ @agent.get(BASE + "/login")
31
+ form = @agent.page.forms.with.action("/sessions").first
32
+ form.login = @config.username
33
+ form.password = @config.password
34
+ @agent.submit(form, form.buttons.name("commit"))
35
+
36
+ # Retreive user id
37
+ @uid = @agent.page.links.text("Your scripts").first.href[%r|/users/(\d+)/scripts|, 1].to_i
38
+ self
39
+ end
40
+
41
+ def list
42
+ check
43
+ @agent.get(BASE + "/users/#{@uid}/scripts")
44
+ @agent.page.links.map {|i|
45
+ uid = i.href[%r|/scripts/show/(\d+)|, 1]
46
+ uid ? Script.new(
47
+ :uid => uid.to_i,
48
+ :name => i.text
49
+ ) : nil
50
+ }.compact
51
+ end
52
+
53
+ def update(name, content)
54
+ script = list.find {|i|
55
+ i == name || i.name == name || i.uid == name
56
+ }
57
+ raise UserscriptsOrgError, "Script named #{name} is not found." if script.nil?
58
+ @agent.get BASE + "/scripts/edit_src/#{script.uid}"
59
+
60
+ form = @agent.page.forms.first
61
+ form["which_source"] = "textarea"
62
+ form["script[src]"] = content
63
+
64
+ @agent.submit(form, form.buttons.name("commit"))
65
+ if (@agent.page.root/"//p[text() = 'a few seconds ago.']").to_a.empty?
66
+ raise UserscriptsOrgError, "Update may failed"
67
+ end
68
+ script
69
+ end
70
+
71
+ def get(script)
72
+ uid = script.respond_to?(:uid) ? script.uid : script
73
+ @agent.get BASE + "/scripts/source/#{uid}.user.js"
74
+ content = @agent.page.content
75
+ Script.new({
76
+ :uid => uid,
77
+ :name => content[/@name\s+(.+)/, 1].chomp,
78
+ :content => content,
79
+ })
80
+ end
81
+
82
+ def create(content)
83
+ @agent.get BASE + "/scripts/new"
84
+ form = @agent.page.forms.first
85
+ form["which_source"] = "textarea"
86
+ form["script[src]"] = content
87
+ @agent.submit(form, form.buttons.name("commit"))
88
+
89
+ get(@agent.page.uri.to_s[%r|/scripts/show/(\d+)|, 1])
90
+ end
91
+
92
+ private
93
+ def check
94
+ unless logined?
95
+ raise NotLoggedinError, "Not Logged in. Please call 'login' method and check username and password"
96
+ end
97
+ end
98
+
99
+ class Script < OpenStruct
100
+ end
101
+ end
102
+
103
+
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/userscripts_org'
3
+
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ require "test/unit"
4
+ class UserscriptsOrgTest < Test::Unit::TestCase
5
+ def test_login
6
+ usorg = UserscriptsOrg.new(Pit.get("userscripts.org", :require => {
7
+ "username" => "username",
8
+ "password" => "password"
9
+ })).login
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: userscripts_org
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - cho45
8
+ autorequire: ""
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-11 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: pit
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: mechanize
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description: Manipulate userscripts.org
34
+ email: cho45@lowreal.net
35
+ executables:
36
+ - userjs
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ - ChangeLog
42
+ files:
43
+ - README
44
+ - ChangeLog
45
+ - Rakefile
46
+ - bin/userjs
47
+ - test/test_helper.rb
48
+ - test/userscripts_org_test.rb
49
+ - lib/userscripts_org.rb
50
+ has_rdoc: true
51
+ homepage: http://lowreal.rubyforge.org
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --title
55
+ - userscripts_org documentation
56
+ - --charset
57
+ - utf-8
58
+ - --opname
59
+ - index.html
60
+ - --line-numbers
61
+ - --main
62
+ - README
63
+ - --inline-source
64
+ - --exclude
65
+ - ^(examples|extras)/
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project: lowreal
83
+ rubygems_version: 1.0.1
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: Manipulate userscripts.org
87
+ test_files:
88
+ - test/test_helper.rb