{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sys.path.append('/Users/joel/src/thundr/tracker')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tracker import sheet\n",
    "from tracker import config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def init():\n",
    "    from google.auth.transport.requests import AuthorizedSession\n",
    "    from google.oauth2 import service_account\n",
    "    import httplib2\n",
    "    from googleapiclient.discovery import build\n",
    "    service_account_info = config.google_auth_dict\n",
    "    credentials = (\n",
    "        service_account.Credentials.from_service_account_info(service_account_info)\n",
    "            .with_scopes(['https://www.googleapis.com/auth/drive.file']))\n",
    "    http = credentials.authorize(httplib2.Http())\n",
    "    return build(\"files\", \"v2\", http)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from oauth2client.service_account import ServiceAccountCredentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ServiceAccountCredentials?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import google_auth_httplib2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.oauth2 import service_account\n",
    "\n",
    "SCOPES = ['https://www.googleapis.com/auth/drive']\n",
    "SERVICE_ACCOUNT_FILE = \"/Users/joel/Downloads/whtlst-158518-33d98d2ce31a.json\"\n",
    "\n",
    "credentials = service_account.Credentials.from_service_account_file(\n",
    "        SERVICE_ACCOUNT_FILE, scopes=SCOPES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "import googleapiclient.discovery\n",
    "\n",
    "ss = googleapiclient.discovery.build('drive', 'v2', credentials=credentials)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = ss.files()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.insert?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def init2():\n",
    "    from google.auth.transport.requests import AuthorizedSession\n",
    "    from google.oauth2 import service_account\n",
    "    import gspread\n",
    "    service_account_info = config.google_auth_dict\n",
    "    credentials = (\n",
    "        service_account.Credentials.from_service_account_info(service_account_info)\n",
    "            .with_scopes(['https://spreadsheets.google.com/feeds']))\n",
    "\n",
    "    client = gspread.Client(auth=credentials)\n",
    "    client.session = AuthorizedSession(credentials)\n",
    "    return client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "urls.construct_url('spreadsheets')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = c.session.post('https://www.googleapis.com/upload/drive/v2/files', )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "urls.construct_url(\n",
    "            'worksheet', visibility='private', projection='full', spreadsheet_id=1, worksheet_id=1, worksheet_version=3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from apiclient import errors\n",
    "from apiclient.http import MediaFileUpload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def insert_file(\n",
    "    service, filename, title, description='A deal', parent_id=None, \n",
    "    mime_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document'):\n",
    "    \n",
    "  \"\"\"Insert new file.\n",
    "  'application/vnd.google-apps.drive-sdk'\n",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document\n",
    "'application/vnd.google-apps.document'\n",
    "\n",
    "  Args:\n",
    "    service: Drive API service instance.\n",
    "    title: Title of the file to insert, including the extension.\n",
    "    description: Description of the file to insert.\n",
    "    parent_id: Parent folder's ID.\n",
    "    mime_type: MIME type of the file to insert.\n",
    "    filename: Filename of the file to insert.\n",
    "  Returns:\n",
    "    Inserted file metadata if successful, None otherwise.\n",
    "  \"\"\"\n",
    "  media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)\n",
    "  body = {\n",
    "    'title': title,\n",
    "    'description': description,\n",
    "    'mimeType': mime_type,\n",
    "  }\n",
    "  # Set the parent folder.\n",
    "  if parent_id:\n",
    "    body['parents'] = [{'id': parent_id}]\n",
    "\n",
    "  try:\n",
    "    file = service.files().insert(\n",
    "        body=body,\n",
    "        media_body=media_body, convert=True).execute()\n",
    "\n",
    "    # Uncomment the following line to print the File ID\n",
    "    # print 'File ID: %s' % file['id']\n",
    "\n",
    "    return file\n",
    "  except errors.HttpError as error:\n",
    "    print('An error occurred: %s' % error)\n",
    "    return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'alternateLink': 'https://docs.google.com/document/d/1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY/edit?usp=drivesdk',\n",
       " 'appDataContents': False,\n",
       " 'capabilities': {'canCopy': True, 'canEdit': True},\n",
       " 'copyable': True,\n",
       " 'createdDate': '2018-06-12T13:04:14.678Z',\n",
       " 'description': 'A deal',\n",
       " 'editable': True,\n",
       " 'embedLink': 'https://docs.google.com/document/d/1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY/preview?ouid=103809770721073131653',\n",
       " 'etag': '\"HPeBWzrNkWqp0rz2LrVPmfIGOTs/MTUyODgwODY1NDc1Nw\"',\n",
       " 'explicitlyTrashed': False,\n",
       " 'exportLinks': {'application/epub+zip': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=epub',\n",
       "  'application/pdf': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=pdf',\n",
       "  'application/rtf': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=rtf',\n",
       "  'application/vnd.oasis.opendocument.text': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=odt',\n",
       "  'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=docx',\n",
       "  'application/zip': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=zip',\n",
       "  'text/html': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=html',\n",
       "  'text/plain': 'https://docs.google.com/feeds/download/documents/export/Export?id=1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY&exportFormat=txt'},\n",
       " 'iconLink': 'https://drive-thirdparty.googleusercontent.com/16/type/application/vnd.google-apps.document',\n",
       " 'id': '1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY',\n",
       " 'kind': 'drive#file',\n",
       " 'labels': {'hidden': False,\n",
       "  'restricted': False,\n",
       "  'starred': False,\n",
       "  'trashed': False,\n",
       "  'viewed': True},\n",
       " 'lastModifyingUser': {'displayName': 'cerebro@whtlst-158518.iam.gserviceaccount.com',\n",
       "  'emailAddress': 'cerebro@whtlst-158518.iam.gserviceaccount.com',\n",
       "  'isAuthenticatedUser': True,\n",
       "  'kind': 'drive#user',\n",
       "  'permissionId': '04516417231338479991'},\n",
       " 'lastModifyingUserName': 'cerebro@whtlst-158518.iam.gserviceaccount.com',\n",
       " 'lastViewedByMeDate': '2018-06-12T13:04:14.678Z',\n",
       " 'markedViewedByMeDate': '1970-01-01T00:00:00.000Z',\n",
       " 'mimeType': 'application/vnd.google-apps.document',\n",
       " 'modifiedByMeDate': '2018-06-12T13:04:14.757Z',\n",
       " 'modifiedDate': '2018-06-12T13:04:14.757Z',\n",
       " 'ownerNames': ['cerebro@whtlst-158518.iam.gserviceaccount.com'],\n",
       " 'owners': [{'displayName': 'cerebro@whtlst-158518.iam.gserviceaccount.com',\n",
       "   'emailAddress': 'cerebro@whtlst-158518.iam.gserviceaccount.com',\n",
       "   'isAuthenticatedUser': True,\n",
       "   'kind': 'drive#user',\n",
       "   'permissionId': '04516417231338479991'}],\n",
       " 'parents': [{'id': '1IsigSj9Yim2u8t90fHS75T1ANnyOHBIp',\n",
       "   'isRoot': False,\n",
       "   'kind': 'drive#parentReference',\n",
       "   'parentLink': 'https://www.googleapis.com/drive/v2/files/1IsigSj9Yim2u8t90fHS75T1ANnyOHBIp',\n",
       "   'selfLink': 'https://www.googleapis.com/drive/v2/files/1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY/parents/1IsigSj9Yim2u8t90fHS75T1ANnyOHBIp'}],\n",
       " 'quotaBytesUsed': '0',\n",
       " 'selfLink': 'https://www.googleapis.com/drive/v2/files/1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY',\n",
       " 'shared': True,\n",
       " 'spaces': ['drive'],\n",
       " 'title': 'A Test',\n",
       " 'userPermission': {'etag': '\"HPeBWzrNkWqp0rz2LrVPmfIGOTs/DN9PUHZo8Jf2pZfWFg8aQyOBDvk\"',\n",
       "  'id': 'me',\n",
       "  'kind': 'drive#permission',\n",
       "  'role': 'owner',\n",
       "  'selfLink': 'https://www.googleapis.com/drive/v2/files/1HLddYJx0a-C0dJwQ6rVZwrR_Un5widLI-BHrkmCiEUY/permissions/me',\n",
       "  'type': 'user'},\n",
       " 'version': '4',\n",
       " 'writersCanShare': True}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insert_file(ss, '/tmp/hello.docx', title='A Test', parent_id='1IsigSj9Yim2u8t90fHS75T1ANnyOHBIp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from docx import Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "document = Document('/Users/joel/Downloads/dealhelloworld.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deal_vars = {\n",
    "    \"artist_name\": \"Test Artist\",\n",
    "    \"offer_date\": \"2 Jun 2018\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for p in document.paragraphs:\n",
    "    print(p.text.format(**deal_vars))\n",
    "    \n",
    "    p.text = p.text.format(**deal_vars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "document.save('/tmp/hello.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import boto3\n",
    "\n",
    "r = boto3.resource('dynamodb')\n",
    "\n",
    "offerTable = r.Table('WlOfferTable')\n",
    "\n",
    "offerTable.get_item(Key=dict(id='eeq7gv8g'))\n",
    "\n",
    "offerTable.update_item(\n",
    "    Key=dict(id='eeq7gv8g'),\n",
    "    UpdateExpression=\"set dealDocLink = :l\",\n",
    "    ExpressionAttributeValues={\n",
    "        ':l': 'not a real link',\n",
    "    },\n",
    "    ReturnValues=\"UPDATED_NEW\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json, decimal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "class DecimalEncoder(json.JSONEncoder):\n",
    "    def default(self, o):\n",
    "        if isinstance(o, decimal.Decimal):\n",
    "            if o % 1 > 0:\n",
    "                return float(o)\n",
    "            else:\n",
    "                return int(o)\n",
    "        return super(DecimalEncoder, self).default(o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "item = offerTable.get_item(Key=dict(id='eeq7gv8g'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"Item\": {\n",
      "    \"createdDate\": \"2018-06-11T16:32:59.149Z\",\n",
      "    \"dealDocLink\": \"not a real link\",\n",
      "    \"advancePerSingle\": 2000,\n",
      "    \"trackId\": \"11003\",\n",
      "    \"rejectedDate\": \"2018-06-11T16:33:04.086Z\",\n",
      "    \"user\": \"joel\",\n",
      "    \"marketingRate\": 0.3,\n",
      "    \"id\": \"eeq7gv8g\",\n",
      "    \"numSingles\": 3\n",
      "  },\n",
      "  \"ResponseMetadata\": {\n",
      "    \"RequestId\": \"IH0UJGD5P7ETLVMIU7H75JL4EVVV4KQNSO5AEMVJF66Q9ASUAAJG\",\n",
      "    \"HTTPStatusCode\": 200,\n",
      "    \"HTTPHeaders\": {\n",
      "      \"server\": \"Server\",\n",
      "      \"date\": \"Tue, 12 Jun 2018 13:42:18 GMT\",\n",
      "      \"content-type\": \"application/x-amz-json-1.0\",\n",
      "      \"content-length\": \"292\",\n",
      "      \"connection\": \"keep-alive\",\n",
      "      \"x-amzn-requestid\": \"IH0UJGD5P7ETLVMIU7H75JL4EVVV4KQNSO5AEMVJF66Q9ASUAAJG\",\n",
      "      \"x-amz-crc32\": \"1414951161\"\n",
      "    },\n",
      "    \"RetryAttempts\": 0\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(item, cls=DecimalEncoder, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "SCOPES = ['https://www.googleapis.com/auth/drive']\n",
    "SERVICE_ACCOUNT_FILE = \"/tracker/whtlst_google.json\"\n",
    "\n",
    "credentials = service_account.Credentials.from_service_account_file(\n",
    "    SERVICE_ACCOUNT_FILE, scopes=SCOPES)\n",
    "\n",
    "service = googleapiclient.discovery.build('drive', 'v2', credentials=credentials)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f = service.files()\n",
    "\n",
    "f.export_media\n",
    "\n",
    "bd = f.export_media(\n",
    "    fileId='1tYc0qftxEwq9GPi9LNCbNm-qkzH_h4mTEQiYUTFusNg',\n",
    "    mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document'\n",
    ").execute()\n",
    "\n",
    "with open('/tmp/t.docx', 'wb') as fout: \n",
    "    fout.write(bd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://www.googleapis.com/drive/v2/files/1tYc0qftxEwq9GPi9LNCbNm-qkzH_h4mTEQiYUTFusNg/export?mimeType=application%2Fvnd.openxmlformats-officedocument.wordprocessingml.document'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bd.uri"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "write() argument must be str, not HttpRequest",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-71-d5a185d60659>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: write() argument must be str, not HttpRequest"
     ]
    }
   ],
   "source": [
    "f.write(bd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Object `f.export` not found.\n"
     ]
    }
   ],
   "source": [
    "f.export?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
