LingDong- / wechit

WeChat in Terminal (微信终端版)
MIT License
89 stars 21 forks source link

The QR code is wierd #1

Closed egg-west closed 5 years ago

egg-west commented 5 years ago

Hi, Lingdong. Due to my server environment, I add chrome_options.add_argument("--remote-debugging-port=9222") to make chrome executable on my server. Then I run wechit. But the QR code is wierd, with most of the area black and only left-up corner is white.

egg-west commented 5 years ago

Actually, I run chrome with google-chrome --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 --user-data-dir and error shows but it still running. Then Error is

Fontconfig warning: "/etc/fonts/fonts.conf", line 86: unknown element "blank"
[1203/152116.043091:ERROR:gpu_process_transport_factory.cc(980)] Lost UI shared context.

DevTools listening on ws://127.0.0.1:9222/devtools/browser/5a00cf75-029a-41b5-9286-e9e131a3cfd9

Do you know a better way to run it?

LingDong- commented 5 years ago

Hi egg-west,

I haven't tried my project remotely myself, but here's a guess:

First, this is how the program extract the QR code:

Therefore my guess is that your invisible Chrome window is too small, and most of the QR code is outside of view, and during the cropping step the invisible area are simply filled black. Normally this won't happen because by default Chrome starts at a reasonably large dimension, but on your server maybe the screen resolution is different, etc.

(I simulated the situation I in the screenshot below)

screen shot 2018-12-04 at 2 01 21 am

I can write up with a solution such as resizing the window automatically for users when I have time. But if it is possible, maybe you can remove the headless flag, and actually see the Chrome window and how the QR code looks in it?

Thanks

egg-west commented 5 years ago

Hi,

If I remove the headless flag, with command google-chrome --disable-gpu --no-sandbox --remote-debugging-port=9222 This Error occurs:

(google-chrome:9913): Gtk-WARNING **: cannot open display: 
[root@iZwz9hwnc0ylklzm48dmm7Z ~]# [1204/152143.602286:ERROR:nacl_helper_linux.cc(310)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly

Thanks

LingDong- commented 5 years ago

Since cannot open display, maybe you need to have X11 forwarding etc. on your server and use ssh -Y to view the GUI?

I’m so sorry but I’m not very familiar with using Chrome remotely. Probably you’ll be able to find more help on chromedriver’s site or selenium’s site: https://github.com/SeleniumHQ/selenium or http://chromedriver.chromium.org ?

Thanks

egg-west commented 5 years ago

I think wechit is cool if I can use it without any display devices. Or if I have display devices, I would like to use web version of wechat.

Thanks.

LingDong- commented 5 years ago

I'm suggesting displaying the GUI because that just makes it easier to track down the problem. Once things are working correctly, there should be no need to show the GUI.

But back to your first question about how only one corner of QR code shows up. try putting this line in init_driver in wechit.py: driver.set_window_size(1000,1000); It makes your window sufficiently large, so the QR code should be visible to the program

egg-west commented 5 years ago

I get more information. The QR code I got is like this image

I have not succeeded to make it show the complete QR code by change the window in any size. but in `temp/login-screen.png', I found the complete QR code picture.

Do you have any idea?

Thank you so much.

LingDong- commented 5 years ago

I think we're almost there! From your screenshot and description, it seems the problem is that during the step where the screenshot is cropped, the bounding box information returned by selenium is inaccurate. (Maybe a display resolution issue)

Could you please try print(rect) after line 212 of wechit.py. It should print the coordinates (x0, y0, x1, y1) that indicates the upper left and lower right corner of the QR code in the screenshot. Compare it with the actual bounding box in the imagetemp/login-screen.png, are they different?

For me, when the window size is driver.set_window_size(1000, 1000), the correct bounding box is (730, 422, 1270, 962) for non-headless mode and (730, 544, 1270, 1084) for headless mode. So maybe you can try hardcoding these values (or what you measure from temp/login-screen.png) on line 217, e.g.

im = Image.open(LOGIN_SCREEN_FILE).crop((730, 544, 1270, 1084))

The above should solve the problem, but if the problem persists then could you please try adding im.save("temp/qr.png") right after line 217. This should save an image containing only the QR code to the ./temp folder. Then compare it to temp/login-screen.png to see which part is being cropped.

Thanks, and I'm so sorry if this has been a frustrating experience for you.

egg-west commented 5 years ago

Thank you so much for your patiently reply.

Tried it but not work. I finally tune the size myself and after a long time exploring I made it show the whole QR code. Then I scan it and accept. But nothing happens. Is this normal? Should I write scrip to control what will happen after this?

Thanks again.

LingDong- commented 5 years ago

Normally, right after you scan the QR code, it will print "logged in as \<your name>! loading chats..." and then print a list of recent conversations.

But before your conversations can be fetched, it first need to wait for the page to load. My code checks if the page is loaded by seeing if your username is visible on page. So it is possible that your username contains weird characters that confuses python. So it kept thinking that your username is not loaded and kept waiting for it. If that's the case, change wait_for_chat_window on line 256 to something like this:

def wait_for_chat_window(driver):
    prompt = "press a key after scanning..."
    if IS_PYTHON3:
        return input(prompt)
    else:
        return raw_input(prompt)

So you'll just hit a key manually after scanning the QR code to proceed.

Another possible cause is the unlikely case that you really have no conversations, and my code assumes that you have and they're just not loaded yet, so it keeps waiting. If that's the case, change while True: on line 295 to for i in range(10):.

I know the logic behind these code sounds horrible but I'll improve them next time I update the project. :P Thanks.

egg-west commented 5 years ago

This QR problem is solved so you may close it freely. @LingDong-

JakeTompkins commented 5 years ago

Hey, I'm having what appears to be the same problem, though I'm not on a server. Is hard-coding the bounding box the only way to solve it still?