Skip to content

Instantly share code, notes, and snippets.

@jmjeong
Created October 27, 2014 03:25
Show Gist options
  • Save jmjeong/aaf54290bd4604c07036 to your computer and use it in GitHub Desktop.
Save jmjeong/aaf54290bd4604c07036 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Jaemok Jeong, 2014/10/26
import feedparser
import re
import urllib
import os
import subprocess
import json
import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def _create(path):
if not os.path.isdir(path):
os.mkdir(path)
if not os.access(path, os.W_OK):
raise IOError('No write access: %s' % path)
return path
def alfred_work(volatile):
path = {
True: '~/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/com.jmjeong.alfredv2.btm',
False: '~/Library/Application Support/Alfred 2/Workflow Data/com.jmjeong.alfredv2.btm'
}[bool(volatile)]
return _create(os.path.expanduser(path))
def config_data():
try:
config = json.loads(open(os.path.join(alfred_work(False), 'config.json')).read())
except:
config = {}
return config
def rss_reload():
imageregex = re.compile(r"img.*src=\"(.*?)\"")
config = config_data()
d = feedparser.parse('http://macnews.tistory.com/rss')
items = []
for e in d.entries:
try:
imageurl = imageregex.search(e.description)
if imageurl:
url = imageurl.group(1)
filepath = os.path.join(alfred_work(True), os.path.split(url)[1])
if not os.path.exists(filepath):
urllib.urlretrieve(url, filepath)
cmd = "sips -z 72 72 '%s'" % filepath
subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
imageurl = filepath
else:
imageurl = u"icon.png"
except:
imageurl = u"icon.png"
items.append({'title':e.title,'published':e.published,'link':e.link,'image':imageurl})
filename = os.path.join(alfred_work(True), 'rss-cache.json')
with open(filename, 'w+') as myFile:
myFile.write(json.dumps(items))
config['last_updated'] = int(time.time())
with open(os.path.join(alfred_work(False), 'config.json'), 'w+') as myFile:
myFile.write(json.dumps(config))
print "Reloading BackToTheMac completed..."
if __name__ == '__main__':
rss_reload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment