mispy-archive / twitter_ebooks

Better twitterbots for all your friends~
MIT License
972 stars 140 forks source link

JSON exception when archiving #124

Open Chenny2 opened 8 years ago

Chenny2 commented 8 years ago

Sometimes I get errors like that when archiving.

C:/Ruby22/lib/ruby/gems/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `initialize': A JSON text must at least contain two octets! (JSON::ParserError)
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `new'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/twitter_ebooks-3.1.6/lib/twitter_ebooks/archive.rb:54:in `initialize'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/twitter_ebooks-3.1.6/bin/ebooks:227:in `new'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/twitter_ebooks-3.1.6/bin/ebooks:227:in `archive'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/twitter_ebooks-3.1.6/bin/ebooks:411:in `command'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/twitter_ebooks-3.1.6/bin/ebooks:429:in `<top (required)>'
        from C:/Ruby22/bin/ebooks:23:in `load'
        from C:/Ruby22/bin/ebooks:23:in `<main>'
atlgeek007 commented 8 years ago

Same issue here :-(

btamayo commented 7 years ago

Seems like it may happen when your archive file exists but is empty (technically also if malformed):

archive.rb's initialize: line ~50

@filetext = File.read(@path, :encoding => 'utf-8')
@tweets = JSON.parse(@filetext, symbolize_names: true)
log "Currently #{@tweets.length} tweets for #{@username}"

A quick fix would be to just put that in an if that checks that @filetext has length > 0:

@filetext = File.read(@path, :encoding => 'utf-8')
if @filetext && @filetext.length > 0
    @tweets = JSON.parse(@filetext, symbolize_names: true)
    log "Currently #{@tweets.length} tweets for #{@username}"
end