Page MenuHomePhabricator

Flask Backend for Single Image Batch Upload
Closed, ResolvedPublic

Description

To receive calls from the front end (T161328: User Interface for Single Image Batch Upload) a Flask app can be used.

To start the Flask app should be callable by a link of the form: app/GLAM/ID, where "GLAM" can be any glam for which there is a metadata-mapping available and "ID" is the identifier of the file to retrieve from the GLAM.

Event Timeline

I was able to upload files from GLAM to commons using a modified https://github.com/basvb/single-image-batch-upload/blob/master/NationaalArchief.py.
Now I wish to call the upload method of this python script from the relevant method in my Flask app. My Flask app is running inside a virtual environment. So I installed pywikibot==2.0rc5 inside the venv where flask app is running. Now I replaced the latest files from the piwikibot repo. The specialbots.py file for uploading images is located at {myapp}/venv/lib/python2.7/site-packages/pywikibot. I don't know how to import that into my upload script.
Is there any other approach of using pywikibot from the venv where the Flask app resides? Need help.

So I installed pywikibot==2.0rc5 inside the venv where flask app is running.

Is it installed via pip?

Now I replaced the latest files from the piwikibot repo.

Try not to do a manual direct replacement, but something like pip install -U https://github.com/wikimedia/pywikibot-core/zipball/master#egg=pywikibot

The specialbots.py file for uploading images is located at {myapp}/venv/lib/python2.7/site-packages/pywikibot. I don't know how to import that into my upload script.

Have you tried import pywikibot and/or import pywikibot.specialbots? If so, does it work? If it doesn't work, any error message?

Is there any other approach of using pywikibot from the venv where the Flask app resides? Need help.

I don't think I get what you mean. In any case, I'll be available in IRC same time as yesterday.

in NationaalArchief.py I had some issues with pointing to pywikibot.specialbots as well. I used sys.path.append(YOURPATHHERE) in line 4 as a quick fix, although I believe fixing imports should ideally not be done like that.

Would it be possible to link to your code (on github?), that would make it easier for us to give pointers.

@zhuyifei1999, yes I installed via pip. But since I had done manual file replacement I now uninstalled via pip and tried to install again using the command suggested by you. But installation is not going through.
It gives the following error:
In /venv/local/lib/python2.7/site-packages/pip/util.py", line 216, in split_leading_dir

path = str(path)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128).
I read some solution to this. May be we can discuss in IRC.

@Basvb Sure. I will soon upload the code into github.

@Basvb, @zhuyifei1999, @tom29739 I have the preliminary code for uploading images based on GLAM name (only Nationaal Archief as of now) and a search string at https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload.

I will add further modifications and enhancements. Please give your constructive comment.

@Infobliss I think you left IRC. Anyways:

10:41 <zhuyifei1999_> if possible don't use specialbots.UploadRobot https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload/blob/master/NationaalArchief.py#L5
10:41 <zhuyifei1999_> too much cli interaction
10:41 <zhuyifei1999_> use the lower level site.upload
10:42 <zhuyifei1999_> https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload/blob/master/NationaalArchief.py#L14 these code are definitely not gonna pass flake8
10:43 <zhuyifei1999_> https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload/blob/master/app.py#L14 why both GET and POST?
10:43 <zhuyifei1999_> iirc request.form is for POST and you'll get 400 for GET
10:44 ⇐ infobliss quit Ping timeout: 260 seconds

https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload/blob/master/app.py#L19 Should raise exception instead and catch the exception when no match. In any case for... else... is better than using a temp variable
https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload/blob/master/app.py#L33 use either % or .format. Also the result is ambiguous (is the file actually uploaded or does it already exist?)
https://github.com/infobliss/Flask-Backend-for-Single-Image-Batch-Upload/blob/master/listOfGlams.py#L1 Why define a function when it only returns a constant?

@zhuyifei1999 thanks a lot for reviewing my code.

if possible don't use specialbots.UploadRobot

I tried using site.upload to upload a file from my local disk, but in https://github.com/wikimedia/pywikibot-core/blob/bc0149d6c3669c5613befb959610a3f0a630c41a/pywikibot/specialbots.py#L410 when I put the absolute path of the file as the second parameter and run, I got output from the following line

https://github.com/wikimedia/pywikibot-core/blob/bc0149d6c3669c5613befb959610a3f0a630c41a/pywikibot/specialbots.py#L171

I am not sure where I went wrong.

Upload/blob/master/app.py#L14 why both GET and POST? Should raise exception instead and catch the exception when no match. use either `%` or `.format`.

I corrected these.

Why define a function when it only returns a constant?

Just returning the constant now.

I got output from the following line https://github.com/wikimedia/pywikibot-core/blob/bc0149d6c3669c5613befb959610a3f0a630c41a/pywikibot/specialbots.py#L171 I am not sure where I went wrong.

site.upload won't call functions from specialbots, AFAIK. Do you mean https://github.com/wikimedia/pywikibot-core/blob/bc0149d6c3669c5613befb959610a3f0a630c41a/pywikibot/throttle.py#L244? That is perfectly normal process during pywikibot built-in throttling.

Thanks @zhuyifei1999 my bad. I had mistaken assuming the print statement is coming from specialbots.py. I uploaded the latest image here https://commons.wikimedia.org/w/index.php?title=Special:ListFiles/infobliss using site.upload.
Actually UploadRobot is uploading quicker for me.

@Infobliss Sorry I went away during that chat. For global user pages you may want to look at https://meta.wikimedia.org/wiki/Global_user_pages

@Basvb, @zhuyifei1999 as one of the microtasks I have complied a list of relevant websites/GLAMs to make metadata-mappings for. The list can be found here. It has been compiled referring to here. Please do let me know if any enhancement or modification is required.