b-twice / arcgis-rest-toolbox

Set of tools for managing ArcGIS REST Services
7 stars 6 forks source link

Replicate Service - Script #2

Closed trillevine closed 9 years ago

trillevine commented 10 years ago

Hi Brian,

I put together this script to replicate a service based on the code I posted in the Issue 1 thread. It's just a script, so it can be run independently of the ArcGIS environment. It was written for Python 2.6, but I imagine it should work for 2.7 as well, since there are no 2.6-specific lines that look like this in there:

'query?where={0}&returnIdsOnly=true&' 'f=json&token={1}'

It's made to create daily backups, so it checks for a folder from the previous day, deletes it, and creates a new folder for the current day.

Feel free to post it in the repo.

import json, urllib, urllib2 import sys, os import shutil import time, datetime from datetime import date, timedelta import zipfile

today = date.today() todayString = today.strftime("%Y.%m.%d") serviceTodayString = 'WAYS_RiverbankAcquisition'+ todayString yesterday = date.today() - timedelta(1) yesterdayString = yesterday.strftime('%Y.%m.%d') serviceYesterdayString = r'G:\Dvkoord\GIS\TEMP\Tle\Scripts\WAYS_RiverbankAcquisition'+ yesterdayString

class GetToken(object): def urlopen(self, url, data=None): referer = "http://www.arcgis.com/arcgis/rest" req = urllib2.Request(url) req.add_header('Referer', referer) if data: response = urllib2.urlopen(req, data) else: response = urllib2.urlopen(req) return response

def gentoken(self, username, password,
    referer = 'www.arcgis.com', expiration=60):
    query_dict = {'username': username,
        'password': password,
        'expiration': str(expiration),
        'client': 'referer',
        'referer': referer,
        'f': 'json'}
    query_string = urllib.urlencode(query_dict)
    token_url = "https://www.arcgis.com/sharing/rest/generateToken"
    token_response = urllib.urlopen(token_url, query_string)
    token = json.loads(token_response.read())
    if "token" not in token:
        print token['messages']
        exit()
    else:
        return token['token']

class QueryService (object): def init (self, service_query, token, service_url): self.service_url = os.path.split(service_url)[0][:-1] + "createReplica" self.token = token self.service_query = service_query

def generateJSON (self):
    self.service_query["token"] = self.token
    service_data = urllib.urlencode(self.service_query)
    request = urllib2.Request(self.service_url, service_data)
    response = json.loads(urllib2.urlopen(request).read())
    return response

def replicateFS (self):
    fs_url = self.generateJSON()['responseUrl']
    req = urllib2.Request(fs_url)
    response = urllib2.urlopen(req)
    output = open(serviceTodayString + '.zip', 'wb')
    output.write(response.read())
    output.close()
    zipped = zipfile.ZipFile(output.name)
    unzipped = zipped.extractall(serviceTodayString)

class DownloadService(GetToken): def init(self, fs, field, destination, user, pw, service_query): self.fs = fs +'/' self.field = field self.where = '1=1' self.token = self.gentoken(user, pw) self.destination = os.path.join(destination, serviceTodayString) self.service_query = service_query

def jsonload(self, url, query):
    return json.loads(urllib.urlopen(url + query).read())

def pullService(self):
    if serviceYesterdayString:
        shutil.rmtree(serviceYesterdayString)
        os.makedirs(self.destination + '\\Geodaten')
    os.chdir(self.destination + '\\Geodaten')
    self.queryservice()

def queryservice(self):
    run = QueryService(self.service_query, self.token, self.fs)
    run.replicateFS()

def main(): service_url = 'http://services1.arcgis.com/0cr41EdkajvOA232/ArcGIS/rest/services//FeatureServer/0' username = "username" password = "password" directory = r'G:\Dvkoord\GIS\TEMP\Tle\Scripts' field_name = '' service_query = { "geometry": '', "geometryType": "esriGeometryEnvelope", "inSR": '', "layerQueries": '', "layers": "0, 1, 2", "replicaName": "read_only_rep", "returnAttachments": 'true', "returnAttachmentsDataByUrl": 'true', "transportType": "esriTransportTypeEmbedded", "async": 'false', "syncModel": "none", "dataFormat": "filegdb", "token": '', "replicaOptions": '', "f": "json" } run = DownloadService(service_url, field_name, directory, username, password, service_query) run.pullService()

if name == 'main': main()