def items(self, query=None, limit=None, recursive=False, since=0): """ Get a list of all items in the library matching the arguments. :param query: Filter items by this query string (targets author and title fields) :type query: str/unicode :param limit: Limit maximum number of returned items :type limit: int :param recursive: Include non-toplevel items (attachments, notes, etc) in output :type recursive: bool :returns: Generator that yields items """ query_args = {'since': since} if query: query_args['q'] = query if limit: query_args['limit'] = limit query_fn = self._zot.items if recursive else self._zot.top items = self._zot.makeiter(query_fn(**query_args)) for chunk in items: for it in chunk: matches = CITEKEY_PAT.finditer(it['data'].get('extra', '')) citekey = next((m.group(1) for m in matches), None) yield Item(key=it['data']['key'], creator=it['meta'].get('creatorSummary'), title=it['data'].get('title', "Untitled"), abstract=it['data'].get('abstractNote'), date=it['data'].get('date'), citekey=citekey)
def search(self, query, limit=None): """ Search the index for items matching the query. :param query: A sqlite FTS4 query :param limit: Maximum number of items to return :returns: Generator that yields matching items. """ with self._db as cursor: query = "'{}'".format(query) for itm in cursor.execute(SEARCH_QUERY, (query, limit or -1)): yield Item(*itm)
def search_tags(self, query, limit=None): """ Search the index for items matching the query. :param query: A sqlite FTS4 query :param limit: Maximum number of items to return :returns: Generator that yields matching items. """ with self._db as cursor: qtags = query.split(',') query = ' AND '.join( ["items_idx.tags LIKE '%{}%'".format(q) for q in qtags]) squery = SEARCH_QUERY_TAGS.replace('items_idx.tags LIKE :query', query).replace(':limit', '-1') for itm in cursor.execute(squery): #, (query, limit or -1)): yield Item(*itm)
def items(self, query=None, limit=None, recursive=False, since=0): """ Get a list of all items in the library matching the arguments. :param query: Filter items by this query string (targets author and title fields) :type query: str/unicode :param limit: Limit maximum number of returned items :type limit: int :param recursive: Include non-toplevel items (attachments, notes, etc) in output :type recursive: bool :returns: Generator that yields items """ if self.betterbibtex: bbtxkeys = self.getBetterBibtexKeys() if limit is None: limit = 100 query_args = {'since': since} if query: query_args['q'] = query if limit: query_args['limit'] = limit query_fn = self._zot.items if recursive else self._zot.top # NOTE: Normally we'd use the makeiter method of Zotero, but it seems # to be broken at the moment, thus we call .follow ourselves items = query_fn(**query_args) last_url = self._zot.links.get('last') if last_url: while self._zot.links['self'] != last_url: items.extend(self._zot.follow()) for it in items: if self.betterbibtex: try: citekey = bbtxkeys[it['data']['key']] except: citekey = None else: matches = CITEKEY_PAT.finditer(it['data'].get('extra', '')) citekey = next((m.group(1) for m in matches), None) yield Item(key=it['data']['key'], creator=it['meta'].get('creatorSummary'), title=it['data'].get('title', "Untitled"), abstract=it['data'].get('abstractNote'), date=it['data'].get('date'), citekey=citekey)