Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Last active December 26, 2021 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyagawa/4560510 to your computer and use it in GitHub Desktop.
Save miyagawa/4560510 to your computer and use it in GitHub Desktop.
import tweets from tweets.zip into Day One app. May or may not work with your dayone preference...
#!/usr/bin/env ruby
require 'json'
require 'open3'
require 'date'
class Importer
def initialize(*args)
@js_files = *args
end
def import
@js_files.each do |file|
puts "Loading file #{file}\n"
content = File.open(file).read
content.sub! /^Grailbird.* = /, ''
tweets = JSON.load(content).reverse
tweets.each do |data|
tweet = Tweet.new(data)
handle_tweet tweet
end
end
end
def handle_tweet(tweet)
unless tweet.reply? or tweet.retweet?
unless imported?(tweet)
import_tweet(tweet)
end
end
end
def imported?(tweet)
load_cache! unless @tweets_cache
@tweets_cache.key? tweet.url
end
def load_cache!
@tweets_cache = {}
Dir["#{ENV['HOME']}/Dropbox/Journal.dayone/entries/*.doentry"].each do |file|
data = File.open(file).read
if %r[(https?://twitter.com/\w+/status/\d+)</string>] === data
@tweets_cache[$1] = 1
end
end
end
def import_tweet(tweet)
Open3.popen3('dayone', "-d=#{tweet.localtime}", 'new') do |stdin, stdout, stderr|
stdin.write(tweet.text + " " + tweet.url)
stdin.close_write
puts stdout.read
end
end
end
class Done < Exception; end
class Tweet
def initialize(data)
@data = data
end
def id
@data['id']
end
def id_str
@data['id_str']
end
def reply?
/^@/.match @data['text']
end
def retweet?
/^RT @/.match @data['text']
end
def url
"http://twitter.com/#{@data['user']['screen_name']}/status/#{id_str}"
end
def text
text = @data['text']
if @data.key?('entities') and @data['entities'].key?('urls')
@data['entities']['urls'].each do |entity|
text.gsub! /#{entity['url']}/, entity['expanded_url']
end
end
text
end
def localtime
DateTime.parse(@data['created_at']).to_time.to_s
end
end
Importer.new(*ARGV).import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment