Ejemplo n.º 1
0
def delete_item(item_id):
    access_token = WF.get_password('pocket_access_token')
    pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
    try:
        pocket_instance.delete(item_id, wait=False)
        remove_from_cache(item_id)
        return 'Link deleted'
    except PocketException:
        return 'Connection error'
Ejemplo n.º 2
0
def get_auth_url():
    request_token = Pocket.get_request_token(
        consumer_key=config.CONSUMER_KEY, redirect_uri=config.REDIRECT_URI)
    WF.cache_data('pocket_request_token', request_token)

    auth_url = Pocket.get_auth_url(
        code=request_token, redirect_uri=config.REDIRECT_URI)

    return auth_url
Ejemplo n.º 3
0
def get_auth_url():
    request_token = Pocket.get_request_token(
        consumer_key=config.CONSUMER_KEY, redirect_uri=config.REDIRECT_URI)
    WF.cache_data('pocket_request_token', request_token)

    auth_url = Pocket.get_auth_url(
        code=request_token, redirect_uri=config.REDIRECT_URI)

    return auth_url
Ejemplo n.º 4
0
def main():
    wf = Workflow()
    error = None
    try:
        # initialize client
        access_token = wf.get_password('pocket_access_token')
        pocket_instance = Pocket(config.CONSUMER_KEY, access_token)

        since = wf.cached_data('pocket_since', max_age=0) or 0
        links = wf.cached_data('pocket_list', max_age=0) or {}

        next_since = 0
        offset = 0
        while True:
            get = pocket_instance.get(
                detailType='complete',
                since=since,
                state='all',
                count=LINK_LIMIT,
                offset=offset
            )[0]

            data = get['list']
            next_since = get['since']

            if get['status'] != 1 or len(data) == 0:
                break

            links.update(data)
            offset += LINK_LIMIT

        # Delete obsolete entries
        for item_id in links.keys():
            if links[item_id]['status'] == '2':
                del links[item_id]

        wf.cache_data('pocket_since', next_since)
        wf.cache_data('pocket_list', links)
        tags = list(set([t for l in links.values() if 'tags' in l
                         for t in l['tags'].keys()]))
        wf.cache_data('pocket_tags', tags)

        # update fulltext search index
        for item_id in links.keys():
            url = links[item_id]['given_url']
            if not FullText.has_link(url):
                FullText.get_instance().add_page(url)
            time.sleep(.1)

    except (AuthException, URLError, PocketException, PasswordNotFound), e:
        error = type(e).__name__
        wf.cache_data('pocket_error', error)

        # delete token if authentication failed
        if error == 'AuthException':
            wf.delete_password('pocket_access_token')
Ejemplo n.º 5
0
def favorite_item(url):
    item_id = get_id(url)
    if not item_id:
        return '"item_id" not found'
    access_token = WF.get_password('pocket_access_token')
    pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
    try:
        pocket_instance.favorite(item_id, wait=False)
        return 'Link favorited'
    except PocketException:
        return 'Connection error'
Ejemplo n.º 6
0
def favorite_item(url):
    item_id = get_id(url)
    if not item_id:
        return '"item_id" not found'
    access_token = WF.get_password('pocket_access_token')
    pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
    try:
        pocket_instance.favorite(item_id, wait=False)
        return 'Link favorited'
    except PocketException:
        return 'Connection error'
Ejemplo n.º 7
0
def add_item(item, tags):
    if item is not None:
        access_token = wf.get_password('pocket_access_token')
        pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
        try:
            pocket_instance.add(
                url=item['url'], title=item['title'], tags=",".join(tags))
            return True
        except InvalidQueryException:
            pass
    return False
Ejemplo n.º 8
0
def delete_item(url):
    item_id = get_id(url)
    if not item_id:
        return '"item_id" not found'
    access_token = WF.get_password('pocket_access_token')
    pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
    try:
        pocket_instance.delete(item_id, wait=False)
        remove_from_cache(item_id)
        FullText.get_instance().del_page(url)
        return 'Link deleted'
    except PocketException:
        return 'Connection error'
Ejemplo n.º 9
0
def main():
    wf = Workflow()
    error = None
    try:
        # initialize client
        access_token = wf.get_password('pocket_access_token')
        pocket_instance = Pocket(config.CONSUMER_KEY, access_token)

        state = None
        since = wf.cached_data('pocket_since', max_age=0) or 0
        links = {}
        # fetch cached links if since is not 0
        if since > 0:
            links = wf.cached_data('pocket_list', max_age=0) or {}
            
            # Only use delta syncing if dict is not empty
            if links:
                state = 'all'

        next_since = 0
        offset = 0
        while True:
            get = pocket_instance.get(
                sort='newest',
                detailType='complete',
                since=since,
                state=state,
                count=LINK_LIMIT,
                offset=offset
            )[0]

            data = get['list']
            next_since = get['since']

            if get['status'] != 1 or len(data) == 0:
                break

            links = sync_data(links, data)
            offset += LINK_LIMIT

        wf.cache_data('pocket_since', next_since)
        wf.cache_data('pocket_list', links)

    except (AuthException, URLError, PocketException, PasswordNotFound), e:
        error = type(e).__name__
        wf.cache_data('pocket_error', error)

        # delete token if authentication failed
        if error == 'AuthException':
            wf.delete_password('pocket_access_token')
