views:

18

answers:

1

I'm new to web app and I want to check when there's a new version of dota map, I'll check links in getdota.com. How can I do this and which language, I want it checks every time you start warcraft, and auto download new map to specific folder. My question is : Can you give a link to a specific article about web automation or something like that. Thanks first :)

+1  A: 

Below is an example in Python.

It parses getdota.com page, reads parameters for POST request for downloading a map, gets the file and saves it in configured directory (by default current directory).

#!/usr/bin/env python
import urllib
import urllib2
import sgmllib
from pprint import pprint
import os.path
import sys

url = 'http://www.getdota.com/'
download_url = 'http://www.getdota.com/app/getmap/'
chunk = 10000
directory = '' #directory where file should be saved, if empty uses current dir

class DotaParser(sgmllib.SGMLParser):
    def parse(self, s):
        self.feed(s)
        self.close()
    def __init__(self, verbose=0):
        sgmllib.SGMLParser.__init__(self, verbose)
        self.URL = ''
        self.post_args = {}
    def getArgs(self):
        return self.post_args
    def start_input(self, attributes):
        d = dict(attributes)
        if d.get('id', None) == None:
            return
        if d['id'] in ["input_mirror2", "input_file_name2", "input_map_id2", "input_language2", "input_language_id2"]:
            self.post_args[d['name']] = d['value']

if __name__ == '__main__':
    dotap = DotaParser()
    data = urllib2.urlopen(urllib2.Request('http://www.getdota.com/')).read()
    dotap.parse(data)
    data = urllib.urlencode(dotap.getArgs())
    request = urllib2.Request(download_url, data)
    response = urllib2.urlopen(request)
    page = response.read()

    #download file
    fname = directory + page.split('/')[-1]
    if os.path.isfile(fname):
        print "No newer file available"
        sys.exit(0)
    f = open(fname, 'w')
    print "New file available. Saving in: %s" % fname
    webFile = urllib.urlopen(page)
    c = webFile.read(chunk)
    while(c):
        f.write(c)
        c = webFile.read(chunk)
    f.close()
    webFile.close()
Paweł
thanks it's great to have an example code :) It's good to learn from somene's code
nXqd