Permalink
Browse files

add typoglycemia and ftp

  • Loading branch information...
321cyb committed Nov 9, 2012
1 parent e41ad83 commit 40f2efa37a058d876289cde432b8fa6572306d26
Showing with 367 additions and 0 deletions.
  1. +133 −0 ftp/monitor.py
  2. +15 −0 ftp/setting.py
  3. +89 −0 ftp/transfer.py
  4. +5 −0 snippets/daemon.py
  5. +70 −0 typoglycemia/typoglycemia.cpp
  6. +55 −0 typoglycemia/typoglycemia.py
View
@@ -0,0 +1,133 @@
+#!/usr/bin/env python3
+#-*- coding: utf-8 -*-
+#Author: Kevin Chen
+#Date:
+#Problem description: http://www.taskcity.com/p/135698
+
+
+
+#Known issues: Use os.stat to check file size may get wrong result
+#on NFS devices.
+
+
+import os
+import sys
+
+
+import logging
+import signal
+import threading
+import subprocess
+import pdb
+import argparse
+
+import transfer
+
+
+parser = argparse.ArgumentParser(description="Monitors all .wmv files under a directory and uploads them to a FTP.")
+parser.add_argument("-d", default=".")
+
+
+transfer_in_progress = False
+folder_name = None
+
+last_file_dict = None
+last_file_set = None
+sent_files - set()
+#By the way, I hate global variable.
+
+
+def collect_data():
+ '''
+ walk through the directory and collects current file size information.
+ '''
+ global folder_name
+ global sent_files
+ current_file_dict = {}
+ for dirpath, dirnames, filenames in os.walk(folder_name):
+ for filename in filenames:
+ if filename.endswith(".wmv") and filename not in sent_files:
+ join_name = os.path.join(dirpath, filename)
+ current_file_dict[join_name] = os.stat(join_name).st_size
+ current_file_set = set(current_file_dict)
+ return (current_file_dict, current_file_set)
+
+
+
+def findout_complete_files(current_file_dict, current_file_set):
+ '''
+ Give the current snapshot and the last snapshot, computes the files that are not changed,
+ and we consider them finished.
+ '''
+ global last_file_dict
+ global last_file_set
+ complete_files = []
+ mutual_set = current_file_set & last_file_set
+ for file in mutual_set:
+ if current_file_dict[file] == last_file_dict[file]:
+ complete_files.append(file)
+ logging.debug("check_complete_files add file: {0}, current size is {1}".format( file, current_file_dict[file]))
+ return complete_files
+
+
+
+
+
+def timeout_handler(first_time = False):
+ '''
+ called when time out.
+ '''
+ global transfer_in_progress
+ global last_file_dict
+ global last_file_set
+ global sent_files
+ logging.warning("")
+ logging.warning("timeout_handler enters, first_time is {0}".format(first_time))
+ if transfer_in_progress:
+ return
+
+ complete_files = []
+
+ current_file_dict, current_file_set = collect_data()
+
+ if not first_time:
+ complete_files = findout_complete_files(current_file_dict, current_file_set)
+
+ last_file_dict, last_file_set = current_file_dict, current_file_set
+
+ transfer_in_progress = True
+
+ for file in complete_files:
+ if file not in sent_files: #double check, to be safe.
+ print(file + "is ready to be processed.")
+ if transfer.process_a_file(file) == 0:
+ sent_files.add(file)
+
+
+ transfer_in_progress = False
+ threading.Timer(6, timeout_handler).start()
+
+
+
+def start_monitor():
+ timeout_handler(True)
+ dummy_event = threading.Event()
+ dummy_event.wait()
+
+
+
+
+if __name__ == "__main__":
+ args = parser.parse_args()
+ folder_name = args.d
+
+ logging.basicConfig(level=logging.DEBUG)
+ #install SIGINT handler
+# signal.signal(signal.SIGINT, signal.SIG_DFL)
+
+ start_monitor()
+
+
+
+
+# vim: ai ts=4 sts=4 et sw=4
View
@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+#-*- coding: utf-8 -*-
+#Author: Kevin Chen
+#Date:
+
+
+REMOTE_HOST = "192.168.0.1"
+USER = "xxx"
+PASSWORD = "xxx"
+
+
+
+
+
+# vim: ai ts=4 sts=4 et sw=4
View
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+#-*- coding: utf-8 -*-
+#Author: Kevin Chen
+#Date:
+
+import os
+import sys
+import subprocess
+import ftplib
+
+from setting import REMOTE_HOST, USER, PASSWORD
+
+def check_filename(filename):
+ '''
+ This function generates an output file name from the input file name.
+ '''
+ if filename.endswith(".wmv"):
+ basename = filename[:filename.rfind(".wmv")]
+ return (filename, basename + ".mp4")
+ else:
+ return (None, None)
+
+
+
+
+def convert_file(input, output):
+ '''
+ This function calls ffmpeg to convert wmv file to mp4 file.
+ Ensure that ffmpeg has libx264, libaac and other related codec support.
+ '''
+ p = subprocess.Popen(["ffmpeg", "-i", input, output], stdout=subprocess.PIPE)
+ if p.wait() is not 0:
+ sys.stderr.write("ffmpeg execute error, below is output messages:\n")
+ sys.stderr.write(p.stdout.read())
+ return False
+ return True
+
+
+
+
+
+def transfer_file(output):
+ '''
+ Open a FTP connection and send the output file to remote server.
+ '''
+ with ftplib.FTP(REMOTE_HOST, USER, PASSWORD) as ftp:
+ with open(output, "r") as f:
+ try:
+ ftp.storbinary("STORE " + output, f)
+ return True
+ except Exception as e:
+ sys.stderr.write(str(e))
+ return False
+ return False
+
+
+
+def process_a_file(filename):
+ '''
+ Given an input file name, this function converts that file to mp4 and send it to server.
+ '''
+ (i,o) = check_filename(filename)
+ if not i or not o:
+ sys.stderr.write("argument error:" + filename)
+ return 1
+ if not convert_file(i,o):
+ return 2
+ if not transfer_file(o):
+ return 3
+ return 0
+
+
+
+
+if __name__ == "__main__":
+ for arg in sys.argv[1:]:
+ (i,o) = check_filename(arg)
+ if not i or not o:
+ sys.stderr.write("argument error:" + arg)
+ sys.exit(1)
+ if not convert_file(i,o):
+ sys.exit(2)
+ if not transfer_file(o):
+ sys.exit(3)
+ sys.exit(0)
+
+
+
+# vim: ai ts=4 sts=4 et sw=4
View
@@ -16,6 +16,11 @@
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
"""
+'''
+Add by me, there's a more mature project called python-daemon,
+there even is a PEP 3143 about it.
+'''
+
__author__ = "Chad J. Schroeder"
__version__ = "$Id$"
@@ -0,0 +1,70 @@
+#include <iostream>
+#include <string>
+
+#include <ctime>
+#include <cstdlib>
+#include <cctype>
+
+using namespace std;
+
+//Do exactly the same as random.shuffle.
+void random_shuffle(string &word)
+{
+ for(int i = word.length() - 1; i > 0; i--)
+ {
+ int j = random() % (i + 1);
+ char temp = word[i];
+ word[i] = word[j];
+ word[j] = temp;
+ }
+}
+
+string process_a_word(const string &word)
+{
+ string ret("");
+ if(word.size() > 3)
+ {
+ string middle = word.substr(1, word.size() - 2);
+ random_shuffle(middle);
+ ret = word[0]+ middle + word[word.size()-1];
+ }
+ else
+ {
+ ret = word;
+ }
+ return ret;
+}
+
+string typoglycemia(string & src)
+{
+ string segment("");
+ string ret("");
+
+ srand(time(NULL));
+
+ for(string::iterator ps = src.begin(); ps != src.end(); ps++)
+ {
+ if(! isalpha(*ps) )
+ {
+ ret += process_a_word(segment);
+ ret += *ps;
+ segment = "";
+ }
+ else
+ {
+ segment += *ps;
+ }
+ }
+
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ string text("According to a research at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place, the reset can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself, but the word as a whole. Amazing!");
+
+ string result = typoglycemia(text);
+ cout << result << endl;
+
+ return 0;
+}
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+#-*- coding: utf-8 -*-
+#Author: Kevin Chen
+#Date:
+#See the description of problem here:
+#http://blog.zhaojie.me/2012/11/how-to-generate-typoglycemia-text.html
+#
+#Here's a version from Internet, only one line:
+#' '.join(map(lambda w:w if len(w)<4 else w[0]+''.join((lambda l:random.shuffle(l)or l)(list(w[1:-1]))) +w[-1], text.split(' ')))
+#or this one:
+#" ".join(["".join([t[0],"".join(random.sample(list(itertools.permutations(t[1:-1],len(t)-2)),1)[0]),t[-1]]) for t in text.split(" ")])
+
+import random
+import sys
+
+def process_a_word(word):
+ if len(word) > 3:
+ first = word[0]
+ last = word[-1]
+ middle = list(word[1:-1])
+ #random.shuffle always return None.
+ result = first + "".join(random.shuffle(middle) or middle) + last
+ sys.stdout.write(result)
+ elif word is not "":
+ sys.stdout.write(word)
+
+
+
+
+def typoglycemia(p):
+ seg = ""
+ for c in p:
+ if not c.isalpha():
+ process_a_word(seg)
+ seg = ""
+ sys.stdout.write(c)
+ else:
+ seg = seg + c
+ process_a_word(seg)
+ print("")
+
+
+text = "According to a research at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place, the reset can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself, but the word as a whole. Amazing!"
+
+
+if __name__ == "__main__":
+ print(text)
+ print("")
+ typoglycemia(text)
+
+
+
+
+
+# vim: ai ts=4 sts=4 et sw=4

0 comments on commit 40f2efa

Please sign in to comment.