Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tumblr_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import sys
import threading
import time
import unicodedata
import urllib
import urllib2
import urlparse
Expand Down Expand Up @@ -103,6 +104,8 @@ def test_jpg(h, f):
# bb-tumblr-backup API key
API_KEY = '8YUsKJvcJxo2MDwmWMDiXZGuMuIbeCwuQGP5ZHSEA4jBJPMnJT'

FILENAME_LIMIT = 250

# ensure the right date/time format
try:
locale.setlocale(locale.LC_TIME, '')
Expand Down Expand Up @@ -292,6 +295,18 @@ def get_style():
return


def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
value = unicode(re.sub('[-\s]+', '-', value))
# TODO: figure out what to do if there's a collision here...
return value[:FILENAME_LIMIT]


class Index:

def __init__(self, blog, body_class='index'):
Expand Down Expand Up @@ -414,7 +429,8 @@ def save_tag_index(self):
mkdir(path_to(tag_index_dir))
self.fixup_media_links()
tag_index = [self.blog.header('Tag index', 'tag-index', self.blog.title, True), '<ul>']
for tag, index in sorted(self.tags.items(), key=lambda kv: kv[1].name):
for index in sorted(self.tags.values(), key=lambda v: v.name):
tag = slugify(index.name)
index.save_index(tag_index_dir + os.sep + tag,
u"Tag ‛%s’" % index.name
)
Expand Down