Skip to content

Commit

Permalink
userinfo cache and automatic login
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Aug 9, 2015
1 parent 6adf551 commit 7ddf578
Show file tree
Hide file tree
Showing 22 changed files with 304 additions and 251 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ app.command

cookie*
.coverage

# user data
data/*.json
cache/*.json
Empty file added cache/README
Empty file.
1 change: 0 additions & 1 deletion cache/readme.md

This file was deleted.

Empty file added data/README
Empty file.
40 changes: 0 additions & 40 deletions data/help.html

This file was deleted.

25 changes: 0 additions & 25 deletions src/api.py

This file was deleted.

17 changes: 14 additions & 3 deletions src/base/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import platform
import asyncio
from base.logger import LOG


import json

from base.logger import LOG


def singleton(cls, *args, **kw):
Expand All @@ -31,6 +30,18 @@ def wrapper(*args, **kwargs):
return wrapper


def write_json_into_file(data_json, filepath):
try:
with open(filepath, "w") as f:
data_str = json.dumps(data_json, indent=4)
f.write(data_str)
return True
except Exception as e:
LOG.error(str(e))
LOG.error("Write json into file failed")
return False


def judge_system():
sys_info = platform.system()
return sys_info
Expand Down
2 changes: 1 addition & 1 deletion src/base/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET
if constants.MODE == constants.DEBUG:
logging.basicConfig(
level=logging.DEBUG,
level=logging.INFO,
format="[%(levelname)s] [%(filename)s line:%(lineno)d] : %(message)s"
)
else:
Expand Down
6 changes: 3 additions & 3 deletions src/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, data, type=None):
def get_model_type(self):
return self.type

def get_model(self):
def get_dict(self):
self.data_model['code'] = 200
return self.data_model

Expand All @@ -38,8 +38,8 @@ def validate(self):

def __getitem__(self, item):
# 没有实现setitem, 暂时没用到这个函数,以后可能废弃
if item in self.get_model():
return self.get_model()[item]
if item in self.get_dict():
return self.get_dict()[item]
else:
return None

Expand Down
1 change: 0 additions & 1 deletion src/base/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def wrapper(self, *args, **kwargs):
if self.__playlist.isEmpty():
self.signal_playlist_is_empty.emit()
return
print(self, *args)
return func(self, *args, **kwargs)
return wrapper

Expand Down
76 changes: 1 addition & 75 deletions src/base/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,8 @@ class MyWeb(QObject):
def __init__(self):
super().__init__()
self.headers = {
'Host': 'music.163.com',
'Connection': 'keep-alive',
'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
'Referer': 'http://music.163.com/',
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36"
}

self.cookies = dict(appver="1.2.1", os="osx")

def post(self, url, data):
"""Load data from the server using a HTTP POST request.
:param string posturl: the URL to which the request is sent.
:param dict dictdata: a dict object that is sent to the server with the request.
"""


try:
res = requests.post(url, data=data, headers=self.headers, cookies=self.cookies)
content = self.show_progress(res)
return content
except Exception as e:
LOG.error(str(e))
return b'{"code": 408}'
}

def post_and_updatecookies(self, url, data):
try:
Expand All @@ -56,55 +34,3 @@ def post_and_updatecookies(self, url, data):
except Exception as e:
LOG.error(str(e))
return {"code": 408}

def get(self, url):
"""Load data from the server using a HTTP GET request.
:param url: the URL to which the request is sent.
:return content: return HTTPResponse Objects, generally speaking, we use READ method.
"""
try:
res = requests.get(url, headers=self.headers)
content = self.show_progress(res)
return content
except Exception as e:
LOG.error(str(e))
return b'{"code": 408}'

def show_progress(self, response):
content = bytes()
total_size = response.headers.get('content-length')
if total_size is None:
LOG.info(u'这个网络response没有Content-Length字段')
content = response.content
return content
else:
total_size = int(total_size)
bytes_so_far = 0

for chunk in response.iter_content():
content += chunk
bytes_so_far += len(chunk)
progress = round(bytes_so_far * 1.0 / total_size * 100)
self.signal_load_progress.emit(progress)
return content

@func_coroutine
def save_cookies(self):
try:
with open(DATA_PATH + "cookies.dat", "wb") as f:
pickle.dump(self.cookies, f)
return True
except Exception as e:
LOG.error(str(e))
return False

def load_cookies(self):
try:
with open(DATA_PATH + "cookies.dat", "rb") as f:
self.cookies = pickle.load(f)
requests.session().cookies = self.cookies
return True
except Exception as e:
LOG.error(str(e))
return False

0 comments on commit 7ddf578

Please sign in to comment.