Skip to content
TidB edited this page Oct 23, 2018 · 3 revisions

File objects are a subclass of Page objects, with a few extra functions specific to files.

Constructor

The constructor for File is identical to Page, minus the namespace option. If no "File:" prefix is in the title, it will be added automatically.

Methods

Most of the methods of the File class are the same as in Page, with a few additional ones.

getFileHistory

getFileHistory(exif=True, limit='all')

Gets the upload history of the file. This works similar, but is distinct from getHistory, which gets the history of the file description page (In previous versions of wikitools, this function was also named getHistory). limit sets the maximum number of revisions to return or "all" (default). If exif is True, it will get EXIF metadata. Returns a list of dicts of the form:

{'bitdepth': '8',
  'comment': 'Upload summary',
  'descriptionurl': 'https://commons.wikimedia.org/wiki/File:Filename.jpg',
  'height': 259,
  'mediatype': 'BITMAP',
  'metadata': [{'name': 'Orientation', 'value': 1}, # EXIF data, only included if exif=True
       {'name': 'XResolution', 'value': '2000000/10000'},
       {'name': 'YResolution', 'value': '2000000/10000'},
       # Actual metadata included will vary depending on the file
       {'name': 'MEDIAWIKI_EXIF_VERSION', 'value': 1}],
  'mime': 'image/jpeg',
  'sha1': 'c858353eed661f22be5d5e44b82c3e18388a25f9',
  'size': 50786,
  'timestamp': '2009-01-19T06:19:25Z',
  'url': 'https://upload.wikimedia.org/wikipedia/commons/3/31/Filename.jpg',
  'user': 'Username',
  'userid': '141409',
  'width': 555}

getFileHistoryGen

getFileHistoryGen(exif=True, limit='all')

The interface for this is the same as getFileHistory, except instead of loading the entire history into memory, it gets 1 version at a time and yields it as a generator function. This may be a better option for files with many versions or a lot of EXIF data.

getUsage

getUsage(titleonly=False, namespaces=None)

Gets a list of all the pages on the wiki that use the file. This only gets local uses of the file. Global usage of shared repository files is not currently supported. By default it will return Page objects. If you only want the titles, set titleonly to True. Can be limited to namespaces by passing an array of Namespaces or integer namespace numbers.

getUsageGen

getUsageGen(titleonly=False, namespaces=None)

Has the same options as getUsage, except it works as a generator function, retrieving a few results at a time and yielding individual Pages or titles.

download

download(width=False, height=False, location=False)

Downloads a copy of the file. You can specify width OR height to get a scaled version. location should include the path and/or filename to save to, if desired. Environment variables and ~ will be expanded. If a path isn't specified, it will use the current working directory. If a filename isn't specified, it will use the page title (excluding the "File:" prefix). Returns the location saved to (as a string).

upload

upload(fileobj=None, comment='', url=None, ignorewarnings=False, watch=False)

Uploads a file. You must pass either a fileobj (file object, open for reading) or a url (if the wiki supports uploading from other sites). comment is used as the upload log comment and will be the file description page text for new files. Set ignorewarnings to true to override warnings about duplicate files, etc. The watch parameter is deprecated in MediaWiki with version 1.17 and is used mostly for compatibility with older versions.

It will return the object from the API which will contain the image info if the upload succeeded, otherwise it will contain any warnings.

Private methods

  • __getFileHistoryInternal() - Does the API queries for getFileHistory/getFileHistoryGen
  • __getUsageInternal() - Does the API queries for getUsage/getUsageGen

Instance variables

Public

File has the same instance variables as Page

Examples

Download a copy of a file

from wikitools import wiki, wikifile
import os
site = wiki.Wiki("https://commons.wikimedia.org/w/api.php")
f = wikifile.File(site, "File:Swallow flying drinking.jpg")
loc = f.download(location='~/Test.jpg')
print(os.stat(loc).st_size)

outputs 3872029

Upload a file

from wikitools import wiki, wikifile
site = wiki.Wiki("https://www.mediawiki.org/w/api.php")
f = wikifile.File(site, "File:Test123.jpg")
if not f.exists:
    res = f.upload(fileobj = open('path/to/file.jpg', 'rb'), comment='This is a test upload')
    if res['upload']['result'] == 'Warning':
        # Handle warning
    elif res['upload']['result'] == 'Success':
        print("File uploaded")
Clone this wiki locally