ayadn 2.1 → 3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,431 +0,0 @@
1
- # encoding: utf-8
2
- module Ayadn
3
- class Migration
4
-
5
- begin
6
- require 'daybreak'
7
- rescue LoadError => e
8
- puts "\nAYADN: Error while loading Gems\n\n"
9
- puts "RUBY: #{e}\n\n"
10
- exit
11
- end
12
-
13
- def initialize
14
- @still = false
15
- @thor = Thor::Shell::Color.new
16
- accounts_old = Dir.home + "/ayadn/accounts.db"
17
- unless File.exist?(accounts_old)
18
- puts "\n"
19
- @thor.say_status :error, "can't find the Ayadn 1.x accounts database", :red
20
- @thor.say_status :canceled, "migration canceled", :red
21
- puts "\n"
22
- exit
23
- end
24
- begin
25
- @accounts = Daybreak::DB.new(accounts_old)
26
- # just in case of a previous canceled migration
27
- # which could have left the accounts.db in place
28
- if @accounts.size == 1 || @accounts.size == 0
29
- @accounts.close
30
- File.delete(Dir.home + "/ayadn/accounts.db")
31
- @thor.say_status :delete, Dir.home + "/ayadn/accounts.db", :yellow
32
- @thor.say_status :stopped, "no more accounts to migrate", :green
33
- exit
34
- end
35
- @active_old = @accounts['ACTIVE']
36
- @home = @accounts[@active_old][:path]
37
- bookmarks_old = "#{@home}/db/bookmarks.db"
38
- aliases_old = "#{@home}/db/aliases.db"
39
- blacklist_old = "#{@home}/db/blacklist.db"
40
- users_old = "#{@home}/db/users.db"
41
- @pagination_old = "#{@home}/pagination/pagination.db"
42
- @index_old = "#{@home}/pagination/index.db"
43
-
44
- @config_path_old = "#{@home}/config/config.yml"
45
-
46
- @bookmarks = Daybreak::DB.new(bookmarks_old) if File.exist?(bookmarks_old)
47
- @aliases = Daybreak::DB.new(aliases_old) if File.exist?(aliases_old)
48
- @blacklist = Daybreak::DB.new(blacklist_old) if File.exist?(blacklist_old)
49
- @users = Daybreak::DB.new(users_old) if File.exist?(users_old)
50
- @pagination = Daybreak::DB.new(@pagination_old) if File.exist?(@pagination_old)
51
- @index = Daybreak::DB.new(@index_old) if File.exist?(@index_old)
52
-
53
- @sqlfile = "#{@home}/db/ayadn.sqlite"
54
- @sql = Amalgalite::Database.new(@sqlfile)
55
- rescue Exception => e
56
- puts "\n"
57
- @thor.say_status :error, "#{e}", :red
58
- @thor.say_status :stack, "#{caller}", :red
59
- @thor.say_status :canceled, "migration canceled", :red
60
- puts "\n"
61
- exit
62
- end
63
- end
64
-
65
- def all
66
- # DON'T MODIFY THE ORDER!
67
- puts banner()
68
- puts "\n"
69
- @thor.say_status :start, "migration", :yellow
70
- old_backup = "#{@home}/backup"
71
- if Dir.exist?(old_backup)
72
- if Dir.entries(old_backup).size > 2
73
- FileUtils.mv(Dir.glob("#{old_backup}/*"), "#{@home}/downloads")
74
- @thor.say_status :move, "files from 'backup' to 'downloads'", :green
75
- end
76
- Dir.rmdir(old_backup)
77
- @thor.say_status :delete, old_backup, :green
78
- end
79
- old_channels = "#{@home}/db/channels.db"
80
- if File.exist?(old_channels)
81
- @thor.say_status :delete, old_channels, :green
82
- File.delete(old_channels)
83
- end
84
- if File.exist?("#{@home}/db/ayadn_pinboard.db")
85
- @thor.say_status :move, "pinboard credentials", :green
86
- FileUtils.mv("#{@home}/db/ayadn_pinboard.db", "#{@home}/auth/pinboard.data")
87
- end
88
- bookmarks()
89
- aliases()
90
- blacklist()
91
- niceranks()
92
- users()
93
- pagination()
94
- index()
95
- accounts()
96
- config()
97
- @thor.say_status :done, "migration", :yellow
98
- puts "\n"
99
- if @still != false
100
- @thor.say_status :WARNING, "another user, @#{@still}, is still in the old database", :red
101
- @thor.say_status :PLEASE, "you should run `ayadn migrate` again right now", :yellow
102
- puts "\n"
103
- else
104
- @thor.say_status :ok, "ready to go!", :green
105
- @thor.say_status :thanks, "you can use Ayadn now", :cyan
106
- puts "\n"
107
- end
108
- end
109
-
110
- def bookmarks
111
- @sql.execute_batch <<-SQL
112
- CREATE TABLE Bookmarks (
113
- post_id INTEGER,
114
- bookmark TEXT
115
- );
116
- SQL
117
- @sql.reload_schema!
118
- if File.exist?("#{@home}/db/bookmarks.db")
119
- @thor.say_status :import, "Bookmarks database", :cyan
120
- @sql.transaction do |db_in_transaction|
121
- @bookmarks.each do |k,v|
122
- insert_data = {}
123
- insert_data[":k"] = k.to_i
124
- insert_data[":v"] = v.to_json.to_s
125
- db_in_transaction.prepare("INSERT INTO Bookmarks(post_id, bookmark) VALUES(:k, :v);") do |insert|
126
- insert.execute(insert_data)
127
- end
128
- end
129
- end
130
- @thor.say_status :done, "#{@bookmarks.size} objects", :green
131
- @bookmarks.close
132
- File.delete("#{@home}/db/bookmarks.db")
133
- @thor.say_status :delete, "#{@home}/db/bookmarks.db", :green
134
- end
135
- end
136
-
137
- def aliases
138
- @sql.execute_batch <<-SQL
139
- CREATE TABLE Aliases (
140
- channel_id INTEGER,
141
- alias VARCHAR(255)
142
- );
143
- SQL
144
- @sql.reload_schema!
145
- if File.exist?("#{@home}/db/aliases.db")
146
- @thor.say_status :import, "Aliases database", :cyan
147
- @sql.transaction do |db_in_transaction|
148
- @aliases.each do |k,v|
149
- insert_data = {}
150
- insert_data[":k"] = v.to_i
151
- insert_data[":v"] = k
152
- db_in_transaction.prepare("INSERT INTO Aliases(channel_id, alias) VALUES(:k, :v);") do |insert|
153
- insert.execute(insert_data)
154
- end
155
- end
156
- end
157
- @thor.say_status :done, "#{@aliases.size} objects", :green
158
- @aliases.close
159
- File.delete("#{@home}/db/aliases.db")
160
- @thor.say_status :delete, "#{@home}/db/aliases.db", :green
161
- end
162
- end
163
-
164
- def blacklist
165
- @sql.execute_batch <<-SQL
166
- CREATE TABLE Blacklist (
167
- type VARCHAR(255),
168
- content TEXT
169
- );
170
- SQL
171
- @sql.reload_schema!
172
- if File.exist?("#{@home}/db/blacklist.db")
173
- @thor.say_status :import, "Blacklist database", :cyan
174
- @sql.transaction do |db_in_transaction|
175
- @blacklist.each do |k,v|
176
- insert_data = {}
177
- ks = k.dup.to_s
178
- ks[0] = "" if ks[0] == "-"
179
- ks[0] = "" if ks[0] == "@"
180
- insert_data[":k"] = v.to_s
181
- insert_data[":v"] = ks
182
- db_in_transaction.prepare("INSERT INTO Blacklist(type, content) VALUES(:k, :v);") do |insert|
183
- insert.execute(insert_data)
184
- end
185
- end
186
- end
187
- @thor.say_status :done, "#{@blacklist.size} objects", :green
188
- @blacklist.close
189
- File.delete("#{@home}/db/blacklist.db")
190
- @thor.say_status :delete, "#{@home}/db/blacklist.db", :green
191
- end
192
- end
193
-
194
- def niceranks
195
- if File.exist?("#{@home}/db/nicerank.db")
196
- File.delete("#{@home}/db/nicerank.db")
197
- @thor.say_status :delete, "#{@home}/db/nicerank.db", :green
198
- end
199
- end
200
-
201
- def users
202
- @sql.execute_batch <<-SQL
203
- CREATE TABLE Users (
204
- user_id INTEGER,
205
- username VARCHAR(20),
206
- name TEXT
207
- );
208
- SQL
209
- @sql.reload_schema!
210
- if File.exist?("#{@home}/db/users.db")
211
- @thor.say_status :import, "Users database", :cyan
212
- @sql.transaction do |db_in_transaction|
213
- @users.each do |k,v|
214
- insert_data = {}
215
- insert_data[":id"] = k.to_i
216
- insert_data[":username"] = v.keys[0]
217
- insert_data[":name"] = v.values[0]
218
- db_in_transaction.prepare("INSERT INTO Users(user_id, username, name) VALUES(:id, :username, :name);") do |insert|
219
- insert.execute(insert_data)
220
- end
221
- end
222
- end
223
- @thor.say_status :done, "#{@users.size} objects", :green
224
- @users.close
225
- File.delete("#{@home}/db/users.db")
226
- @thor.say_status :delete, "#{@home}/db/users.db", :green
227
- end
228
- end
229
-
230
- def pagination
231
- @sql.execute_batch <<-SQL
232
- CREATE TABLE Pagination (
233
- name TEXT,
234
- post_id INTEGER
235
- );
236
- SQL
237
- @sql.reload_schema!
238
- if File.exist?(@pagination_old)
239
- @thor.say_status :import, "Pagination database", :cyan
240
- @sql.transaction do |db_in_transaction|
241
- @pagination.each do |k,v|
242
- insert_data = {}
243
- insert_data[":post_id"] = v.to_i
244
- insert_data[":name"] = k.to_s
245
- db_in_transaction.prepare("INSERT INTO Pagination(name, post_id) VALUES(:name, :post_id);") do |insert|
246
- insert.execute(insert_data)
247
- end
248
- end
249
- end
250
- @thor.say_status :done, "#{@pagination.size} objects", :green
251
- @pagination.close
252
- File.delete(@pagination_old)
253
- @thor.say_status :delete, @pagination_old, :green
254
- end
255
- end
256
-
257
- def index
258
- @sql.execute_batch <<-SQL
259
- CREATE TABLE TLIndex (
260
- count INTEGER,
261
- post_id INTEGER,
262
- content TEXT
263
- );
264
- SQL
265
- @sql.reload_schema!
266
- if File.exist?(@index_old)
267
- @thor.say_status :import, "Index database", :cyan
268
- @sql.transaction do |db_in_transaction|
269
- @index.each do |k,v|
270
- insert_data = {}
271
- insert_data[":post_id"] = v[:id]
272
- insert_data[":count"] = v[:count]
273
- insert_data[":content"] = v.to_json.to_s
274
- db_in_transaction.prepare("INSERT INTO TLIndex(count, post_id, content) VALUES(:count, :post_id, :content);") do |insert|
275
- insert.execute(insert_data)
276
- end
277
- end
278
- end
279
- @thor.say_status :done, "#{@index.size} objects", :green
280
- @index.close
281
- File.delete(@index_old)
282
- @thor.say_status :delete, @index_old, :green
283
- Dir.rmdir("#{@home}/pagination")
284
- @thor.say_status :delete, "#{@home}/pagination", :green
285
- end
286
- end
287
-
288
- def accounts
289
- @thor.say_status :create, Dir.home + "/ayadn/accounts.sqlite", :blue
290
- sql = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
291
- @thor.say_status :import, "account informations", :cyan
292
- if sql.schema.tables.empty?
293
- sql.execute_batch <<-SQL
294
- CREATE TABLE Accounts (
295
- username VARCHAR(20),
296
- user_id INTEGER,
297
- handle VARCHAR(21),
298
- account_path TEXT,
299
- active INTEGER,
300
- token TEXT
301
- );
302
- SQL
303
- sql.reload_schema!
304
- end
305
- active, active_account = ""
306
- @accounts.each do |k,v|
307
- if k == "ACTIVE"
308
- active_account = v
309
- next
310
- end
311
- end
312
- actives = sql.execute("SELECT user_id FROM Accounts WHERE active=1")
313
- sql.execute("UPDATE Accounts SET active=0") unless actives.empty?
314
- sql.transaction do |db_in_transaction|
315
- active = [@accounts[active_account]]
316
- @thor.say_status :delete, "old @#{active_account} account", :green
317
- @accounts.delete(active_account)
318
- if @accounts.size == 1 # only the 'ACTIVE' label, so it's like 0
319
- @accounts.close
320
- File.delete(Dir.home + "/ayadn/accounts.db")
321
- @thor.say_status :delete, Dir.home + "/ayadn/accounts.db", :green
322
- else
323
- @accounts.each do |k,v|
324
- next if k == "ACTIVE"
325
- @accounts['ACTIVE'] = v[:username]
326
- @still = v[:username]
327
- break
328
- end
329
- @accounts.close
330
- end
331
- active.each do |obj|
332
- insert_data = {}
333
- insert_data[":username"] = obj[:username]
334
- insert_data[":user_id"] = obj[:id].to_i
335
- insert_data[":handle"] = obj[:handle]
336
- insert_data[":account_path"] = obj[:path]
337
- insert_data[":active"] = 1
338
- template = Dir.home + "/ayadn/#{obj[:username]}/auth/token"
339
- insert_data[":token"] = File.read(template)
340
- db_in_transaction.prepare("INSERT INTO Accounts(username, user_id, handle, account_path, active, token) VALUES(:username, :user_id, :handle, :account_path, :active, :token);") do |insert|
341
- insert.execute(insert_data)
342
- end
343
- end
344
- end
345
- @thor.say_status :imported, "@#{active_account} account", :green
346
- end
347
-
348
- def config
349
- @thor.say_status :load, "config file", :blue
350
- old_conf = YAML.load(File.read(@config_path_old))
351
- conf = Settings.defaults
352
- @thor.say_status :convert, "settings", :cyan
353
- conf[:timeline][:source] = old_conf[:timeline][:show_source] || true
354
- conf[:timeline][:symbols] = old_conf[:timeline][:show_symbols] || true
355
- conf[:timeline][:name] = old_conf[:timeline][:show_real_name] || true
356
- conf[:timeline][:date] = old_conf[:timeline][:show_date] || true
357
- conf[:timeline][:debug] = old_conf[:timeline][:show_debug] || false
358
- conf[:timeline][:compact] = old_conf[:timeline][:compact] || false
359
- if old_conf[:timeline][:directed] == 1 || old_conf[:timeline][:directed] == true
360
- conf[:timeline][:directed] = true
361
- else
362
- conf[:timeline][:directed] = false
363
- end
364
- if old_conf[:marker].nil?
365
- conf[:marker] = { messages: true }
366
- else
367
- conf[:marker][:messages] = old_conf[:marker][:update_messages] || true
368
- end
369
- conf[:backup][:posts] = old_conf[:backup][:auto_save_sent_posts] || false
370
- conf[:backup][:messages] = old_conf[:backup][:auto_save_sent_messages] || false
371
- conf[:backup][:lists] = old_conf[:backup][:auto_save_lists] || false
372
- conf[:colors][:id] = old_conf[:colors][:id] || :blue
373
- conf[:colors][:index] = old_conf[:colors][:index] || :red
374
- conf[:colors][:username] = old_conf[:colors][:username] || :green
375
- conf[:colors][:name] = old_conf[:colors][:name] || :magenta
376
- conf[:colors][:debug] = old_conf[:colors][:debug] || :red
377
- conf[:colors][:date] = old_conf[:colors][:date] || :cyan
378
- conf[:colors][:link] = old_conf[:colors][:link] || :yellow
379
- conf[:colors][:dots] = old_conf[:colors][:dots] || :blue
380
- conf[:colors][:hashtags] = old_conf[:colors][:hashtags] || :cyan
381
- conf[:colors][:debug] = old_conf[:colors][:debug] || :red
382
- conf[:colors][:mentions] = old_conf[:colors][:mentions] || :red
383
- conf[:colors][:source] = old_conf[:colors][:source] || :cyan
384
- conf[:colors][:symbols] = old_conf[:colors][:symbols] || :green
385
- conf[:colors][:unread] = old_conf[:colors][:unread] || :cyan
386
- conf[:formats][:list] = old_conf[:formats][:list] || {}
387
- conf[:formats][:list][:reverse] = old_conf[:formats][:list][:reverse] || true
388
- conf[:formats][:table] = old_conf[:formats][:table] || {}
389
- conf[:formats][:table][:width] = old_conf[:formats][:table][:width] || 75
390
- conf[:formats][:table][:borders] = old_conf[:formats][:table][:borders] || true
391
- conf[:scroll][:spinner] = old_conf[:timeline][:show_spinner] || true
392
- conf[:scroll][:timer] = old_conf[:scroll][:timer] || 3
393
- conf[:movie][:hashtag] = old_conf[:movie][:hashtag] || 'nowwatching'
394
- conf[:tvshow][:hashtag] = old_conf[:tvshow][:hashtag] || 'nowwatching'
395
- conf[:channels][:links] = old_conf[:timeline][:show_channel_oembed] || true
396
- conf[:nicerank][:threshold] = old_conf[:nicerank][:threshold] || 2.1
397
- conf[:nicerank][:filter] = old_conf[:nicerank][:filter] || true
398
- conf[:counts][:defaults] = old_conf[:counts][:defaults] || 50
399
- conf[:counts][:unified] = old_conf[:counts][:unified] || 50
400
- conf[:counts][:global] = old_conf[:counts][:global] || 50
401
- conf[:counts][:checkins] = old_conf[:counts][:checkins] || 50
402
- conf[:counts][:conversations] = old_conf[:counts][:conversations] || 50
403
- conf[:counts][:photos] = old_conf[:counts][:photos] || 50
404
- conf[:counts][:trending] = old_conf[:counts][:trending] || 50
405
- conf[:counts][:mentions] = old_conf[:counts][:mentions] || 50
406
- conf[:counts][:convo] = old_conf[:counts][:convo] || 50
407
- conf[:counts][:posts] = old_conf[:counts][:posts] || 50
408
- conf[:counts][:messages] = old_conf[:counts][:messages] || 50
409
- conf[:counts][:search] = old_conf[:counts][:search] || 200
410
- conf[:counts][:whoreposted] = old_conf[:counts][:whoreposted] || 20
411
- conf[:counts][:whostarred] = old_conf[:counts][:whostarred] || 20
412
- conf[:counts][:whatstarred] = old_conf[:counts][:whatstarred] || 100
413
- conf[:counts][:files] = old_conf[:counts][:files] || 50
414
- @thor.say_status :save, "config file", :green
415
- File.write(@config_path_old, conf.to_yaml)
416
- end
417
-
418
- def banner
419
- <<-BANNER
420
-
421
- \t\t _____ __ __ _____ ____ _____
422
- \t\t | _ | | | _ | \\| | |
423
- \t\t | |_ _| | | | | | |
424
- \t\t |__|__| |_| |__|__|____/|_|___|
425
-
426
-
427
- BANNER
428
- end
429
-
430
- end
431
- end