登录Github,获取头像
| 30 | |
| 31 | |
| 32 | class LoginThread(QObject): |
| 33 | """登录Github,获取头像 |
| 34 | """ |
| 35 | |
| 36 | Url = 'https://api.github.com/user' |
| 37 | |
| 38 | def __init__(self, account, password, *args, **kwargs): |
| 39 | super(LoginThread, self).__init__(*args, **kwargs) |
| 40 | self.account = account |
| 41 | self.password = password |
| 42 | |
| 43 | @classmethod |
| 44 | def quit(cls): |
| 45 | """退出线程 |
| 46 | :param cls: |
| 47 | """ |
| 48 | if hasattr(cls, '_thread'): |
| 49 | cls._thread.quit() |
| 50 | AppLog.info('login thread quit') |
| 51 | |
| 52 | @classmethod |
| 53 | def start(cls, account, password, parent=None): |
| 54 | """启动登录线程 |
| 55 | :param cls: |
| 56 | :param account: 账号 |
| 57 | :param password: 密码 |
| 58 | """ |
| 59 | cls._thread = QThread(parent) |
| 60 | cls._worker = LoginThread(account, password) |
| 61 | cls._worker.moveToThread(cls._thread) |
| 62 | cls._thread.started.connect(cls._worker.run) |
| 63 | cls._thread.finished.connect(cls._worker.deleteLater) |
| 64 | cls._thread.start() |
| 65 | AppLog.info('login thread started') |
| 66 | |
| 67 | def get_avatar(self, uid, avatar_url): |
| 68 | try: |
| 69 | req = requests.get(avatar_url) |
| 70 | if req.status_code == 200: |
| 71 | imgformat = req.headers.get('content-type', |
| 72 | 'image/jpg').split('/')[1] |
| 73 | Constants.ImageAvatar = os.path.join( |
| 74 | Constants.ImageDir, str(uid)).replace('\\', '/') + '.jpg' |
| 75 | AppLog.debug('image type: {}'.format(imgformat)) |
| 76 | AppLog.debug('content length: {}'.format(len(req.content))) |
| 77 | |
| 78 | image = QImage() |
| 79 | if image.loadFromData(req.content): |
| 80 | # 缩放图片 |
| 81 | if not image.isNull(): |
| 82 | image = image.scaled(130, 130, Qt.IgnoreAspectRatio, |
| 83 | Qt.SmoothTransformation) |
| 84 | AppLog.debug('save to: {}'.format( |
| 85 | Constants.ImageAvatar)) |
| 86 | image.save(Constants.ImageAvatar) |
| 87 | else: |
| 88 | AppLog.warn('avatar image is null') |
| 89 | else: |