This commit is contained in:
Ethan Dalool 2017-11-03 12:50:16 -07:00
parent 629b466c3a
commit b5a1649bc0
12 changed files with 267 additions and 22 deletions

View file

@ -9,7 +9,7 @@ var image_height = 200;
var video_height = 300; var video_height = 300;
var audio_width = 1000; var audio_width = 1000;
var IMAGE_TYPES = ["\\.jpg", "\\.jpeg", "\\.jpg", "\\.bmp", "\\.tiff", "\\.tif", "\\.bmp", "\\.gif", "\\.png", "reddituploads\.com"].join("|"); var IMAGE_TYPES = ["\\.jpg", "\\.jpeg", "\\.jpg", "\\.bmp", "\\.tiff", "\\.tif", "\\.bmp", "\\.gif", "\\.png", "reddituploads\.com"].join("|");
var AUDIO_TYPES = ["\\.aac", "\\.mp3", "\\.m4a", "\\.ogg", "\\.opus", "\\.wav"].join("|"); var AUDIO_TYPES = ["\\.aac", "\\.flac", "\\.mp3", "\\.m4a", "\\.ogg", "\\.opus", "\\.wav"].join("|");
var VIDEO_TYPES = ["\\.mp4", "\\.m4v", "\\.webm", "\\.ogv"].join("|"); var VIDEO_TYPES = ["\\.mp4", "\\.m4v", "\\.webm", "\\.ogv"].join("|");
IMAGE_TYPES = new RegExp(IMAGE_TYPES, "i"); IMAGE_TYPES = new RegExp(IMAGE_TYPES, "i");
AUDIO_TYPES = new RegExp(AUDIO_TYPES, "i"); AUDIO_TYPES = new RegExp(AUDIO_TYPES, "i");
@ -22,7 +22,8 @@ var CSS = ""
+ "audio, video { display: block; }" + "audio, video { display: block; }"
+ "audio { width: $audio_width$px; }" + "audio { width: $audio_width$px; }"
+ "video { height: $video_height$px; }" + "video { height: $video_height$px; }"
+ "img { display: block; height: $image_height$px; max-width: 100% }" + "img { display: block; height: $image_height$px; max-width: 100%; }"
+ "a { color: #000 !important; }"
+ ".control_panel { position: relative; background-color: #aaa; min-height: 10px; width: 100%; }" + ".control_panel { position: relative; background-color: #aaa; min-height: 10px; width: 100%; }"
+ ".workspace { background-color: #ddd; min-height: 10px; float: left; }" + ".workspace { background-color: #ddd; min-height: 10px; float: left; }"
+ ".arealabel { position:absolute; right: 0; bottom: 0; opacity: 0.8; background-color: #000; color: #fff; }" + ".arealabel { position:absolute; right: 0; bottom: 0; opacity: 0.8; background-color: #000; color: #fff; }"

View file

@ -2,7 +2,7 @@
This function is slow and ugly, but I need a way to safely print unicode strings This function is slow and ugly, but I need a way to safely print unicode strings
on systems that don't support it without crippling those who do. on systems that don't support it without crippling those who do.
''' '''
def safeprint(text, file_handle=None): def safeprint(text, file_handle=None, end='\n'):
for character in text: for character in text:
try: try:
if file_handle: if file_handle:
@ -15,4 +15,4 @@ def safeprint(text, file_handle=None):
else: else:
print('?', end='', flush=False) print('?', end='', flush=False)
if not file_handle: if not file_handle:
print() print(end, end='', flush=True)

View file

@ -53,7 +53,7 @@ def callback_v1(fpobj, written_bytes, total_bytes):
''' '''
filename = fpobj.absolute_path.encode('ascii', 'replace').decode() filename = fpobj.absolute_path.encode('ascii', 'replace').decode()
if written_bytes >= total_bytes: if written_bytes >= total_bytes:
ends = '\n' ends = '\r\n'
else: else:
ends = '' ends = ''
percent = (100 * written_bytes) / max(total_bytes, 1) percent = (100 * written_bytes) / max(total_bytes, 1)
@ -414,7 +414,11 @@ def copy_file(
written_bytes = 0 written_bytes = 0
while True: while True:
data_chunk = source_handle.read(CHUNK_SIZE) try:
data_chunk = source_handle.read(CHUNK_SIZE)
except PermissionError as e:
print(source)
raise
data_bytes = len(data_chunk) data_bytes = len(data_chunk)
if data_bytes == 0: if data_bytes == 0:
break break

View file

@ -115,6 +115,11 @@ def get_static(filename):
filename = os.path.join('static', filename) filename = os.path.join('static', filename)
return flask.send_file(filename) return flask.send_file(filename)
@site.route("/float/<float:value>")
def float_type(value):
print(value + 1)
return "correct"
if __name__ == '__main__': if __name__ == '__main__':
#site.run(threaded=True) #site.run(threaded=True)
pass pass

View file

@ -13,17 +13,17 @@ else:
if port == 443: if port == 443:
http = gevent.pywsgi.WSGIServer( http = gevent.pywsgi.WSGIServer(
listener=('', port), listener=('0.0.0.0', port),
application=flasksite.site, application=flasksite.site,
keyfile='https\\flasksite.key', keyfile='https\\flasksite.key',
certfile='https\\flasksite.crt', certfile='https\\flasksite.crt',
) )
else: else:
http = gevent.pywsgi.WSGIServer( http = gevent.pywsgi.WSGIServer(
listener=('', port), listener=('0.0.0.0', port),
application=flasksite.site, application=flasksite.site,
) )
print('Starting server') print('Starting server on port %d' % port)
http.serve_forever() http.serve_forever()

21
Toolbox/crop.py Normal file
View file

@ -0,0 +1,21 @@
import os
import sys
from PIL import Image
filename = sys.argv[1]
(name, extension) = os.path.splitext(filename)
newname = '%s_cropped%s' % (name, extension)
crops = sys.argv[2:]
crops = ' '.join(crops)
crops = crops.replace(',', ' ')
crops = crops.replace(' ', ' ')
crops = crops.split(' ')
crops = [int(x) for x in crops]
crops = list(crops)
print(crops)
i = Image.open(filename)
if len(crops) == 2:
crops.extend(i.size)
i = i.crop(crops)
i.save(newname, quality=100)

142
Toolbox/getcrx.py Normal file
View file

@ -0,0 +1,142 @@
import argparse
import io
import json
import os
import requests
import sys
import time
import traceback
import zipfile
from voussoirkit import clipext
FILENAME_BADCHARS = '\\/:*?<>|"'
WEBSTORE_URL = 'https://chrome.google.com/webstore/detail/x/{extension_id}'
CRX_URL = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=59.0&x=id%3D{extension_id}%26installsource%3Dondemand%26uc'
def sanitize_filename(name):
for c in FILENAME_BADCHARS:
name = name.replace(c, '-')
return name
def prompt_permission(prompt):
answer = input(prompt)
return answer.lower() in {'yes', 'y'}
def get_webstore_name_version(extension_id):
url = WEBSTORE_URL.format(extension_id=extension_id)
response = requests.get(url)
try:
name = response.text
name = name.split('meta property="og:title" content="')[1]
name = name.split('"')[0]
except IndexError:
name = None
try:
version = response.text
version = version.split('meta itemprop="version" content="')[1]
version = version.split('"')[0]
except IndexError:
version = None
return (name, version)
def get_crx_name_version(crx_bytes):
crx_handle = io.BytesIO(crx_bytes)
crx_archive = zipfile.ZipFile(crx_handle)
manifest = json.loads(crx_archive.read('manifest.json'))
name = manifest.get('name', None)
version = manifest.get('version', None)
return (name, version)
def getcrx(extension_id, auto_overwrite=None):
url = CRX_URL.format(extension_id=extension_id)
response = requests.get(url)
response.raise_for_status()
(name, version) = get_webstore_name_version(extension_id)
if name is None or version is None:
(crx_name, crx_ver) = get_crx_name_version(response.content)
name = name or crx_name
version = version or crx_version
name = name or extension_id
version = version or time.strftime('%Y%m%d')
version = version or response.url.split('/')[-1]
crx_filename = '{name} ({id}) [{version}]'
crx_filename = crx_filename.format(
name=name,
id=extension_id,
version=version,
)
if not crx_filename.endswith('.crx'):
crx_filename += '.crx'
crx_filename = sanitize_filename(crx_filename)
if os.path.isfile(crx_filename):
if auto_overwrite is None:
message = '"%s" already exists. Overwrite?' % crx_filename
permission = prompt_permission(message)
else:
permission = False
else:
permission = True
if permission:
crx_handle = open(crx_filename, 'wb')
crx_handle.write(response.content)
print('Downloaded "%s".' % crx_filename)
def getcrx_argparse(args):
extension_ids = []
if len(args.extension_ids) == 1:
extension_ids.extend(clipext.resolve(args.extension_ids[0], split_lines=True))
elif args.extension_ids:
extension_ids.extend(args.extension_ids)
if args.file:
with open(args.file, 'r') as handle:
lines = handle.readlines()
extension_ids.extend(lines)
extension_ids = [x.split('/')[-1].strip() for x in extension_ids]
if args.overwrite and not args.dont_overwrite:
auto_overwrite = True
elif args.dont_overwrite and not args.overwrite:
auto_overwrite = False
else:
auto_overwrite = None
for extension_id in extension_ids:
try:
getcrx(extension_id, auto_overwrite=auto_overwrite)
except Exception:
if args.fail_early:
raise
else:
traceback.print_exc()
print('Resuming...')
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('extension_ids', nargs='*', default=None)
parser.add_argument('--file', dest='file', default=None)
parser.add_argument('--fail_early', dest='fail_early', action='store_true')
parser.add_argument('--overwrite', dest='overwrite', action='store_true')
parser.add_argument('--dont_overwrite', dest='dont_overwrite', action='store_true')
parser.set_defaults(func=getcrx_argparse)
args = parser.parse_args(argv)
args.func(args)
if __name__ == '__main__':
main(sys.argv[1:])

15
Toolbox/head.py Normal file
View file

@ -0,0 +1,15 @@
'''
Perform a HEAD request and print the results.
'''
import sys
import json
import requests
from voussoirkit import clipext
url = clipext.resolve(sys.argv[1])
page = requests.head(url)
headers = dict(page.headers)
headers = json.dumps(headers, indent=4, sort_keys=True)
print(page)
print(headers)

16
Toolbox/repeat.py Normal file
View file

@ -0,0 +1,16 @@
'''
Repeat the input as many times as you want
> repeat "hello" 8
> echo hi | repeat !i 4
'''
import sys
from voussoirkit import clipext
text = clipext.resolve(sys.argv[1])
repeat_times = int(sys.argv[2])
for t in range(repeat_times):
print(text)

29
Toolbox/resize.py Normal file
View file

@ -0,0 +1,29 @@
from PIL import Image
import sys
filename = sys.argv[1]
try:
newname = sys.argv[4]
except IndexError:
newname = None
i = Image.open(filename)
if all(x.isdigit() for x in sys.argv[2:3]):
new_x = int(sys.argv[2])
new_y = int(sys.argv[3])
else:
try:
ratio = float(sys.argv[2])
new_x = int(i.size[0] * ratio)
new_y = int(i.size[1] * ratio)
except ValueError:
print('you did it wrong')
quit()
print(i.size, new_x, new_y)
i = i.resize( (new_x, new_y) )
if newname is None:
if '.' in filename:
suffix = '_{width}x{height}'.format(width=new_x, height=new_y)
newname = filename.replace('.', suffix + '.')
else:
newname += suffix
i.save(newname, quality=100)

View file

@ -1,3 +1,4 @@
print('importing')
import argparse import argparse
import fnmatch import fnmatch
import itertools import itertools
@ -45,6 +46,8 @@ def search(
local_only=False, local_only=False,
text=None, text=None,
): ):
if text is None:
print('starting search')
terms = { terms = {
'yes_all': yes_all, 'yes_all': yes_all,
'yes_any': yes_any, 'yes_any': yes_any,
@ -98,6 +101,9 @@ def search(
search_objects = text.splitlines() search_objects = text.splitlines()
for (index, search_object) in enumerate(search_objects): for (index, search_object) in enumerate(search_objects):
if index % 10 == 0:
#print(index, end='\r', flush=True)
pass
if isinstance(search_object, pathclass.Path): if isinstance(search_object, pathclass.Path):
search_text = search_object.basename search_text = search_object.basename
result_text = search_object.absolute_path result_text = search_object.absolute_path
@ -105,7 +111,7 @@ def search(
search_text = search_object search_text = search_object
result_text = search_object result_text = search_object
if line_numbers: if line_numbers:
result_text = '%d | %s' % (index+1, result_text) result_text = '%4d | %s' % (index+1, result_text)
if all_terms_match(search_text, terms, term_matches): if all_terms_match(search_text, terms, term_matches):
if not content_args: if not content_args:
@ -115,9 +121,17 @@ def search(
if not filepath.is_file: if not filepath.is_file:
continue continue
try: try:
with open(filepath.absolute_path, 'r', encoding='utf-8') as handle: with open(filepath.absolute_path, 'r') as handle:
text = handle.read() text = handle.read()
except: except UnicodeDecodeError:
try:
with open(filepath.absolute_path, 'r', encoding='utf-8') as handle:
text = handle.read()
except UnicodeDecodeError:
#safeprint.safeprint(filepath.absolute_path)
#traceback.print_exc()
continue
except Exception:
safeprint.safeprint(filepath.absolute_path) safeprint.safeprint(filepath.absolute_path)
traceback.print_exc() traceback.print_exc()
continue continue

View file

@ -5,18 +5,16 @@ from voussoirkit import clipext
import sys import sys
if len(sys.argv) > 1: if len(sys.argv) > 1:
text = clipext.resolve(sys.argv[1]) source = sys.argv[1]
else: else:
text = clipext.resolve('!input') source = '!input'
lines = clipext.resolve(source, split_lines=True)
text = text.split('\n')
new_text = [] new_text = []
seen = set() seen = set()
for item in text: for line in lines:
if item not in seen: if line not in seen:
new_text.append(item) #new_text.append(line)
seen.add(item) seen.add(line)
print(line)
new_text = '\n'.join(new_text)
print(new_text)