picasaweb-backup 0.3.1 → 0.4.0

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/README.md CHANGED
@@ -23,8 +23,8 @@ Options
23
23
  --dir DIR optional directory in which the download should be executed
24
24
  -h, --help print this help section
25
25
  ```
26
- Create an empty folder an place a file called `account.yml` with the following
27
- content:
26
+ Create an empty folder and place a file called `account.yml` with the following
27
+ content (make sure you leave a space after the colon):
28
28
 
29
29
  ```yaml
30
30
  username: YOUR_USERNAME
data/bin/picasaweb-backup CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/ruby
1
+ #! /usr/bin/env ruby
2
2
  require 'rubygems'
3
3
  require 'optparse'
4
4
  require 'picasaweb-backup'
@@ -6,7 +6,6 @@ require 'artii'
6
6
  require 'colored'
7
7
 
8
8
  opts = {}
9
- usage =
10
9
 
11
10
  opt_parser = OptionParser.new('Backing up your Picasaweb photos') do |opt|
12
11
  artii = Artii::Base.new :font => "small"
@@ -34,58 +33,8 @@ cli = Picasaweb::CLI.new opts
34
33
 
35
34
  begin
36
35
 
37
- account = File.open("account.yml", 'r') { |f| YAML.load f }
38
- verify_account account
39
- client = picasa_client account["username"], account["password"]
40
- albums = picasa_albums client
41
- dir_name = "Albums"
36
+ cli.start_backup
42
37
 
43
- if !File.directory? dir_name
44
- Dir.mkdir dir_name
45
- end
46
-
47
- Dir.chdir dir_name
48
-
49
- albums.each do |album|
50
-
51
- if !File.directory? album[:title]
52
- Dir.mkdir album[:title]
53
- cli.print "Creating directory for album '#{album[:title]}'"
54
- end
55
-
56
- Dir.chdir album[:title] do
57
- cli.print "Checking for new files in '#{album[:title]}'"
58
- photos = nil
59
- until photos
60
- begin
61
- photos = picasa_photos client, album
62
- rescue GData::Client::ServerError
63
- "Server error, retrying\n"
64
- end
65
- end
66
-
67
- downloaded_photos = 0
68
- photos.each do |photo|
69
- if !File.exists? photo[:title]
70
- cli.print " ==> #{photo[:title]}"
71
- response = nil
72
- until response
73
- begin
74
- response = client.get photo[:url]
75
- rescue GData::Client::ServerError
76
- "Server error, retrying\n"
77
- end
78
- end
79
-
80
- File.open(photo[:title], 'w') { |f| f.write response.body }
81
- downloaded_photos += 1
82
- end
83
- end
84
- if downloaded_photos == 0
85
- cli.print "==> no new photos found"
86
- end
87
- end
88
- end
89
38
  rescue => e
90
39
  cli.print "ERROR: #{e.message}".red
91
40
  cli.print ""
@@ -12,9 +12,16 @@ require "rubygems"
12
12
  require "gdata"
13
13
  require "logger"
14
14
 
15
+ #monkey-patch logger formatting
16
+ class Logger
17
+ def format_message(level, time, progname, msg)
18
+ "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{msg}\n"
19
+ end
20
+ end
21
+
15
22
  module Picasaweb
16
23
  class CLI
17
-
24
+ ALBUM_DIR = "Albums"
18
25
  def initialize opts
19
26
  @opts = opts
20
27
 
@@ -24,13 +31,13 @@ module Picasaweb
24
31
 
25
32
  if @opts[:log]
26
33
  @logger = Logger.new "picasaweb-backup.log", shift_age = "monthly"
27
- @logger.datetime_format = "%Y-%m-%d %H:%M"
28
34
  end
29
35
 
30
36
  if opts[:dir]
31
37
  self.print "Changing working directory to #{opts[:dir]}"
32
38
  end
33
39
 
40
+ @account = File.open("account.yml", 'r') { |f| YAML.load f }
34
41
  end
35
42
 
36
43
  def print msg
@@ -41,58 +48,111 @@ module Picasaweb
41
48
  end
42
49
  end
43
50
 
44
- end
45
- end
51
+ def verify_account account
52
+ if account["username"].nil?
53
+ raise "Please add your username to account.yml"
54
+ elsif account["password"].nil?
55
+ raise "Please add your password to account.yml"
56
+ end
57
+ end
46
58
 
47
- def verify_account account
48
- if account["username"].nil?
49
- raise "Please add your username to account.yml"
50
- elsif account["password"].nil?
51
- raise "Please add your password to account.yml"
52
- end
53
- end
59
+ def picasa_client(username, password)
60
+ client = GData::Client::Photos.new
61
+ client.clientlogin(username, password)
62
+ client
63
+ end
54
64
 
55
- # config_path should point to a yaml file that looks like this:
56
- # username: costan
57
- # password: "secret"
58
- def picasa_client(username, password)
59
- client = GData::Client::Photos.new
60
- client.clientlogin(username, password)
61
- client
62
- end
65
+ def ensure_exists dir
66
+ if !File.directory? dir
67
+ Dir.mkdir dir
68
+ self.print "Creating directory '#{dir}'"
69
+ end
70
+ end
63
71
 
64
- # Retrieves all albums for a user.
65
- def picasa_albums(client, user = nil)
66
- uri = "http://picasaweb.google.com/data/feed/api/user/#{user || 'default'}"
67
- feed = client.get(uri).to_xml
68
- albums = []
69
- feed.elements.each('entry') do |entry|
70
- next unless entry.elements['gphoto:id']
71
- albums << { :id => entry.elements['gphoto:id'].text,
72
- :user => entry.elements['gphoto:user'].text,
73
- :title => entry.elements['title'].text }
74
- end
75
- albums
76
- end
72
+ # Retrieves all albums for a user.
73
+ def get_albums(client, user = nil)
74
+ uri = "http://picasaweb.google.com/data/feed/api/user/#{user || 'default'}"
75
+ feed = client.get(uri).to_xml
76
+ albums = []
77
+ feed.elements.each('entry') do |entry|
78
+ next unless entry.elements['gphoto:id']
79
+ albums << { :id => entry.elements['gphoto:id'].text,
80
+ :user => entry.elements['gphoto:user'].text,
81
+ :title => entry.elements['title'].text }
82
+ end
83
+ albums
84
+ end
85
+
86
+ def download_album client, album
87
+ Dir.chdir album[:title] do
88
+ self.print "Checking for new photos in '#{album[:title]}'"
89
+ photos = nil
90
+ until photos
91
+ begin
92
+ photos = get_photos client, album
93
+ rescue GData::Client::ServerError
94
+ "Server error, retrying\n"
95
+ end
96
+ end
97
+
98
+ downloaded_photos = 0
99
+ photos.each do |photo|
100
+ if !File.exists? photo[:title]
101
+ self.print " ==> #{photo[:title]}"
102
+ response = nil
103
+ until response
104
+ begin
105
+ response = client.get photo[:url]
106
+ rescue GData::Client::ServerError
107
+ "Server error, retrying\n"
108
+ end
109
+ end
110
+
111
+ File.open(photo[:title], 'w') { |f| f.write response.body }
112
+ downloaded_photos += 1
113
+ end
114
+ end
115
+ if downloaded_photos == 0
116
+ self.print "==> no new photos found"
117
+ end
118
+ end
119
+ end
77
120
 
78
- # Retrieves all photos from an album.
79
- def picasa_photos(client, album)
80
- uri = "http://picasaweb.google.com/data/feed/api/user/" +
121
+ # Retrieves all photos from an album.
122
+ def get_photos(client, album)
123
+ uri = "http://picasaweb.google.com/data/feed/api/user/" +
81
124
  "#{album[:user] || 'default'}/albumid/#{album[:id]}?kind=photo&imgmax=d"
82
125
 
83
- feed = client.get(uri).to_xml
84
- photos = []
85
- feed.elements.each('entry') do |entry|
86
- next unless entry.elements['gphoto:id']
87
- next unless entry.elements['media:group']
88
- photo = { :id => entry.elements['gphoto:id'].text,
89
- :album_id => entry.elements['gphoto:albumid'].text,
90
- :title => entry.elements['title'].text }
91
- entry.elements['media:group'].elements.each('media:content') do |content|
92
- photo[:url] = content.attribute('url').value
126
+ feed = client.get(uri).to_xml
127
+ photos = []
128
+ feed.elements.each('entry') do |entry|
129
+ next unless entry.elements['gphoto:id']
130
+ next unless entry.elements['media:group']
131
+ photo = { :id => entry.elements['gphoto:id'].text,
132
+ :album_id => entry.elements['gphoto:albumid'].text,
133
+ :title => entry.elements['title'].text }
134
+ entry.elements['media:group'].elements.each('media:content') do |content|
135
+ photo[:url] = content.attribute('url').value
136
+ end
137
+ photos << photo
138
+ end
139
+ photos
140
+ end
141
+
142
+ def start_backup
143
+ verify_account @account
144
+ client = picasa_client @account["username"], @account["password"]
145
+ albums = get_albums client
146
+
147
+ ensure_exists ALBUM_DIR
148
+ Dir.chdir ALBUM_DIR
149
+
150
+ albums.each do |album|
151
+ ensure_exists album[:title]
152
+ download_album client, album
153
+ end
154
+
93
155
  end
94
- photos << photo
95
156
  end
96
- photos
97
157
  end
98
158
 
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Picasaweb
2
2
  module Backup
3
- VERSION = "0.3.1"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picasaweb-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-09 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gdata_19