Example #1
0
def drive_changelog(yesterday, html):
    """ Do something """
    drive = util.get_driveclient()

    start_change_id = util.CONFIG.get("changestamp", "1")

    html += """<p><table border="1" cellpadding="3" cellspacing="0">
<thead>
<tr><th>Changestamp</th><th>Time</th><th>Author</th><th>Resource</th></tr>
</thead>
<tbody>"""

    largestChangeId = -1
    hits = 0
    page_token = None
    while True:
        param = {}
        if start_change_id:
            param['startChangeId'] = start_change_id
        if page_token:
            param['pageToken'] = page_token
        print(("Requesting start_change_id: %s "
               "largestChangeId: %s page_token: %s"
               ) % (start_change_id, largestChangeId, page_token))
        response = drive.changes().list(**param).execute()
        largestChangeId = response['largestChangeId']
        page_token = response.get('nextPageToken')
        for item in response['items']:
            changestamp = item['id']
            if item['deleted']:
                continue
            modifiedDate = datetime.datetime.strptime(
                item['file']['modifiedDate'][:19], '%Y-%m-%dT%H:%M:%S')
            modifiedDate = modifiedDate.replace(tzinfo=pytz.timezone("UTC"))
            if modifiedDate < yesterday:
                continue
            uri = item['file']['alternateLink']
            title = item['file']['title']
            author = item['file']['lastModifyingUserName']
            localts = modifiedDate.astimezone(pytz.timezone("America/Chicago"))
            hits += 1
            html += """
<tr><td>%s</td><td>%s</td><td>%s</td><td><a href="%s">%s</a></td></tr>
            """ % (changestamp, localts.strftime("%-d %b %I:%M %P"),
                   author, uri, title)
        if not page_token:
            break

    util.CONFIG['changestamp'] = changestamp
    if hits == 0:
        html += """<tr><td colspan="4">No Changes Found...</td></tr>\n"""

    html += """</tbody></table>"""

    util.save_config()
    return html
