Skip to content

Instantly share code, notes, and snippets.

@imebeh
Created November 1, 2016 11:27
Show Gist options
  • Save imebeh/303058ee805311c1bdaa33cc6ace694b to your computer and use it in GitHub Desktop.
Save imebeh/303058ee805311c1bdaa33cc6ace694b to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#coding: utf-8
import os
import sys
import urllib2
encod = sys.stdin.encoding
is_for_local = False
html = u'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta content="True" name="HandheldFriendly">
<meta content="width=device-width, initial-scale=0.5" name="viewport">
<title>%s</title>
<style>
a {
font-size: 2em;
display: block;
}
</style>
</head>
<body>
%s
</body>
</html>
'''
img = u' <img width="768px" src="%s" /><br>\n'
link = u' <a href="%s" target="_blank">%s</a><br>\n'
folder = u' <a href="%s/index.html">%s</a><br>\n'
def enum_files(chdir, img_types=[], link_types=[]):
global html, img, link, folder, is_for_local
for root, dirs, files in os.walk(top= chdir, topdown= True):
relate_path = os.path.relpath(root)
if relate_path=='.' or (relate_path[0]!='.' and os.path.join('a','.')[1:] not in relate_path):
print root
body = u''
for onedir in dirs:
thisdir = onedir.decode(encod)
if is_for_local:
body += folder % (thisdir, thisdir)
else:
body += folder % ((urllib2.quote(onedir)).decode(encod), thisdir)
for onefile in files:
if onefile[0]!='.':
thisfile = onefile.decode(encod)
t = os.path.splitext(thisfile)
if (t[1].lower() in img_types):
if is_for_local:
body += img % thisfile
else:
body += img % (urllib2.quote(onefile)).decode(encod)
if (t[1].lower() in link_types):
if is_for_local:
body += link % (thisfile, thisfile)
else:
body += link % ((urllib2.quote(onefile)).decode(encod), thisfile)
if not os.path.exists(os.path.join(root, '.nomedia')):
with open(os.path.join(root, '.nomedia'), 'w') as f:
f.write('\n')
if len(body)>0:
ret = html % (os.path.basename(root).decode(encod), body)
with open(os.path.join(root, 'index.html'), 'w') as f:
f.write(ret.encode('utf-8'))
if __name__ == '__main__':
if raw_input('is this for local review?(y/n)')=='y':
is_for_local = True
enum_files(os.getcwd(), img_types=['.jpg','.jpeg','.gif','.png'], link_types=['.mp3','.mp4','.flv','.mov',])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment