def add_kw_rule(db, c, kw=None, item_uid=None, match='word', target='title', feed_only=False, retroactive=False, stem=None, **kwargs): feed_only = bool(feed_only) retroactive = bool(retroactive) if feed_only: item_uid = int(item_uid) else: item_uid = None if match == 'word': kw = stem if not kw: return if match in ['author', 'tag', 'phrase_lc']: words = [normalize.lower(kw)] elif match in {'word', 'exactword'}: words = normalize.get_words(kw) elif match == 'all': words = [' '.join(normalize.get_words(kw))] elif match == 'phrase': words = [kw] else: return if match in ['author', 'tag']: rule_type = match else: rule_type = target + '_' + match for word in words: print >> param.log, 'ADD_KW_RULES', rule_type, item_uid, word c.execute("""insert into fm_rules (rule_type, rule_feed_uid, rule_text) values (?, (select item_feed_uid from fm_items where item_uid=?), ?)""", [rule_type, item_uid, word]); invalidate()
def __init__(self, uid, expires, rule, rtype): Rule.__init__(self, uid, expires) self.target, self.match = rtype.split('_', 1) assert self.target in ['title', 'content'] if self.match in ['exactword']: self.rule = normalize.get_words(rule) elif self.match in ['word', 'all']: self.rule = normalize.stem(normalize.get_words(rule)) else: self.rule = rule
def stem(): term = flask.request.args.get('q', '') stem = ' '.join(normalize.stem(normalize.get_words(term))) return (stem, 200, {'Content-Type': 'text/plain'})
def process_request(self): try: if self.path in ['', '/']: self.browser_output(301, None, 'This document has moved.', ['Location: /view']) return path, query_string = urlparse.urlparse(self.path)[2:5:2] vars = [] if query_string: # parse_qsl does not comply with RFC 3986, we have to decode UTF-8 query_list = [(n, v.decode('UTF-8')) for n, v in urlparse.parse_qsl(query_string, 1)] self.input.update(dict(query_list)) if param.debug: logging.info((self.command, self.path, self.request_version, vars)) if path.endswith('.gif') and path[1:] in self.images: self.browser_output(200, 'image/gif', self.images[path[1:]], http_headers=no_expire) return if path.endswith('.js') and path[1:] in self.rsrc: self.browser_output(200, 'text/javascript', self.rsrc[path[1:]], http_headers=no_expire) return if path.startswith('/tiny_mce'): # guard against attempts to subvert security using ../ path = os.path.normpath('.' + path) assert path.startswith('tiny_mce') self.set_mime_type(path) self.browser_output(200, self.mime_type, open(path).read(), http_headers=no_expire) return if path.count('favicon.ico') > 0: self.favicon() if path.endswith('.css'): path = path.replace('.css', '_css') tmpl = path.split('/', 1)[1].strip('/') self.use_template(tmpl, [self.input]) if not self.require_auth(param.auth_dict): return if path.startswith('/redirect/'): from singleton import db c = db.cursor() item_uid = int(path[10:]) c.execute('select item_link from fm_items where item_uid=%d' % item_uid) redirect_url = c.fetchone()[0] c.close() self.browser_output(301, None, 'This document has moved.', ['Location: ' + redirect_url]) return if path.startswith('/threads'): frames = sys._current_frames() row = 0 out = [] if singleton.c_opened: out.append('<h1>Open Cursors</h1>\n') for curs, tb in singleton.c_opened.iteritems(): if curs not in singleton.c_closed: row += 1 if row % 2: color = '#ddd' else: color = 'white' out.append('<div style="background-color: ' + color + '">\n<pre>') out.append(curs.replace('<', '<').replace('>', '>') + '\n') out.append('\n'.join(tb[:-2])) out.append('</pre></div>\n') out.append('<h1>Threads</h1>\n') row = 0 for thread_id, frame in sorted(frames.iteritems()): if thread_id == threading.currentThread()._Thread__ident: continue row += 1 if row % 2: color = '#ddd' else: color = 'white' out.append('<div style="background-color: ' + color + '">\n<pre>') out.append('Thread %s (%d refs)\n' % (thread_id, sys.getrefcount(frame))) out.append(''.join(traceback.format_stack(frame)).replace( '&', '&').replace('<', '<').replace('>', '>')) out.append('\n<hr>\n') out.append(pprint.pformat(frame.f_locals).replace( '&', '&').replace('<', '<').replace('>', '>')) out.append('\n</pre>\n</div>\n') del frames self.browser_output(200, 'text/html', ''.join(out)) return if path.startswith('/xmlfeedback/'): op, item_uid = path.split('/')[2::2] item_uid = item_uid.split('.')[0] # for safety, these operations should be idempotent if op in ['promote', 'demote', 'basic', 'yappi']: if op != 'yappi': item_uid = int(item_uid) getattr(self, 'op_' + op)(item_uid) self.xml() return if path.startswith('/stem'): txt = self.input['q'] stem = ' '.join(normalize.stem(normalize.get_words(txt))) self.browser_output(200, 'text/plain', stem) return if path.startswith('/add_kw_rule'): from singleton import db c = db.cursor() try: filters.add_kw_rule(db, c, **self.input) except: util.print_stack() db.commit() c.close() self.xml() return if path.startswith('/del_kw_rule'): from singleton import db c = db.cursor() try: filters.del_kw_rule(db, c, **self.input) except: util.print_stack() db.commit() c.close() self.xml() return if path.startswith('/stats'): from singleton import db c = db.cursor() c.execute("""select date(item_loaded) as date, count(*) as articles, sum(case when item_rating=1 then 1 else 0 end) as interesting, sum(case when item_rating=0 then 1 else 0 end) as unread, sum(case when item_rating=-1 then 1 else 0 end) as filtered from fm_items where item_loaded > julianday('now') - 30 group by 1 order by 1""") csvfile = cStringIO.StringIO() out = csv.writer(csvfile, dialect='excel', delimiter=',') out.writerow([col[0].capitalize() for col in c.description]) for row in c: out.writerow(row) self.browser_output(200, 'text/csv', csvfile.getvalue()) csvfile.close() c.close() return if path.endswith('.css'): path = path.replace('.css', '_css') tmpl = path.split('/', 1)[1].strip('/') self.use_template(tmpl, [self.input]) except TembozTemplate.Redirect, e: redirect_url = e.args[0] self.browser_output(301, None, 'This document has moved.', ['Location: ' + redirect_url]) return