Created
February 25, 2015 04:46
-
-
Save crorella/13cb7eea70dbe335ef25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = 'cristiano' | |
| # Update addons from curseforge | |
| from bs4 import BeautifulSoup | |
| import urllib2 | |
| import urllib | |
| import tempfile | |
| from os import path, makedirs | |
| from shutil import move, rmtree | |
| import zipfile | |
| class AddOnUpdater(): | |
| def __init__(self, url): | |
| if not url: | |
| print ("Error - No URL was provided") | |
| self.url = url | |
| self.base_url = self.url.split("/")[2] | |
| self.addon_path = '/Applications/World of Warcraft/Interface/AddOns' | |
| def get_addon_url(self): | |
| parser = BeautifulSoup(urllib2.urlopen(self.url)) | |
| urls = parser.select("li.user-action-download > a") | |
| download_page = BeautifulSoup(urllib2.urlopen("http://" + self.base_url + urls[0].attrs['href'])) | |
| download_info = download_page.select("li.user-action-download > span > a") | |
| download_url = download_info[0].attrs['href'] | |
| self.download_url = download_url | |
| def download_file(self): | |
| if self.download_url.endswith(".zip"): | |
| file_name = self.download_url.split("/")[-1] | |
| self.temp_file = path.join(tempfile.gettempdir(), file_name) | |
| print "Downloading %s" % file_name | |
| #print file_name | |
| urllib.urlretrieve(self.download_url, self.temp_file) | |
| #return | |
| def extract_file(self): | |
| # TODO - Create move_file and split the functionality | |
| zfile = zipfile.ZipFile(self.temp_file) | |
| for name in zfile.namelist(): | |
| #(dirname, filename) = path.split(name) | |
| # create root folder | |
| # dump all the content from that folder to final folder | |
| # print name | |
| #print "Decompressing " + filename + " on " + dirname | |
| # if not path.exists(path.dirname(self.temp_file)): | |
| # makedirs(path.dirname(self.temp_file)) | |
| #zfile.extract(name, path.dirname(self.temp_file)) | |
| #print path.dirname(self.temp_file) | |
| zfile.extract(name, self.addon_path) | |
| # root_folder = name.split("/")[0] | |
| # origin_folder = path.join(path.dirname(self.temp_file), root_folder) | |
| # destination_folder = path.join(self.addon_path, root_folder) | |
| # if path.exists(destination_folder): | |
| # # TODO create a backup of the old files | |
| # rmtree(destination_folder) | |
| # | |
| # move(origin_folder, destination_folder) | |
| # | |
| with file("/Applications/World of Warcraft/Interface/AddOns/list.txt") as f: | |
| for line in f: | |
| if line[0] != '#': | |
| au = AddOnUpdater(line) | |
| au.get_addon_url() | |
| au.download_file() | |
| au.extract_file() | |
| # au = AddOnUpdater("http://wow.curseforge.com/addons/pettracker") | |
| # au.get_addon_url() | |
| # au.download_file() | |
| # au.extract_file() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment