Permalink
Browse files

serious refactoring going on here

  • Loading branch information...
1 parent 383bf38 commit cb6cb3f760e4cf1bc6ddc9752603f44096368c83 @ttscoff committed Aug 7, 2012
View
@@ -1,38 +1,34 @@
require 'yaml'
+
class ConfigTools
def initialize(options = {})
@config_file = options['config_file'] || File.expand_path(File.dirname(__FILE__)+'/../slogger_config')
end
attr_accessor :config_file
def load_config
- File.open(@config_file) { |yf| YAML::load(yf) }
+ File.open(@config_file, 'r') { |yf| JSON.parse(yf) }
end
def dump_config (config)
- File.open(@config_file, 'w') { |yf| YAML::dump(config, yf) }
+ File.open(@config_file, 'w') { |yf| yf.puts(config.to_json) }
+ end
+
+ def default_config
+ config = {
+ 'storage' => 'icloud'
+ }
+ config
end
def config_exists?
if !File.exists?(@config_file)
- dump_config( {
- 'lastfm_user' => '',
- 'rss_feeds' => [],
- 'markdownify_rss_posts' => false,
- 'star_rss_posts' => false,
- 'twitter_users' => [],
- 'save_images' => true,
- 'droplr_domain' => 'd.pr',
- 'gist_user' => '',
- 'rss_tags' => '@social @blogging',
- 'lastfm_tags' => '@social @music',
- 'twitter_tags' => '@social @twitter',
- 'gist_tags' => '@social @coding',
- 'storage' => 'icloud'
- } )
- puts "Please update the configuration file at #{@config_file}."
- Process.exit(-1)
+ dump_config( default_config )
+ return false
+ # puts "Please update the configuration file at #{@config_file}."
+ # Process.exit(-1)
+ else
+ return true
end
- return true
end
end
@@ -1,72 +1,35 @@
-class DayOne < SocialLogger
- def initialize(options = {})
- options['storage'] ||= 'icloud'
- if options['storage'].downcase == 'icloud'
- dayonedir = %x{ls ~/Library/Mobile\\ Documents/|grep dayoneapp}.strip
- full_path = File.expand_path("~/Library/Mobile\ Documents/#{dayonedir}/Documents/Journal_dayone/")
- if File.exists?(full_path)
- @dayonepath = full_path
- else
- raise "Failed to find iCloud storage path"
- Process.exit(-1)
- end
- elsif File.exists?(File.expand_path(options['storage']))
- @dayonepath = File.expand_path(options['storage'])
- else
- raise "Path not specified or doesn't exist: #{options['storage']}"
- Process.exit(-1)
- end
-
- @template = ERB.new <<-XMLTEMPLATE
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
-<plist version="1.0">
-<dict>
- <key>Creation Date</key>
- <date><%= datestamp %></date>
- <key>Entry Text</key>
- <string><%= entry %></string>
- <key>Starred</key>
- <<%= starred %>/>
- <key>UUID</key>
- <string><%= uuid %></string>
-</dict>
-</plist>
-XMLTEMPLATE
- end
- attr_accessor :dayonepath
-
+class DayOne < Slogger
def to_dayone(options = {})
content = options['content'] || ''
uuid = options['uuid'] || %x{uuidgen}.gsub(/-/,'').strip
starred = options['starred'] || false
datestamp = options['datestamp'] || Time.now.utc.iso8601
- if @debug || options['debug']
- return true
- end
-
# entry = CGI.escapeHTML(content.unpack('C*').pack('U*').gsub(/[^[:punct:]\w\s]+/,' ')) unless content.nil?
entry = CGI.escapeHTML(content) unless content.nil?
+ @dayonepath = storage_path
+ @log.info("Saving entry to #{@dayonepath}/entries/#{uuid}.doentry")
fh = File.new(File.expand_path(@dayonepath+'/entries/'+uuid+".doentry"),'w+')
fh.puts @template.result(binding)
fh.close
return true
end
def save_image(imageurl,uuid)
+ @dayonepath = Slogger.new.storage_path
source = imageurl.gsub(/^https/,'http')
match = source.match(/(\..{3,4})($|\?|%22)/)
unless match.nil?
ext = match[1]
else
- Logger.new(STDERR).warn("Attempted to save #{imageurl} but extension could not be determined")
+ @log.warn("Attempted to save #{imageurl} but extension could not be determined")
ext = '.jpg'
end
target = @dayonepath + '/photos/'+uuid+ext
begin
Net::HTTP.get_response(URI.parse(imageurl)) do |http|
data = http.body
+ @log.info("Retrieving image: #{imageurl} to #{target}")
open( File.expand_path(target), "wb" ) { |file| file.write(data) }
end
return self.process_image(target)
@@ -81,6 +44,7 @@ def process_image(image)
match = orig.match(/(\..{3,4})$/)
return false if match.nil?
ext = match[1]
+ @log.info("Resizing image #{orig}")
%x{sips -Z 800 "#{orig}"}
unless ext =~ /\.jpg$/
case ext
@@ -89,10 +53,16 @@ def process_image(image)
FileUtils.mv(orig,target)
return target
when /\.(png|gif)$/
- target = orig.gsub(/#{ext}$/,'.jpg')
- %x{/usr/local/bin/convert "#{orig}" "#{target}"}
- File.delete(orig)
- return target
+ if File.exists?('/usr/local/bin/convert')
+ target = orig.gsub(/#{ext}$/,'.jpg')
+ @log.info("Converting #{orig} to JPEG")
+ %x{/usr/local/bin/convert "#{orig}" "#{target}"}
+ File.delete(orig)
+ return target
+ else
+ @log.warn("Image could not be converted to JPEG format and may not show up in Day One. Please install ImageMagick.")
+ return orig
+ end
else
return orig
end
@@ -106,7 +76,7 @@ def store_single_photo(file, options = {}, copy = false)
options['uuid'] ||= %x{uuidgen}.gsub(/-/,'').strip
options['starred'] ||= false
options['datestamp'] ||= Time.now.utc.iso8601
- target_path = File.expand_path(@dayonepath+"/photos/"+options['uuid']+".jpg")
+ target_path = File.expand_path(Slogger.new.storage_path+"/photos/"+options['uuid']+".jpg")
if copy
FileUtils.copy(File.expand_path(file),target_path)
View
@@ -1,48 +0,0 @@
-=begin
-Checks last.fm feed once a day for that day's posts. You can specify multiple
-feeds to parse in the feeds array.
-=end
-class LastFMLogger < SocialLogger
-
- def initialize(config = {})
- if config['user']
- @user = config['user']
- else
- return false
- end
- @tags ||= ''
- @tags = "\n\n#{@tags}\n" unless @tags == ''
- @debug = config['debug'] || false
- @feeds = [{'title'=>"## Listening To", 'feed' => "http://ws.audioscrobbler.com/2.0/user/#{@user}/recenttracks.rss"},{'title'=>"## Loved Tracks", 'feed' => "http://ws.audioscrobbler.com/2.0/user/#{@user}/lovedtracks.rss"}]
- @storage = config['storage'] || 'icloud'
- @sl = DayOne.new({ 'storage' => @storage })
- @today = Time.now - (60 * 60 * 24)
- end
- attr_accessor :user, :feeds, :debug
-
- def log_lastfm
- @feeds.each do |rss_feed|
- entrytext = ''
- rss_content = ""
- begin
- feed_url = URI.parse(rss_feed['feed'])
- feed_url.open do |f|
- rss_content = f.read
- end
- rescue Exception => e
- raise "ERROR fetching feed #{rss_feed['title']}"
- p e
- end
- content = ''
- rss = RSS::Parser.parse(rss_content, false)
- rss.items.each { |item|
- break if Time.parse(item.pubDate.to_s) < @today
- content += "* [#{item.title}](#{item.link})\n"
- }
- if content != ''
- entrytext = "#{rss_feed['title']} for #{@today.strftime('%m-%d-%Y')}\n\n" + content + "\n#{@tags}"
- end
- @sl.to_dayone({'content' => entrytext}) unless entrytext == ''
- end
- end
-end
View
@@ -1,24 +1,7 @@
class SocialLogger
- require 'open-uri'
- require 'net/http'
- require 'net/https'
- require 'time'
- require 'cgi'
- require 'rss'
- require 'erb'
- require 'logger'
- require RUBY_VERSION < "1.9" ? 'ftools' : 'fileutils'
- root = File.dirname(__FILE__)+'/'
- require root + 'create.rb'
- require root + 'rsslogger.rb'
- require root + 'twitterlogger.rb'
- require root + 'lastfmlogger.rb'
- require root + 'redirect.rb'
-
def initialize(options = {})
@debug = options['debug'] || false
@config = options['config'] || {}
end
attr_accessor :debug, :config
-
end
@@ -0,0 +1,55 @@
+=begin
+Plugin: FourSquare Logger
+Description: Checks Foursquare feed once a day for that day's posts.
+Author: [Jeff Mueller](https://github.com/jeffmueller)
+Configuration:
+ foursquare_feed: "https://feeds.foursquare.com/history/yourfoursquarehistory.rss"
+ foursquare_tags: "@social @checkins"
+Notes:
+ Find your feed at <https://foursquare.com/feeds/> (in RSS option)
+=end
+
+
+
+class FoursquareLogger < Slogger
+ default_config = {
+ 'foursquare_feed' => "",
+ 'foursquare_tags' => "@social @checkins"
+ }
+ $slog.register_plugin({ 'class' => 'FoursquareLogger', 'config' => default_config })
+ def do_log
+ @log.info("Getting FourSquare checkins")
+ if @config['foursquare_feed'].nil? || @config['foursquare_feed'] == ''
+ @log.warn("FourSquare feed has not been configured or the feed is invalid, please edit your slogger_config file.")
+ return
+ end
+ @feed = @config['foursquare_feed']
+
+ @config['foursquare_tags'] ||= ''
+ @tags = "\n\n#{@config['foursquare_tags']}\n" unless @config['foursquare_tags'] == ''
+ @debug = @config['debug'] || false
+ @today = Time.now - (60 * 60 * 24)
+
+ entrytext = ''
+ rss_content = ""
+ begin
+ feed_url = URI.parse(@feed)
+ feed_url.open do |f|
+ rss_content = f.read
+ end
+ rescue Exception => e
+ raise "ERROR fetching Foursquare feed"
+ p e
+ end
+ content = ''
+ rss = RSS::Parser.parse(rss_content, false)
+ rss.items.each { |item|
+ break if Time.parse(item.pubDate.to_s) < @today
+ content += "* [#{item.title}](#{item.link})\n"
+ }
+ if content != ''
+ entrytext = "## Foursquare Checkins for #{@today.strftime('%m-%d-%Y')}\n\n" + content + "\n#{@tags}"
+ end
+ DayOne.new.to_dayone({'content' => entrytext}) unless entrytext == ''
+ end
+end
@@ -1,29 +1,26 @@
-# NOTE: Requires json gem
-class GistLogger < SocialLogger
- def initialize(options = {})
- if !options['user'].empty?
- options.each_pair do |att_name, att_val|
- instance_variable_set("@#{att_name}", att_val)
- end
- else
- raise "No Gist user configured"
- return false
- end
+=begin
+Plugin: Gist Logger
+Description: Logs daily Gists for the specified user
+Author: [Brett Terpstra](http://brettterpstra.com)
+Configuration:
+ gist_user: githubuser
+ gist_tags: "@social @coding"
+Notes:
- @tags ||= ''
- @tags = "\n\n#{@tags}\n" unless @tags == ''
- @storage ||= 'icloud'
- end
- attr_accessor :user
-
- def e_sh(str)
- str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
- end
+=end
+# NOTE: Requires json gem
+config = {
+ 'gist_user' => '',
+ 'gist_tags' => '@social @coding',
+}
+$slog.register_plugin({ 'class' => 'RSSLogger', 'config' => config })
- def log_gists
+class GistLogger < Slogger
+ def do_log
+ @log.info("Logging gists for #{@config['gist_user']}")
begin
- url = URI.parse "https://api.github.com/users/#{@user}/gists"
+ url = URI.parse "https://api.github.com/users/#{@config['gist_user']}/gists"
http = Net::HTTP.new url.host, url.port
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -60,8 +57,8 @@ def log_gists
}
return false if output.strip == ""
- entry = "## Gists for #{Time.now.strftime("%m-%d-%Y")}:\n\n#{output}#{@tags}"
- DayOne.new({ 'storage' => @storage }).to_dayone({ 'content' => entry })
+ entry = "## Gists for #{Time.now.strftime("%m-%d-%Y")}:\n\n#{output}#{@config['gist_tags']}"
+ DayOne.new.to_dayone({ 'content' => entry })
end
end
Oops, something went wrong.

0 comments on commit cb6cb3f

Please sign in to comment.