Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

修復cahce login 的問題#74 #75

Merged
merged 3 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/kuas_api/kuas/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import requests
from werkzeug.contrib.cache import SimpleCache


import kuas_api.kuas.ap as ap
import kuas_api.kuas.leave as leave
import kuas_api.kuas.parse as parse
Expand All @@ -31,12 +30,16 @@
AP_GUEST_PASSWORD = "123"

s_cache = SimpleCache()
red = redis.StrictRedis.from_url(url= os.environ['REDIS_URL'],db=2)
red = redis.StrictRedis.from_url(url=os.environ['REDIS_URL'], db=2)
SECRET_KEY = red.get("SECRET_KEY") if red.exists(
"SECRET_KEY") else str(os.urandom(32))
# Only use in cache.login , get encoded data from redis.
# get data from redis should be able use without any decode or encode action.
red_auth = redis.StrictRedis.from_url(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it, what is the different between red and red_auth?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't use use red?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這次為了減少更動,當初存cookie存入redis沒有設定編碼(存bytes),導致把資料拿出來又要經過一波轉碼的過程,個人認為轉碼應該在redis上就做好,不應該撈出來再轉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, then it should be a separate PR, and it should not deal in this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我認為還是在同一個問題中,我並沒有要處理其他舊有的code存入redis是bytes的問題,我而外開了一個是已經會編碼的redis連接只處理cache login 沒檢查cache的事情

當然如果還是覺得有問題
也是能繼續用red 去連線,也只是讀入時要再額外編碼就是

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But @takidog think this problem login cache is server delay main reason in this weeks.
At same time v3 api is developing, and request will down in next week.
I prefer change in v3 api.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@takidog this problem need open issue to tracking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comment on that line to express your idea, otherwise others will get the same confused I got

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已補

url=os.environ['REDIS_URL'], db=2, charset="utf-8", decode_responses=True)


def dump_session_cookies(session,is_login):
def dump_session_cookies(session, is_login):
"""Dumps cookies to list
"""

Expand All @@ -47,13 +50,17 @@ def dump_session_cookies(session,is_login):
'domain': c.domain,
'value': c.value})

return {'is_login': is_login, 'cookies':cookies}
return {'is_login': is_login, 'cookies': cookies}


def login(username, password):
session = requests.Session()
is_login = {}

if red_auth.exists(username):
user_redis_cookies = red_auth.get(username)
return json.loads(user_redis_cookies)

# AP Login
try:
is_login["ap"] = ap.login(session, username, password)
Expand All @@ -72,15 +79,15 @@ def login(username, password):
is_login["leave"] = leave.login(session, username, password)
except:
is_login["leave"] = False
if is_login["ap"]:
return dump_session_cookies(session,is_login)
if is_login["ap"]:
return dump_session_cookies(session, is_login)
else:
return False
return False


def ap_query(session, qid=None, args=None,
username=None, expire=AP_QUERY_EXPIRE):
ap_query_key_tag = str(username) + str(args) + str(SECRET_KEY)
ap_query_key_tag = str(username) + str(args) + str(SECRET_KEY)
ap_query_key = qid + \
hashlib.sha512(
bytes(ap_query_key_tag, "utf-8")).hexdigest()
Expand All @@ -95,6 +102,7 @@ def ap_query(session, qid=None, args=None,

return ap_query_content


def leave_query(session, year="102", semester="2"):
return leave.getList(session, year, semester)

Expand Down Expand Up @@ -194,31 +202,30 @@ def get_semester_list():
"""

s = requests.Session()
ap.login(s,AP_GUEST_ACCOUNT, AP_GUEST_PASSWORD)
ap.login(s, AP_GUEST_ACCOUNT, AP_GUEST_PASSWORD)

content = ap_query(s, "ag304_01")
if len(content)<3000:
if len(content) < 3000:
return False
root = etree.HTML(content)

#options = root.xpath("id('yms_yms')/option")
try:
options = map(lambda x: {"value": x.values()[0].replace("#", ","),
"selected": 1 if "selected" in x.values() else 0,
"text": x.text},
root.xpath("id('yms_yms')/option")
)
"selected": 1 if "selected" in x.values() else 0,
"text": x.text},
root.xpath("id('yms_yms')/option")
)
except:
return False

options = list(options)

return options



if __name__ == "__main__":
s = requests.Session()
is_login = login(s, "guest", "123")

print(is_login)
print(is_login)
2 changes: 1 addition & 1 deletion src/kuas_api/modules/stateless_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def generate_auth_token(username, cookies, expiration=600):
"""
s = Serializer(DIRTY_SECRET_KEY, expires_in=expiration)

red.set(username, json.dumps(cookies))
red.set(username, json.dumps(cookies), ex=600)

return s.dumps({"sid": username})

Expand Down