-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
731 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import re | ||
import plistlib | ||
import cndict | ||
|
||
|
||
class AlfredPlist(object): | ||
def __init__(self): | ||
self.__modifier_map = { | ||
0: 'none', | ||
524288: 'alt', | ||
262144: 'ctrl', | ||
131072: 'shift', | ||
1048576: 'cmd', | ||
8388608: 'fn' | ||
} | ||
self.__plist = None | ||
self.__base = None | ||
self.__branches = {} | ||
|
||
def read(self, path): | ||
self.__plist = plistlib.readPlist(path) | ||
for obj in self.__plist['objects']: | ||
if 'keyword' in obj['config']: | ||
self.__base = obj | ||
uid = self.__base['uid'] | ||
for conn in self.__plist['connections'][uid]: | ||
self.__branches[conn['destinationuid']] = [conn, None] | ||
break | ||
for uid, pair in self.__branches.iteritems(): | ||
for obj in self.__plist['objects']: | ||
if uid == obj['uid']: | ||
pair[1] = obj | ||
break | ||
|
||
def write(self, path): | ||
if self.__plist: | ||
plistlib.writePlist(self.__plist, path) | ||
|
||
def get_keyword(self): | ||
if self.__base: | ||
return self.__base['config']['keyword'] | ||
return '' | ||
|
||
def set_keyword(self, value): | ||
if self.__base: | ||
self.__base['config']['keyword'] = value | ||
|
||
def get_keymap(self): | ||
keymap = {} | ||
for conn, child in self.__branches.values(): | ||
modifier = self.__modifier_map[conn['modifiers']] | ||
match = re.search(r'[@|>] (\w+)"', child['config']['script']) | ||
if match: | ||
keymap[modifier] = match.group(1) | ||
return keymap | ||
|
||
def set_keymap(self, value): | ||
keymap = value | ||
for conn, child in self.__branches.values(): | ||
modifier = self.__modifier_map[conn['modifiers']] | ||
if modifier in keymap: | ||
child['config']['script'] = re.sub(r'(?<=[@|>] )\w+(?=")', keymap[modifier], | ||
child['config']['script']) | ||
if modifier != 'none': | ||
dict_name = cndict.get_full_name(keymap[modifier]) | ||
conn['modifiersubtext'] = re.sub(r'(?<=in )\w+(?= dict)', dict_name, | ||
conn['modifiersubtext']) | ||
|
||
def get_bundleid(self): | ||
if self.__plist: | ||
return self.__plist['bundleid'] | ||
return '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
import os | ||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | ||
from cndict import lookup | ||
|
||
if len(sys.argv) < 3: | ||
print 'usage: python cndict <dict> <word>' | ||
sys.exit(1) | ||
|
||
result = lookup(*(sys.argv[1:])) | ||
print '\n'.join(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import urllib | ||
import urllib2 | ||
import gzip | ||
import re | ||
import StringIO | ||
from utils import * | ||
|
||
|
||
def lookup(word, wap_page=True, *args): | ||
params = {'q': word} | ||
if wap_page: | ||
params['view'] = 'wap' | ||
url = 'http://dict.bing.com.cn/' | ||
else: | ||
url = 'http://www.bing.com/dict/search' | ||
url = '{}?{}'.format(url, urllib.urlencode(params)) | ||
try: | ||
request = urllib2.Request(url) | ||
request.add_header('Accept-Encoding', 'gzip') | ||
response = urllib2.urlopen(request) | ||
data = response.read() | ||
except: | ||
raise DictLookupError('error to fetch data.') | ||
|
||
if response.info().get('Content-Encoding') == 'gzip': | ||
gzip_file = gzip.GzipFile(fileobj=StringIO.StringIO(data)) | ||
data = gzip_file.read() | ||
|
||
result = [] | ||
is_eng = is_english(word) | ||
|
||
if wap_page: | ||
match = re.search(r'<div><b>.*?</b>{}<br />(.*?)<br />web\.<br />'.format( | ||
r'.*?US:\[(.*?)\](.*UK:\[.*?\])?.*?' | ||
if is_eng else | ||
r'.*?'), data) | ||
if match is None: | ||
raise DictLookupError('failed to parse html.') | ||
|
||
phonetic = match.group(1) if is_eng else '' | ||
result.append('{}{}'.format(word, ' /{}/'.format(phonetic) if phonetic else '')) | ||
definition = match.group(3 if is_eng else 1) | ||
items = definition.replace(' ', '').replace('•', '').split('<br />') | ||
part = '' | ||
for item in items: | ||
match = re.match(r'^([a-z]+\.)$', item) | ||
if match: | ||
part = match.group(1) | ||
continue | ||
result.append('{} {}'.format(part, item)) | ||
else: | ||
# no need to use BeautifulSoup, just extract definition from meta tag | ||
match = re.search(r'<meta name="description" content="(.*?)"/>', data) | ||
if match is None: | ||
raise DictLookupError('failed to find meta tag.') | ||
description = match.group(1) | ||
|
||
match = re.match(r'^必应词典为您提供.*的释义,{},(.*); 网络释义: .*; $'.format( | ||
r'美\[(.*?)\](,英\[.*?\])?' | ||
if is_eng else | ||
r'拼音\[(.*)\]'), description) | ||
if match: | ||
phonetic = match.group(1) | ||
result.append('{}{}'.format(word, ' /{}/'.format(phonetic) if phonetic else '')) | ||
items = match.group(3 if is_eng else 2).split('; ') | ||
if is_eng: | ||
for item in items: | ||
if item != '': | ||
result.append(item) | ||
else: | ||
for item in items: | ||
match = re.match(r'([a-z]+\.) (.+)', item) | ||
if match: | ||
part = match.group(1) | ||
for new_item in match.group(2).split('; '): | ||
result.append('{} {}'.format(part, new_item)) | ||
return result | ||
|
||
|
||
def extract(word, item): | ||
if not is_english(word): | ||
match = re.match(r'[a-z]+\. (.+)', item) | ||
if match: | ||
return match.group(1) | ||
|
||
|
||
def get_url(word): | ||
params = {'q': word} | ||
return '{}?{}'.format('http://www.bing.com/dict/search', urllib.urlencode(params)) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.