forked from hmason/gitmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hilary Mason
committed
Dec 17, 2010
0 parents
commit 899b680
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
""" | ||
delicious_import.py | ||
Created by Hilary Mason on 2010-11-28. | ||
Copyright (c) 2010 Hilary Mason. All rights reserved. | ||
""" | ||
|
||
import sys, os | ||
import urllib | ||
from xml.dom import minidom | ||
from optparse import OptionParser | ||
|
||
from gitmark import gitMark | ||
|
||
class delicious_import(object): | ||
def __init__(self, username, password=''): | ||
# API URL: https://user:[email protected]/v1/posts/all | ||
url = "https://%s:%[email protected]/v1/posts/all" % (username, password) | ||
h = urllib.urlopen(url) | ||
content = h.read() | ||
h.close() | ||
|
||
x = minidom.parseString(content) | ||
|
||
# sample post: <post href="http://www.pixelbeat.org/cmdline.html" hash="e3ac1d1e4403d077ee7e65f62a55c406" description="Linux Commands - A practical reference" tag="linux tutorial reference" time="2010-11-29T01:07:35Z" extended="" meta="c79362665abb0303d577b6b9aa341599" /> | ||
for post in x.getElementsByTagName("post"): | ||
url = post.getAttribute('href') | ||
desc = post.getAttribute('description') | ||
tags = ",".join([t for t in post.getAttribute('tag').split()]) | ||
timestamp = post.getAttribute('time') | ||
|
||
options = {} | ||
options['tags'] = tags | ||
options['push'] = False | ||
options['msg'] = desc | ||
|
||
args = [url] | ||
|
||
g = gitMark(options, args) | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
(username, password) = sys.argv[1:] | ||
except ValueError: | ||
print "Usage: python delicious_import.py username password" | ||
|
||
d = delicious_import(username, password) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
""" | ||
gitmark.py | ||
Created by Hilary Mason on 2010-09-24. | ||
Copyright (c) 2010 Hilary Mason. All rights reserved. | ||
""" | ||
|
||
import sys, os | ||
import urllib | ||
import re | ||
import hashlib | ||
import csv | ||
import subprocess | ||
from optparse import OptionParser | ||
|
||
from settings import * | ||
|
||
class gitMark(object): | ||
|
||
def __init__(self, options, args): | ||
modified = [] # track files we need to add - a hack, because it will add files that are already tracked by git | ||
|
||
url = args[0].strip('/') | ||
content = self.getContent(url) | ||
title = self.parseTitle(content) | ||
content_filename = self.generateHash(url) | ||
|
||
modified.append(self.saveContent(content_filename, content)) | ||
tags = ['all'] | ||
tags.extend(options['tags'].split(',')) | ||
for tag in tags: | ||
t = tag.strip() | ||
modified.append(self.saveTagData(t, url, title, content_filename)) | ||
|
||
self.gitAdd(modified) | ||
|
||
commit_msg = options['msg'] | ||
if not commit_msg: | ||
commit_msg = 'adding %s' % url | ||
|
||
self.gitCommit(commit_msg) | ||
|
||
if options['push']: | ||
self.gitPush() | ||
|
||
def gitAdd(self, files): | ||
for f in files: | ||
cmd = 'git add %s' % f | ||
pipe = subprocess.Popen(cmd, shell=True) | ||
pipe.wait() | ||
|
||
def gitCommit(self, msg): | ||
pipe = subprocess.Popen("git commit -m '%s'" % msg, shell=True) | ||
pipe.wait() | ||
|
||
def gitPush(self): | ||
pipe = subprocess.Popen("git push origin master", shell=True) | ||
pipe.wait() | ||
|
||
def saveContent(self, filename, content): | ||
f = open('%s%s' % (CONTENT_PATH, filename), 'w') | ||
f.write(content) | ||
f.close() | ||
return '%s%s' % (CONTENT_PATH, filename) | ||
|
||
def saveTagData(self, tag, url, title, content_filename): | ||
tag_writer = csv.writer(open('%s%s' % (TAG_PATH, tag), 'a')) | ||
tag_writer.writerow([url, title, content_filename]) | ||
return '%s%s' % (TAG_PATH, tag) | ||
|
||
def parseTitle(self, content): | ||
re_htmltitle = re.compile(".*<title>(.*)</title>.*") | ||
t = re_htmltitle.search(content) | ||
try: | ||
title = t.group(1) | ||
except AttributeError: | ||
title = '[No Title]' | ||
|
||
return title | ||
|
||
def generateHash(self, text): | ||
m = hashlib.md5() | ||
m.update(text) | ||
return m.hexdigest() | ||
|
||
def getContent(self, url): | ||
h = urllib.urlopen(url) | ||
content = h.read() | ||
h.close() | ||
return content | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
parser = OptionParser("usage: %prog [options]") | ||
parser.add_option("-p", "--push", dest="push", action="store_false", default=True, help="don't push to origin.") | ||
parser.add_option("-t", "--tags", dest="tags", action="store", default='notag', help="comma seperated list of tags") | ||
parser.add_option("-m", "--message", dest="msg", action="store", default=None, help="specify a commit message (default is 'adding [url]')") | ||
(options, args) = parser.parse_args() | ||
|
||
opts = {'push': options.push, 'tags': options.tags, 'msg': options.msg} | ||
|
||
g = gitMark(opts, args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
TAG_PATH = 'tags/' | ||
|
||
CONTENT_PATH = 'content/' |