Пример #1
0
def query_word():
    '''查詢單字'''
    # print request.json
    # js = jsonify(**request.json)
    word = request.json['word']

    dict = YahooDict()
    result = dict.query(word).html()
    if result == None:
        result = "Can't find!"
    return result
Пример #2
0
def save():
    '''存到單字本(存到mongodb)'''
    
    if g.user == None:
        return "You should login first!"

    word = request.json['word']
    try:  

        mcli = MongoClient('localhost', 27017)
        mdb = mcli.wordgo
        db_words = mdb.word
		
        # 確認是否已經有了
        qword = db_words.find_one({"word" : word })
        print qword
        if qword:
            return "have!!"		
    	
        dict = YahooDict()
        result = dict.query(word)
        word_info = {}
        word_info['html'] = result.html()
        word_info['text'] = result.text()
        
        # json
        t = time.time()
        word_jn = {}
        word_jn['user'] = g.user.account
        word_jn['word'] = word
        word_jn['word_info'] = word_info
        word_jn['familiar'] = 0
        word_jn['date'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t))

        word_id = db_words.insert(word_jn)
        print word_id
        
    except ValueError:
    	return "Save Error"

    return "Saved"
Пример #3
0
def save_old():
    '''存到單字本(舊的, 寫入到本地端資料)'''
    word = request.json['word']
    if g.user:
        fn = "users/"+g.user.account+"/words.dat"
        path = "." + url_for('static', filename=fn)
        #path = ".//static//users//"+g.user.account+"//words.dat"
        print path
        try:  
            dict = YahooDict()
            result = dict.query(word)
            word_info = {}
            word_info['html'] = result.html()
            word_info['text'] = result.text()
            
            # # shelve
            # db = shelve.open(path, 'c')  
            # db[str(word)] = word_info

            if not os.path.exists(path):
                ff = open(path,"w+")
                ff.close()

            # json
            word_jn = {}
            try:
                with open(path,"r") as f: 
                    word_jn = json.load(f)
            except ValueError:
                pass

            word_jn[word] = word_info
            f = open(path,"w")
            json.dump(word_jn,f)

        finally:  
            #db.close()
            f.close()

    return "Saved"