Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
add page navigate for vip user
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfen@iMac committed Sep 5, 2017
1 parent cf0359c commit cf0d6f5
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 24 deletions.
Binary file modified icons/dark/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icons/dark/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icons/dark/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/dark/next.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icons/light/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icons/light/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icons/light/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/light/next.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions links/api/search_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
from links import icons, config
from links.util import workflow

from links.models.preferences import Preferences
import logging
from logging.config import fileConfig
fileConfig('logging_config.ini')
Expand All @@ -27,7 +27,7 @@ def getWorkflowVersion():
return ver.strip()


def search(query):
def search(query, offset, size):
localResult = {
'statusCode': 0,
'message': '',
Expand All @@ -38,9 +38,12 @@ def search(query):
log.info('appKey: %s' % (appKey))

# query the keyword from web server
prefs = Preferences.current_prefs()
session = requests.session()
formData = {
'keyword': query
'keyword': query,
'from': offset,
'size': size
}
resp = session.post(
url=config.LK_SEARCH_APP_URL,
Expand Down
2 changes: 2 additions & 0 deletions links/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def handle_authorization_url(url):
workflow().store_data(config.KC_RESERVED_COUNT, params['reservedCount'][0])
if 'userName' in params:
workflow().store_data(config.KC_USER_NAME, params['userName'][0])
if 'vip' in params:
workflow().store_data(config.KC_VIP_STATUS, params['vip'][0])

print '您已经成功登录'
return True
Expand Down
9 changes: 7 additions & 2 deletions links/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
KC_MAX_COUNT = 'links_max_count'
KC_RESERVED_COUNT = 'links_reserved_count'
KC_ENABLE_SEARCH = 'links_enable_search'
KC_VIP_STATUS = 'links_user_vip_status'
KC_CURRENT_PAGE_INDEX = 'links_current_page_idx'
KC_LAST_TIME_QUERY_WORD = 'links_last_time_query_word'


LK_SEARCH_APP_URL = 'http://elinks.localhost.com/api/v1/links/search/app'
LK_OAUTH_URL = 'http://links.localhost.com/authorize'
LK_SEARCH_APP_URL = 'http://elinks.tickstep.com/api/v1/links/search/app'
LK_OAUTH_URL = 'http://links.tickstep.com/authorize'
# LK_SEARCH_APP_URL = 'http://elinks.localhost.com/api/v1/links/search/app'
# LK_OAUTH_URL = 'http://links.localhost.com/authorize'
6 changes: 4 additions & 2 deletions links/handlers/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@


def filter(args):
prefs = Preferences.current_prefs()

workflow().add_item(
u'查询结果个数',
u'设置服务器返回结果的最大个数',
arg='-pref resultCount', valid=True, icon=icons.LIST
u'设置服务器每页返回结果的最大个数',
autocomplete='-result_count ',
icon=icons.LIST
)

workflow().add_item(
Expand Down
45 changes: 45 additions & 0 deletions links/handlers/result_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# encoding: utf-8

from links import icons, config
from links.util import workflow, relaunch_alfred
from links.models.preferences import Preferences


def filter(args):
prefs = Preferences.current_prefs()

icon = icons.TASK
if prefs.maxResultCount == 7:
icon = icons.TASK_COMPLETED
workflow().add_item(
u'设定最大值为 7',
arg='-result_count max_7', valid=True, icon=icon
)

icon = icons.TASK
if prefs.maxResultCount == 20:
icon = icons.TASK_COMPLETED
workflow().add_item(
u'设定最大值为 20',
u'VIP账户才能生效',
arg='-result_count max_20', valid=True, icon=icon
)

workflow().add_item(
u'返回主菜单',
autocomplete=' ', icon=icons.BACK
)


def commit(args, modifier=None):
prefs = Preferences.current_prefs()
relaunch_command = 'lk '

if 'max_7' in args:
prefs.maxResultCount = 7

elif 'max_20' in args:
prefs.maxResultCount = 20

if relaunch_command:
relaunch_alfred(relaunch_command)
5 changes: 4 additions & 1 deletion links/handlers/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ def route(args):
elif 'pref'.find(action) == 0:
from links.handlers import preferences
handler = preferences
elif 'result_count'.find(action) == 0:
from links.handlers import result_count
handler = result_count

elif 'search'.find(action) == 0:
elif action.find('search') == 0:
from links.handlers import search
handler = search

Expand Down
59 changes: 45 additions & 14 deletions links/handlers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import datetime
from links import icons, config
from links.models.preferences import Preferences
from links.util import workflow
from links.api import search_api
import logging
Expand All @@ -11,14 +12,28 @@


def filter(args):
prefs = Preferences.current_prefs()

log.info(args)
queryWord = ''
if len(args) >= 2:
queryWord = ''
for idx in range(1, len(args)):
w = args[idx]
queryWord = "%s %s" % (queryWord, w)
queryWord = queryWord.strip()

if not workflow().stored_data(config.KC_ENABLE_SEARCH):
workflow().add_item(
u'先返回主菜单再进行搜索',
autocomplete=' ', icon=icons.BACK
)
workflow().store_data(config.KC_ENABLE_SEARCH, False)
return
log.info('enable search false')
log.info('store last time keyword: %s' % workflow().stored_data(config.KC_LAST_TIME_QUERY_WORD))
# the same keyword means this is page navigate action
if queryWord != workflow().stored_data(config.KC_LAST_TIME_QUERY_WORD):
workflow().add_item(
u'需要先返回主菜单才能进行新的搜索',
autocomplete=' ', icon=icons.BACK
)
workflow().store_data(config.KC_ENABLE_SEARCH, False)
return

workflow().add_item(
u'返回主菜单',
Expand All @@ -27,16 +42,19 @@ def filter(args):
log.info('begin to search')
workflow().store_data(config.KC_ENABLE_SEARCH, False)

# page index
scmd = args[0]
idx = scmd.find(':')
if idx > 0:
pidx = int(scmd[idx + 1:])
workflow().store_data(config.KC_CURRENT_PAGE_INDEX, pidx)
else:
workflow().store_data(config.KC_CURRENT_PAGE_INDEX, 0)

# search
queryWord = ''
if len(args) >= 2:
queryWord = ''
for idx in range(1, len(args)):
w = args[idx]
queryWord = "%s %s" % (queryWord, w)
queryWord = queryWord.strip()
log.info('begin to search %s' % (queryWord))
result = search_api.search(queryWord)
workflow().store_data(config.KC_LAST_TIME_QUERY_WORD, queryWord)
result = search_api.search(queryWord, workflow().stored_data(config.KC_CURRENT_PAGE_INDEX) * prefs.maxResultCount, prefs.maxResultCount)

if result['statusCode'] == 0:
if 'data' in result and result['data'] != None and len(result['data']) > 0:
Expand All @@ -48,6 +66,11 @@ def filter(args):
if 'reserved' in queryCount:
workflow().store_data(config.KC_RESERVED_COUNT, queryCount['reserved'])

# vip
if 'vip' in queryCount:
log.info('vip = %s' % (queryCount['vip']))
workflow().store_data(config.KC_VIP_STATUS, queryCount['vip'])

# download link item
items = result['data']['items']
if items == None or len(items) == 0:
Expand All @@ -69,6 +92,14 @@ def filter(args):
workflow().add_item(item['name'], ts + ' / ' + item['dlUrl']['url'], copytext=dl,
largetext=item['name'] + '\n' + dl,
icon=icons.APP)

# next page has?
if len(items) >= prefs.maxResultCount and workflow().stored_data(config.KC_VIP_STATUS):
workflow().add_item(
u'下一页',
autocomplete='-search:%s %s' % (workflow().stored_data(config.KC_CURRENT_PAGE_INDEX) + 1, queryWord), icon=icons.NEXT
)

else:
workflow().add_item(u'没有查询到结果,更改关键词再试试吧')
else:
Expand Down
7 changes: 6 additions & 1 deletion links/handlers/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def filter(args):
icon=icons.SEARCH
)
workflow().store_data(config.KC_ENABLE_SEARCH, True)
workflow().store_data(config.KC_CURRENT_PAGE_INDEX, 0)

reservedCount = u'%s: %s 次, %s: %s 次' % (u'本月剩余', workflow().stored_data(config.KC_RESERVED_COUNT), u'本月总共', workflow().stored_data(config.KC_MAX_COUNT))
workflow().add_item(
Expand All @@ -63,7 +64,11 @@ def filter(args):
icon=icons.LIST
)

logoutAccount = '%s: %s' % (u'退出您当前登录的账户', workflow().stored_data(config.KC_USER_NAME))
vipFlag = ''
log.info(workflow().stored_data(config.KC_VIP_STATUS))
if workflow().stored_data(config.KC_VIP_STATUS):
vipFlag = '(VIP)'
logoutAccount = '%s: %s%s' % (u'退出您当前登录的账户', workflow().stored_data(config.KC_USER_NAME), vipFlag)
workflow().add_item(
u'退出登录',
logoutAccount,
Expand Down
1 change: 1 addition & 0 deletions links/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ def icon_theme():
APP = _icon_path + 'app.png'
COPY = _icon_path + 'copy.png'
ICON = _icon_path + 'icon.png'
NEXT = _icon_path + 'next.png'
2 changes: 1 addition & 1 deletion links/models/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _get(self, key, default=None, type=str):

@property
def maxResultCount(self):
return self._get(KEY_MAX_RESTUL_COUNT) or 7
return self._get(KEY_MAX_RESTUL_COUNT, 20)

@maxResultCount.setter
def maxResultCount(self, count):
Expand Down

0 comments on commit cf0d6f5

Please sign in to comment.