copiedbook 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +22 -0
- data/Rakefile +1 -0
- data/bin/copiedbook +32 -0
- data/copiedbook.gemspec +19 -0
- data/lib/copiedbook/fb_calls.rb +45 -0
- data/lib/copiedbook/helpers.rb +46 -0
- data/lib/copiedbook/version.rb +3 -0
- data/lib/copiedbook.rb +94 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Markus Kuhnt
|
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,22 @@
|
|
1
|
+
copiedbook
|
2
|
+
==========
|
3
|
+
|
4
|
+
Take a offline copy of a facebook fan page.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'copiedbook'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install copiedbook
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
just run ./bin/copiedbook to get more information about how to specifiy what and by whom to download.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/copiedbook
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
4
|
+
begin
|
5
|
+
require 'logger'
|
6
|
+
require 'date'
|
7
|
+
require 'erb'
|
8
|
+
require 'fileutils'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rubygems'
|
11
|
+
require 'logger'
|
12
|
+
require 'date'
|
13
|
+
require 'erb'
|
14
|
+
require 'fileutils'
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'copiedbook'
|
18
|
+
|
19
|
+
# Configuration
|
20
|
+
# Config variables
|
21
|
+
debug = false # Set to true to get some debug information
|
22
|
+
current_path = File.expand_path(File.dirname(__FILE__) + '/../')
|
23
|
+
$log_limit = 10 # How many log files to store on log path
|
24
|
+
$log_path = current_path + "/log"
|
25
|
+
log_file = $log_path + "/main-" +
|
26
|
+
DateTime.now.strftime("%Y%m%d-%H%M%S") + ".log"
|
27
|
+
log_file = Logger.new(log_file)
|
28
|
+
CONSTANT = "..."
|
29
|
+
variable = "..."
|
30
|
+
|
31
|
+
machine = Copiedbook::CopyMachine.new(log_file)
|
32
|
+
machine.run_it
|
data/copiedbook.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'copiedbook/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "copiedbook"
|
8
|
+
gem.version = Copiedbook::VERSION
|
9
|
+
gem.authors = ["Markus Kuhnt", "Ignacio Huerta"]
|
10
|
+
gem.email = ["info@tridoco.com"]
|
11
|
+
gem.description = %q{Take a offline copy of a facebook fan page.}
|
12
|
+
gem.summary = %q{Take a offline copy of a facebook fan page.}
|
13
|
+
gem.homepage = "http://www.tridoco.com"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module FBCalls
|
4
|
+
|
5
|
+
def get_feed
|
6
|
+
if @token.empty?
|
7
|
+
puts "Introduce a new token"
|
8
|
+
@token = gets.chomp
|
9
|
+
end
|
10
|
+
@graph = Koala::Facebook::API.new(@token)
|
11
|
+
begin
|
12
|
+
feed = @graph.get_connections(@fanpage_id, "feed")
|
13
|
+
rescue Exception => ex
|
14
|
+
feed = handle_error(ex)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_next_page(feed,parameters = nil)
|
19
|
+
begin
|
20
|
+
parameters = feed.next_page_params if parameters == nil
|
21
|
+
return nil if parameters == nil
|
22
|
+
feed = @graph.get_page(parameters)
|
23
|
+
rescue Exception => ex
|
24
|
+
feed = handle_error(ex)
|
25
|
+
get_next_page(feed, parameters)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_comment_feed(post)
|
30
|
+
begin
|
31
|
+
comment_feed = @graph.get_connections(post["id"], "comments")
|
32
|
+
rescue Exception => ex
|
33
|
+
feed = handle_error(ex)
|
34
|
+
get_comment_feed
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def handle_error(ex)
|
39
|
+
File.open(@output_file, 'w') {|f| f.write(@output.to_json) }
|
40
|
+
puts "Facebook has returned an error: #{ex.message}"
|
41
|
+
@token = ""
|
42
|
+
return get_feed
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Helpers
|
2
|
+
|
3
|
+
def log_this(message, type_of_message="info")
|
4
|
+
@log_file.send type_of_message.to_sym, message
|
5
|
+
if type_of_message != 'debug' || $debug == true
|
6
|
+
puts("#{type_of_message.upcase} : #{message}")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def run_command(command,error_msg)
|
11
|
+
log_this("#{command}",'debug')
|
12
|
+
output = `#{command}`
|
13
|
+
result=$?.success?
|
14
|
+
if result == false
|
15
|
+
log_this(error_msg,'error')
|
16
|
+
exit(1)
|
17
|
+
end
|
18
|
+
return output
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_sentence(the_array)
|
22
|
+
if the_array.length == 1
|
23
|
+
return the_array[0]
|
24
|
+
else
|
25
|
+
return "#{the_array[0, the_array.length-1].join(', ')} and #{the_array.last}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def clean_the_log
|
30
|
+
log_this('Cleaning log folder!','debug')
|
31
|
+
# Get the filenames of log folder on an array sorted by
|
32
|
+
# modification time (oldest first)
|
33
|
+
log_files = Dir.glob($log_path + '/*.log').sort_by do
|
34
|
+
|file| File.mtime(file)
|
35
|
+
end
|
36
|
+
log_this("No log file will be cleaned because the limit hasn't be reached :)", 'debug') if log_files.size <= $log_limit
|
37
|
+
# Operate over the array until we reach the established limit
|
38
|
+
# of files for the log path
|
39
|
+
while log_files.size > $log_limit
|
40
|
+
log_this("Log file will be deleted: #{log_files.first}",'debug')
|
41
|
+
File.delete(log_files.first)
|
42
|
+
log_files.delete log_files.first
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/lib/copiedbook.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require "copiedbook/version"
|
2
|
+
require "copiedbook/helpers"
|
3
|
+
require "copiedbook/fb_calls"
|
4
|
+
|
5
|
+
module Copiedbook
|
6
|
+
class CopyMachine
|
7
|
+
include Helpers
|
8
|
+
include FBCalls
|
9
|
+
|
10
|
+
def initialize(log_file)
|
11
|
+
@log_file = log_file
|
12
|
+
#log_this("\e[1m# Description to show on screen...\e[0m\n")
|
13
|
+
|
14
|
+
|
15
|
+
# Arguments from command line
|
16
|
+
@fanpage_id = ARGV[0]
|
17
|
+
@token = ARGV[1]
|
18
|
+
@output_file = ARGV[2]
|
19
|
+
end
|
20
|
+
|
21
|
+
def info
|
22
|
+
puts("\e[1mName:\e[0m Facebook Dumper")
|
23
|
+
puts("\e[1mDescription:\e[0m This scripts dumps JSON from a Facebook fanpage")
|
24
|
+
puts("\e[1mCreated at:\e[0m 12/12/12")
|
25
|
+
end
|
26
|
+
|
27
|
+
def help
|
28
|
+
puts "Not enough arguments. Example: \nruby copiedbook.rb 270335753072803 AAACEdEose0cBAIGTvuh38rLhsDD4jJdJjApGjH8z6uUmZCMH0I0IptiQ3z9qbbzVNITJtLJ2ReL4ZBwJGZCAtHAdpMT1wYlY1Qu9A71rnwxf8RPG3QZA my_file.json"
|
29
|
+
end
|
30
|
+
|
31
|
+
def main
|
32
|
+
puts(" ---- STARTING ----\n\n")
|
33
|
+
|
34
|
+
# Connect to Facebook and get the feed with all posts
|
35
|
+
require 'koala'
|
36
|
+
@graph = Koala::Facebook::API.new(@token)
|
37
|
+
@output = []
|
38
|
+
|
39
|
+
feed = get_feed
|
40
|
+
|
41
|
+
counter = 1
|
42
|
+
until feed == nil
|
43
|
+
@output += feed
|
44
|
+
puts "We are in page #{counter} of the fanpage's feed"
|
45
|
+
counter += 1
|
46
|
+
feed = get_next_page(feed)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
# Iterate through the feed to get all comments
|
51
|
+
@output.each_with_index do |post, index|
|
52
|
+
if post["comments"]["count"] > 0
|
53
|
+
puts " The post #{post["id"]} has #{post["comments"]["count"]} comments. We are going to retrieve them now."
|
54
|
+
comments = []
|
55
|
+
comment_feed = get_comment_feed(post)
|
56
|
+
until comment_feed == nil
|
57
|
+
comments += comment_feed
|
58
|
+
comment_feed = get_next_page(comment_feed)
|
59
|
+
end
|
60
|
+
post["comments"] = comments
|
61
|
+
@output[index] = post
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Write the file
|
66
|
+
File.open(@output_file, 'w') {|f| f.write(@output.to_json) }
|
67
|
+
|
68
|
+
puts("\n\n ---- ENDING ----\n")
|
69
|
+
clean_the_log
|
70
|
+
exit(0)
|
71
|
+
end
|
72
|
+
|
73
|
+
def run_it
|
74
|
+
if ARGV.size == 3
|
75
|
+
ARGV.clear
|
76
|
+
main
|
77
|
+
else
|
78
|
+
case ARGV[0]
|
79
|
+
when "help"
|
80
|
+
help
|
81
|
+
when "info"
|
82
|
+
info
|
83
|
+
else
|
84
|
+
help
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: copiedbook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Markus Kuhnt
|
9
|
+
- Ignacio Huerta
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Take a offline copy of a facebook fan page.
|
16
|
+
email:
|
17
|
+
- info@tridoco.com
|
18
|
+
executables:
|
19
|
+
- copiedbook
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/copiedbook
|
29
|
+
- copiedbook.gemspec
|
30
|
+
- lib/copiedbook.rb
|
31
|
+
- lib/copiedbook/fb_calls.rb
|
32
|
+
- lib/copiedbook/helpers.rb
|
33
|
+
- lib/copiedbook/version.rb
|
34
|
+
homepage: http://www.tridoco.com
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.23
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Take a offline copy of a facebook fan page.
|
58
|
+
test_files: []
|