Esempio n. 1
0
 def restore_cookies(self):
     if os.path.exists(self.cookie_path):
         with open(self.cookie_path, 'r') as f:
             lines = ''
             for line in f:
                 lines = lines + line
                 allCookies = QNetworkCookie.parseCookies(lines)
                 QNetworkCookieJar.setAllCookies(self, allCookies)
Esempio n. 2
0
class LoginWindow(QWebEngineView):
    def __init__(self):
        super().__init__()
        self.setWindowIcon(QIcon(app_icon))
        self.setWindowTitle('login')
        self.loadFinished.connect(self.process_load_finish)
        self.main_window = None
        self.is_login = False
        cookie_store = self.page().profile().cookieStore()
        print(cookie_store)
        self.cookiejar = QNetworkCookieJar()
        cookie_store.cookieAdded.connect(self.cookie_added)
        self.login_on_startup()

    def set_main_window(self, main_window):
        self.main_window = main_window

    def show(self):
        self.load(QUrl(longzhu_login_url))
        super().show()

    def process_load_finish(self):
        if self.url().host() == 'www.longzhu.com' and self.url().path() == '/':
            print('after login')
            self.page().toHtml(self.login)

    def login(self, data):
        try:
            self.login_html = data
            user_menu = pq(data)('#topbar-user-menu')
            self.level = user_menu.find('i.user-lv').attr['class'].split(
                '-')[-1]
            self.uid = user_menu.find(
                'a.report-rbi-click').attr['data-label'].split(':')[-1]
            self.username = user_menu.find('span.topbar-username').text()
            self.is_login = True
            print(self.level)
            print(self.uid)
            print(self.username)
            for cookie in self.cookiejar.cookiesForUrl(
                    QUrl(longzhu_login_url)):
                print('name = {}, value = {}'.format(
                    str(cookie.name(), encoding='ascii'),
                    str(cookie.value(), encoding='ascii')))
            self.main_window.login(self.username, self.level)
            self.load(QUrl(''))
            self.hide()
            self.save_cookie()
        except:
            pass

    def logout(self):
        self.level = ''
        self.uid = ''
        self.username = ''
        self.is_login = False
        self.cookiejar.setAllCookies([])
        self.page().profile().cookieStore().deleteAllCookies()
        self.delete_cookie()

    def login_on_startup(self):
        if self.load_cookie():
            self.load(QUrl('http://www.longzhu.com'))

    def cookie_added(self, cookie):
        if cookie.name() == b'p1u_id' or cookie.name() == b'PLULOGINSESSID':
            print('name = {}, domain = {}, value = {}, path = {}, date = {}'.
                  format(cookie.name(), cookie.domain(), cookie.value(),
                         cookie.path(),
                         cookie.expirationDate().date()))
        self.cookiejar.setCookiesFromUrl([cookie], QUrl(longzhu_login_url))

    def save_cookie(self):
        with open(cookie_file, 'wb') as f:
            for cookie in self.cookiejar.allCookies():
                f.write(cookie.toRawForm() + b'\n')

    def delete_cookie(self):
        try:
            os.remove(cookie_file)
        except OSError:
            pass

    def load_cookie(self):
        cookies = []
        if os.path.isfile(cookie_file):
            with open(cookie_file, 'rb') as f:
                for line in f:
                    cookie = QNetworkCookie.parseCookies(line)
                    self.cookiejar.setCookiesFromUrl(cookie,
                                                     QUrl(longzhu_login_url))
                    self.page().profile().cookieStore().setCookie(cookie[0])
                return True
        return False
Esempio n. 3
0
 def setAllCookies(self, cookie_list, network_cookie=None):
     QNetworkCookieJar.setAllCookies(self, cookie_list, network_cookie)