little_fish 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.
- data/.gitignore +17 -0
- data/.rvmrc +81 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +8 -0
- data/bin/littlefish +5 -0
- data/lib/little_fish/fisher.rb +114 -0
- data/lib/little_fish/runner.rb +27 -0
- data/lib/little_fish/version.rb +3 -0
- data/lib/little_fish.rb +5 -0
- data/little_fish.gemspec +22 -0
- data/spec/lib/little_fish_spec.rb +14 -0
- data/spec/spec_helper.rb +2 -0
- metadata +95 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
|
7
|
+
environment_id="ruby-1.9.3-p0@bigfish"
|
8
|
+
|
9
|
+
#
|
10
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
11
|
+
#
|
12
|
+
# rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
#
|
18
|
+
|
19
|
+
#
|
20
|
+
# Uncomment following line if you want options to be set only for given project.
|
21
|
+
#
|
22
|
+
# PROJECT_JRUBY_OPTS=( --1.9 )
|
23
|
+
#
|
24
|
+
# The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
|
25
|
+
#
|
26
|
+
# chmod +x ${rvm_path}/hooks/after_use_jruby_opts
|
27
|
+
#
|
28
|
+
|
29
|
+
#
|
30
|
+
# First we attempt to load the desired environment directly from the environment
|
31
|
+
# file. This is very fast and efficient compared to running through the entire
|
32
|
+
# CLI and selector. If you want feedback on which environment was used then
|
33
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
34
|
+
#
|
35
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
36
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
37
|
+
then
|
38
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
39
|
+
|
40
|
+
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
41
|
+
then
|
42
|
+
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
43
|
+
fi
|
44
|
+
else
|
45
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
46
|
+
if ! rvm --create "$environment_id"
|
47
|
+
then
|
48
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
49
|
+
return 1
|
50
|
+
fi
|
51
|
+
fi
|
52
|
+
|
53
|
+
#
|
54
|
+
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
|
55
|
+
# it be automatically loaded. Uncomment the following and adjust the filename if
|
56
|
+
# necessary.
|
57
|
+
#
|
58
|
+
# filename=".gems"
|
59
|
+
# if [[ -s "$filename" ]]
|
60
|
+
# then
|
61
|
+
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
|
62
|
+
# fi
|
63
|
+
|
64
|
+
# If you use bundler, this might be useful to you:
|
65
|
+
# if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
|
66
|
+
# then
|
67
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
68
|
+
# gem install bundler
|
69
|
+
# fi
|
70
|
+
# if [[ -s Gemfile ]] && command -v bundle
|
71
|
+
# then
|
72
|
+
# bundle install
|
73
|
+
# fi
|
74
|
+
|
75
|
+
if [[ $- == *i* ]] # check for interactive shells
|
76
|
+
then
|
77
|
+
echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
|
78
|
+
else
|
79
|
+
echo "Using: $GEM_HOME" # don't use colors in interactive shells
|
80
|
+
fi
|
81
|
+
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 yeer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# LittleFish
|
2
|
+
|
3
|
+
This gem is a little tool for pulling infomation from boston bigpicture, which is a news site with beautiful photos.
|
4
|
+
The reutrn info format is a data sructure looks like:
|
5
|
+
|
6
|
+
bp: info
|
7
|
+
description : text
|
8
|
+
image_list : list
|
9
|
+
image_url : string
|
10
|
+
desc : text
|
11
|
+
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'little_fish'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install little_fish
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
1. demo
|
29
|
+
little_fish --help
|
30
|
+
|
31
|
+
2. add this line in your application
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'little_fish'
|
35
|
+
fisher = LittleFish::Fisher.new do |f|
|
36
|
+
f.last_nth_item = 0 #0 for latest post, and 1 means the last one, 2 means the one before last post, eg.
|
37
|
+
f.dumpfile = 'temp.txt'#if not assigned, filename will be named by datetime. if assigned 'no', 'none', or 'false', it wont dump out any file
|
38
|
+
end
|
39
|
+
info = fisher.pull
|
40
|
+
```
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/littlefish
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'rss/1.0'
|
2
|
+
require 'rss/2.0'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'timeout'
|
6
|
+
|
7
|
+
module LittleFish
|
8
|
+
class Fisher
|
9
|
+
attr_accessor :url, :last_nth_item, :dumpfile
|
10
|
+
def initialize
|
11
|
+
if block_given?
|
12
|
+
yield self
|
13
|
+
else
|
14
|
+
@url = 'http://feeds.boston.com/boston/bigpicture/index'
|
15
|
+
@last_nth_item = 0
|
16
|
+
@dumpfile = './pic_output_' + Time.now.strftime('%H-%M-%S') + '.txt'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def pull
|
21
|
+
info 'opening URI ... '
|
22
|
+
rss_string = open_url(@url)
|
23
|
+
|
24
|
+
info 'parsing doc from text ... '
|
25
|
+
doc = get_doc(rss_string, @last_nth_item)
|
26
|
+
|
27
|
+
info 'filling doc into bp_info ... '
|
28
|
+
bp_info = get_bp_info(doc)
|
29
|
+
|
30
|
+
unless ['none', 'NONE', 'no', 'NO', 'false', 'FALSE'].include? @dumpfile
|
31
|
+
info 'dumping bp_info ... '
|
32
|
+
f = File.new(@dumpfile, 'w')
|
33
|
+
bp_info[:images].each do |imgs|
|
34
|
+
f.puts imgs
|
35
|
+
end
|
36
|
+
info 'saved info in ' + @dumpfile
|
37
|
+
end
|
38
|
+
|
39
|
+
bp_info
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def info(str)
|
44
|
+
puts "LittleFish(INFO): " + str
|
45
|
+
end
|
46
|
+
|
47
|
+
def warning(str)
|
48
|
+
puts "LittleFish(WARNING): " + str
|
49
|
+
end
|
50
|
+
|
51
|
+
def error(str)
|
52
|
+
puts "LittleFish(ERROR): " + str
|
53
|
+
end
|
54
|
+
|
55
|
+
def open_url(uri)
|
56
|
+
retries = 3
|
57
|
+
begin
|
58
|
+
Timeout::timeout 10 do
|
59
|
+
rss_string = open(@url).read
|
60
|
+
end
|
61
|
+
rescue Timeout::Error
|
62
|
+
retries -= 1
|
63
|
+
if retries > 0
|
64
|
+
warning 'retrying .. '
|
65
|
+
sleep 2 and retry
|
66
|
+
else
|
67
|
+
error "can not open url: #{@url}"
|
68
|
+
raise
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_doc(rss_str, n)
|
74
|
+
rss = nil
|
75
|
+
begin
|
76
|
+
Timeout::timeout 10 do
|
77
|
+
rss = RSS::Parser.parse(rss_str, false)
|
78
|
+
end
|
79
|
+
rescue
|
80
|
+
retries -= 1
|
81
|
+
if retries > 0
|
82
|
+
warning 'retrying .. '
|
83
|
+
sleep 2 and retry
|
84
|
+
else
|
85
|
+
error 'cat not parse rss, maybe try it again'
|
86
|
+
raise
|
87
|
+
end
|
88
|
+
end
|
89
|
+
link = rss.items[n].link
|
90
|
+
doc = Nokogiri::HTML(open(link))
|
91
|
+
end
|
92
|
+
|
93
|
+
def get_bp_info(doc)
|
94
|
+
bp_info = {}
|
95
|
+
total_desc = doc.css('.bpBody').children.first.content
|
96
|
+
bp_info[:total_desc]=total_desc
|
97
|
+
|
98
|
+
img_list = []
|
99
|
+
top = doc.css('.bpImageTop').children.children
|
100
|
+
img_url = top.first.attributes['src'].value
|
101
|
+
img_info = top[1].content
|
102
|
+
img_list << { img_url => img_info }
|
103
|
+
|
104
|
+
both = doc.css('.bpBoth')
|
105
|
+
both.each do |e|
|
106
|
+
img_url = e.children[1].attributes['src'].value
|
107
|
+
img_info = e.children.last.content
|
108
|
+
img_list << { img_url => img_info }
|
109
|
+
end
|
110
|
+
bp_info[:images] = img_list
|
111
|
+
bp_info
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module LittleFish
|
4
|
+
class Runner
|
5
|
+
def initialize(arguments)
|
6
|
+
@arguments = arguments
|
7
|
+
@demo = LittleFish::Fisher.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
parse_options
|
12
|
+
@demo.pull
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_options
|
18
|
+
options = OptionParser.new
|
19
|
+
options.banner = "Usage: littlefish [options]"
|
20
|
+
options.on('-f', '--file FILENAME', "Dumping info into file name by datetime in current directory. NOTE: use '-f no', '-f none', or '-f false' will disable dumping into any file") { |filename| @demo.dumpfile = filename }
|
21
|
+
options.on('-n', '--number NUM', "Pull last x th item by given num") { |n| @demo.last_nth_item = n.to_i }
|
22
|
+
options.on('-u', '--url URL', "Redirect the url") { |url| @demo.url = url }
|
23
|
+
options.on('-h', '--help', "Show this message") { puts(options); exit }
|
24
|
+
options.parse!(@arguments)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/little_fish.rb
ADDED
data/little_fish.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/little_fish/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["yeer"]
|
6
|
+
gem.email = ["athom@126.com"]
|
7
|
+
gem.description = %q{my first toy gem}
|
8
|
+
gem.summary = %q{it's just pulling data from website boston bigpicture}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "little_fish"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = LittleFish::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
gem.add_development_dependency 'nokogiri'
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LittleFish do
|
4
|
+
it 'should return info and dump file in a pulling' do
|
5
|
+
#TODO for somereason open-uri is not availble in spec
|
6
|
+
# fisher = LittleFish::Fisher.new do |f|
|
7
|
+
# f.dumpfile = 'spec_test.txt'
|
8
|
+
# end
|
9
|
+
# info = fisher.pull
|
10
|
+
# info.should_not be_nil
|
11
|
+
#File.exist?('spec_test.txt').should be_true
|
12
|
+
#File.delete 'spec_test.txt'
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: little_fish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- yeer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70115706073680 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70115706073680
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70115706073220 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70115706073220
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: nokogiri
|
38
|
+
requirement: &70115706072740 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70115706072740
|
47
|
+
description: my first toy gem
|
48
|
+
email:
|
49
|
+
- athom@126.com
|
50
|
+
executables:
|
51
|
+
- littlefish
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rvmrc
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- bin/littlefish
|
62
|
+
- lib/little_fish.rb
|
63
|
+
- lib/little_fish/fisher.rb
|
64
|
+
- lib/little_fish/runner.rb
|
65
|
+
- lib/little_fish/version.rb
|
66
|
+
- little_fish.gemspec
|
67
|
+
- spec/lib/little_fish_spec.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
homepage: ''
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.15
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: it's just pulling data from website boston bigpicture
|
93
|
+
test_files:
|
94
|
+
- spec/lib/little_fish_spec.rb
|
95
|
+
- spec/spec_helper.rb
|