kelciour / batch-download-pictures-from-google-images

7 stars 5 forks source link

No images #3

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi. I selected "Source Field" and selected "Target Field" instead of "". Then I hit "Start". A message appeared: "Processed 1 note", but I have no images at all. I have Anki 2.1.35 Mac OS

kelciour commented 3 years ago

It's still working fine for me, but maybe Google is rolling out a new update and changed how pictures were stored in the html file.

Since I can't reproduce it, maybe open the Debug Console, copy-paste the following code, press Ctrl+Enter and attach google-images.log.txt that will be saved in your Desktop folder in a new message. The output will be something like

Found 0 pictures. C:\Users\Nickolay\Desktop\google-images.log.txt

import requests

query = "tabby cat"

headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36" }

r = requests.get("https://www.google.com/search?tbm=isch&q={}&safe=active".format(query), headers=headers, timeout=15)

r.raise_for_status()

log_file = os.path.join(os.path.expanduser('~'), 'Desktop', 'google-images.log.txt')

with open(log_file, 'wb') as f_log:
    f_log.write(r.content)

# -----------------------

import json
import re
from bs4 import BeautifulSoup

soup = BeautifulSoup(r.text, "html.parser")

rg_meta = soup.find_all("div", {"class": "rg_meta"})
metadata = [json.loads(e.text) for e in rg_meta]
results = [d["ou"] for d in metadata]

if not results:
    regex = re.escape("AF_initDataCallback({")
    regex += r'[^<]*?data:[^<]*?' + r'(\[[^<]+\])'

    for txt in re.findall(regex, r.text):
        data = json.loads(txt)

        try:
            for d in data[31][0][12][2]:
                try:
                    results.append(d[1][3][0])
                except Exception as e:
                    pass
        except Exception as e:
            pass

# -----------------------
print()
print('Found {} pictures.'.format(len(results)))
print(log_file)
ghost commented 3 years ago

I did it. Found 100 pictures. Do I need to send it? I have 2 fields: "Front" and "Back" and I want a picture in the "Front". Do I have to select "Source Field" and "Target Field" as "Front"?

kelciour commented 3 years ago

It looks like the add-on is working.

Yes, it should be possible to use "Front" both as "Source Field" and "Target Field", but use "Append" instead of "Skip".

It might be better to add a new field for pictures by clicking on Fields in the editor window and after adding it, clicking on Cards in the editor window to add the same field to the front or back template to make it show up on cards. After that this field can be used with the add-on.

ghost commented 3 years ago

Yeah, it works) The reason was "use "Append" instead of "Skip". Thanks a jillion)