Example #2
0
def drive_changelog(regime, yesterday, html):
    """ Do something """
    drive = util.get_driveclient()
    folders = util.get_folders(drive)
    start_change_id = util.CONFIG.get("changestamp_" + regime, "1")

    html += """<p><table border="1" cellpadding="3" cellspacing="0">
<thead>
<tr><th>Folder</th><th>Resource</th></tr>
</thead>
<tbody>"""

    largestChangeId = -1
    hits = 0
    page_token = None
    param = {"includeDeleted": False, "maxResults": 1000}
    while True:
        if start_change_id:
            param["startChangeId"] = start_change_id
        if page_token:
            param["pageToken"] = page_token
        print(
            ("[%s] start_change_id: %s largestChangeId: %s page_token: %s")
            % (regime, start_change_id, largestChangeId, page_token)
        )
        response = drive.changes().list(**param).execute()
        largestChangeId = response["largestChangeId"]
        page_token = response.get("nextPageToken")
        for item in response["items"]:
            changestamp = item["id"]
            if item["deleted"]:
                continue
            # don't do more work when this file actually did not change
            modifiedDate = datetime.datetime.strptime(item["file"]["modifiedDate"][:19], "%Y-%m-%dT%H:%M:%S")
            modifiedDate = modifiedDate.replace(tzinfo=pytz.timezone("UTC"))
            if modifiedDate < yesterday:
                continue
            # Need to see which base folder this file is in!
            isproject = False
            for parent in item["file"]["parents"]:
                if parent["id"] not in folders:
                    print(("[%s] file: %s has unknown parent: %s") % (regime, item["id"], parent["id"]))
                    continue
                if folders[parent["id"]]["basefolder"] == util.CONFIG[regime]["basefolder"]:
                    isproject = True
            if not isproject:
                print(("[%s] %s skipped") % (regime, repr(item["file"]["title"])))
                continue
            uri = item["file"]["alternateLink"]
            title = item["file"]["title"].encode("ascii", "ignore")
            localts = modifiedDate.astimezone(pytz.timezone("America/Chicago"))
            hits += 1
            pfolder = item["file"]["parents"][0]["id"]
            html += """
<tr>
<td><a href="https://docs.google.com/folderview?id=%s&usp=drivesdk">%s</a></td>
<td><a href="%s">%s</a></td></tr>
            """ % (
                pfolder,
                folders[pfolder]["title"],
                uri,
                title,
            )
            hit = False
            if "version" in item["file"] and item["file"]["mimeType"] != FMIME:
                lastmsg = ""
                try:
                    revisions = drive.revisions().list(fileId=item["file"]["id"]).execute()
                except:
                    print(("[%s] file %s (%s) failed revisions") % (regime, title, item["file"]["mimeType"]))
                    revisions = {"items": []}
                for item2 in revisions["items"]:
                    md = datetime.datetime.strptime(item2["modifiedDate"][:19], "%Y-%m-%dT%H:%M:%S")
                    md = md.replace(tzinfo=pytz.timezone("UTC"))
                    if md < yesterday:
                        continue
                    localts = md.astimezone(pytz.timezone("America/Chicago"))
                    if "lastModifyingUser" not in item2:
                        print(("[%s] file: %s has no User? %s") % (regime, title, item2))
                        continue
                    luser = item2["lastModifyingUser"]
                    hit = True
                    thismsg = """
    <tr><td colspan="2"><img src="%s" style="height:25px;"/> %s by
     %s (%s)</td></tr>
                    """ % (
                        (luser["picture"]["url"] if "picture" in luser else ""),
                        localts.strftime("%-d %b %-I:%M %p"),
                        luser["displayName"],
                        luser["emailAddress"],
                    )
                    if thismsg != lastmsg:
                        html += thismsg
                    lastmsg = thismsg
            # Now we check revisions
            if not hit:
                luser = item["file"]["lastModifyingUser"]
                html += """
<tr><td colspan="2"><img src="%s" style="height:25px;"/> %s by
 %s (%s)</td></tr>
                """ % (
                    luser["picture"]["url"] if "picture" in luser else "",
                    localts.strftime("%-d %b %-I:%M %p"),
                    luser["displayName"],
                    luser.get("emailAddress", "n/a"),
                )
        if not page_token:
            break

    util.CONFIG["changestamp_" + regime] = changestamp
    if hits == 0:
        html += """<tr><td colspan="5">No Changes Found...</td></tr>\n"""

    html += """</tbody></table>"""

    util.save_config()
    return html
import ConfigParser
import util  # @UnresolvedImport
import sys

config = ConfigParser.ConfigParser()
config.read('mytokens.cfg')

spr_client = util.get_spreadsheet_client(config)
drive = util.get_driveclient()

xref_plotids = util.get_xref_plotids(spr_client, config)

xref_feed = spr_client.get_list_feed(config.get('cscap', 'xrefrot'), 'od6')
rotations = {}
for entry in xref_feed.entry:
    data = entry.to_dict()
    rotations[data['code']] = data

# Build xref of cropmate with variables
cropmates = {}
xref_feed = spr_client.get_list_feed(config.get('cscap', 'xrefrotvars'), 'od6')
for entry in xref_feed.entry:
    data = entry.to_dict()
    res = cropmates.setdefault(data['cropmate'], [])
    res.append(data['variable'].lower())

res = drive.files().list(q="title contains 'Agronomic Data'").execute()

for item in res['items']:
    if item['mimeType'] != 'application/vnd.google-apps.spreadsheet':
        continue
Example #4
0
import util
import sys
import ConfigParser
import psycopg2

YEAR = sys.argv[1]

config = ConfigParser.ConfigParser()
config.read("mytokens.cfg")

pgconn = psycopg2.connect(database="sustainablecorn", host=config.get("database", "host"))
pcursor = pgconn.cursor()

# Get me a client, stat
spr_client = util.get_spreadsheet_client(config)
drive_client = util.get_driveclient()

res = (
    drive_client.files()
    .list(q=("title contains '%s'") % (("Soil Bulk Density and " "Water Retention Data"),))
    .execute()
)

DOMAIN = ["SOIL1", "SOIL2", "SOIL29", "SOIL30", "SOIL31", "SOIL32", "SOIL33", "SOIL34", "SOIL35", "SOIL39"]

for item in res["items"]:
    if item["mimeType"] != "application/vnd.google-apps.spreadsheet":
        continue
    try:
        spreadsheet = util.Spreadsheet(spr_client, item["id"])
    except Exception, exp:
Example #5
0
def drive_changelog(regime, yesterday, html):
    """ Do something """
    drive = util.get_driveclient()

    start_change_id = util.CONFIG.get("changestamp_"+regime, "1")

    html += """<p><table border="1" cellpadding="3" cellspacing="0">
<thead>
<tr><th>Changestamp</th><th>Time</th><th>Author</th><th>Resource</th></tr>
</thead>
<tbody>"""

    largestChangeId = -1
    hits = 0
    page_token = None
    while True:
        param = {}
        if start_change_id:
            param['startChangeId'] = start_change_id
        if page_token:
            param['pageToken'] = page_token
        print(("Requesting start_change_id: %s "
               "largestChangeId: %s page_token: %s"
               ) % (start_change_id, largestChangeId, page_token))
        response = drive.changes().list(**param).execute()
        largestChangeId = response['largestChangeId']
        page_token = response.get('nextPageToken')
        for item in response['items']:
            changestamp = item['id']
            if item['deleted']:
                continue
            # don't do more work when this file actually did not change
            modifiedDate = datetime.datetime.strptime(
                item['file']['modifiedDate'][:19], '%Y-%m-%dT%H:%M:%S')
            modifiedDate = modifiedDate.replace(tzinfo=pytz.timezone("UTC"))
            if modifiedDate < yesterday:
                continue
            # Need to see which base folder this file is in!
            parentid = get_base_folder_id(regime, drive, item['fileId'])
            if parentid != util.CONFIG[regime]['basefolder']:
                print(('Skipping %s as it is other project'
                       ) % (repr(item['file']['title']), ))
                continue
            uri = item['file']['alternateLink']
            title = item['file']['title'].encode('ascii', 'ignore')
            author = item['file']['lastModifyingUserName']
            localts = modifiedDate.astimezone(pytz.timezone("America/Chicago"))
            hits += 1
            html += """
<tr><td>%s</td><td>%s</td><td>%s</td><td><a href="%s">%s</a></td></tr>
            """ % (changestamp, localts.strftime("%-d %b %I:%M %P"),
                   author, uri, title)
        if not page_token:
            break

    util.CONFIG['changestamp_'+regime] = changestamp
    if hits == 0:
        html += """<tr><td colspan="4">No Changes Found...</td></tr>\n"""

    html += """</tbody></table>"""

    util.save_config()
    return html
Example #6
0
"""
Sync authorized users on Google Sites to Google Drive
"""
import gdata.docs.client
import gdata.docs.data
import gdata.acl.data
import gdata.data
import ConfigParser
import util

config = ConfigParser.ConfigParser()
config.read('mytokens.cfg')

spr_client = util.get_sites_client(config)
service = util.get_driveclient()

site_users = []
for acl in spr_client.get_acl_feed().entry:
    userid = acl.scope.value
    if userid not in site_users:
        site_users.append(acl.scope.value)

# Get a listing of current permissions
perms = service.permissions().list(fileId=config.get('cscap', 'folderkey')).execute()
for item in perms.get('items', []):
    email = item['emailAddress']
    if email in site_users:
        site_users.remove(email)
        continue
    print("Email: %s can access Drive, not sites" % (email,))