Ejemplo n.º 10
0
def main():
    wf = Workflow()
    error = None
    try:
        # initialize client
        access_token = wf.get_password('pocket_access_token')
        pocket_instance = Pocket(config.CONSUMER_KEY, access_token)

        since = wf.cached_data('pocket_since', max_age=0) or 0
        links = wf.cached_data('pocket_list', max_age=0) or {}

        next_since = 0
        offset = 0
        while True:
            get = pocket_instance.get(
                detailType='complete',
                since=since,
                state='all',
                count=LINK_LIMIT,
                offset=offset
            )[0]

            data = get['list']
            next_since = get['since']

            if get['status'] != 1 or len(data) == 0:
                break

            links.update(data)
            offset += LINK_LIMIT

        # Delete obsolete entries
        for item_id in links.keys():
            if links[item_id]['status'] == '2':
                del links[item_id]

        wf.cache_data('pocket_since', next_since)
        wf.cache_data('pocket_list', links)
        tags = list(set([t for l in links.values() if 'tags' in l
                        for t in l['tags'].keys()]))
        wf.cache_data('pocket_tags', tags)

    except (AuthException, URLError, PocketException, PasswordNotFound), e:
        error = type(e).__name__
        wf.cache_data('pocket_error', error)

        # delete token if authentication failed
        if error == 'AuthException':
            wf.delete_password('pocket_access_token')
Ejemplo n.º 11
0
def main():
    wf = Workflow()
    error = None
    try:
        # initialize client
        access_token = wf.get_password('pocket_access_token')
        pocket_instance = Pocket(config.CONSUMER_KEY, access_token)

        since = wf.cached_data('pocket_since', max_age=0) or 0
        links = wf.cached_data('pocket_list', max_age=0) or {}

        next_since = 0
        offset = 0
        while True:
            get = pocket_instance.get(detailType='complete',
                                      since=since,
                                      state='all',
                                      count=LINK_LIMIT,
                                      offset=offset)[0]

            data = get['list']
            next_since = get['since']

            if get['status'] != 1 or len(data) == 0:
                break

            links.update(data)
            offset += LINK_LIMIT

        # Delete obsolete entries
        for item_id in links.keys():
            if links[item_id]['status'] == '2':
                del links[item_id]

        wf.cache_data('pocket_since', next_since)
        wf.cache_data('pocket_list', links)

    except (AuthException, URLError, PocketException, PasswordNotFound), e:
        error = type(e).__name__
        wf.cache_data('pocket_error', error)

        # delete token if authentication failed
        if error == 'AuthException':
            wf.delete_password('pocket_access_token')
Ejemplo n.º 12
0
def authorize():  # pragma: no cover
    request_token = WF.cached_data('pocket_request_token')
    if request_token:
        try:
            user_credentials = Pocket.get_credentials(
                consumer_key=config.CONSUMER_KEY, code=request_token)
            WF.save_password('pocket_access_token',
                             user_credentials['access_token'])
            # We don't need the cache anymore. Clear it for security reasons
            WF.clear_cache()
        except RateLimitException:
            WF.logger.error('RateLimitException')
Ejemplo n.º 13
0
def authorize():  # pragma: no cover
    request_token = WF.cached_data('pocket_request_token')
    if request_token:
        try:
            user_credentials = Pocket.get_credentials(
                consumer_key=config.CONSUMER_KEY,
                code=request_token
            )
            WF.save_password(
                'pocket_access_token',
                user_credentials['access_token']
            )
            # We don't need the cache anymore. Clear it for security reasons
            WF.clear_cache()
        except RateLimitException:
            WF.logger.error('RateLimitException')
Ejemplo n.º 14
0
import argparse
import os
import subprocess
import urlparse
from pocket_api import Pocket, InvalidQueryException
from workflow import Workflow
import config

WF = Workflow()
POCKET = Pocket(config.CONSUMER_KEY, WF.get_password('pocket_access_token'))

FRONTMOST_APP = """\
osascript -e 'application (path to frontmost application as text)'\
"""
BROWSER_SCRIPTS = {
    'Firefox': {
        'url': """\
osascript -e 'tell application "Firefox" to activate\n \
  set old_clipboard to the clipboard\n \
  tell application "System Events"\n \
      repeat until (exists window 1 of process "Firefox") \n \
        delay 0.1 \n \
      end repeat\n \
      keystroke "l" using command down\n \
      keystroke "c" using command down\n \
  end tell\n \
  delay .5\n \
  set new_clipboard to the clipboard\n \
  set the clipboard to old_clipboard\n \
  return new_clipboard' \
""",