Skip to content

Instantly share code, notes, and snippets.

@streetgt
Last active October 2, 2016 23:31
Show Gist options
  • Save streetgt/9c7390707aa8b4d09243821d531aa396 to your computer and use it in GitHub Desktop.
Save streetgt/9c7390707aa8b4d09243821d531aa396 to your computer and use it in GitHub Desktop.
MediaWiki File Upload Bot - python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python bot for upload files in MediaWiki, originaly created for use at GTA Network.
# Tiago 'StreetGT' Cardoso (tiagocardosoweb@gmail.com)
# For use, you need to create a folder called "files" and add the files inside to be upload.
# Should be on same folder root from the script, or won't work.
import os
import numpy
from selenium import webdriver
def login(username,password):
# Browser
driver = webdriver.Chrome()
driver.get('Login Page here: https://example.com/index.php?title=Special:UserLogin&returnto=Main+Page')
# log in
driver.find_element_by_id('wpName1').send_keys(username)
driver.find_element_by_id('wpPassword1').send_keys(password)
driver.find_element_by_id('wpLoginAttempt').click()
return driver
def upload_file(driver,files):
# Check if there still urls
if len( files ) == 0:
print 'Upload Files Bot: Finished!'
driver.close()
# Visit Upload Page
driver.get("Upload page here: https://example.com/index.php?title=Special:Upload")
# Pop a URL from the array
file_path = files.pop()
# Click on select file and send file relative path
driver.find_element_by_id('wpUploadFile').send_keys(os.getcwd()+ "/" + file_path)
# Click on upload file
driver.find_element_by_name('wpUpload').click()
# Lets wait a bit
r = numpy.random.randint(2,5)
time.sleep(r)
# Information about how many left
print 'MediaWiki Upload Files Bot: ' + str(len( files )) + ' files left!'
# Recursive
return upload_file(driver, files)
if __name__ == '__main__':
# Credentials
username = 'XXXXXXX'
password = 'XXXXXX'
# Files Names
files = []
# List files name in files Directory
for file in os.listdir(os.getcwd()+"/files"):
files.append("files/" + file)
# Sort the files to start from A to Z
files.sort(reverse=True)
# Login in Wiki
driver = login(username, password)
# Upload files
upload_file(driver,files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment