{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import firebase_admin\n",
    "from firebase_admin import credentials\n",
    "from firebase_admin import firestore\n",
    "import csv\n",
    "import numpy as np\n",
    "# Use a service account\n",
    "cred = credentials.Certificate(r'../keys/fsk.json')\n",
    "#add your own key as this is public. email me for details\n",
    "firebase_admin.initialize_app(cred)\n",
    "\n",
    "db = firestore.client()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "teams=db.collection('data').document('team-2022').collection(\"Midwest 2019\").get()\n",
    "full=[]\n",
    "tms=[]\n",
    "for team in teams:\n",
    "    data=[]\n",
    "    tms.append(team.id)\n",
    "    reports=db.collection('data').document('team-2022').collection(\"Midwest 2019\").document(team.id).collection(\"matches\").get()\n",
    "    for report in reports:\n",
    "        data.append(db.collection('data').document('team-2022').collection(\"Midwest 2019\").document(team.id).collection(\"matches\").document(report.id).get().to_dict())\n",
    "    full.append(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def expcsv(loc,data):\n",
    "    with open(loc+'.csv', 'w', newline='', encoding='utf-8') as csvfile:\n",
    "        w = csv.writer(csvfile, delimiter=',', quotechar=\"\\\"\", quoting=csv.QUOTE_MINIMAL)\n",
    "        for i in data:\n",
    "            w.writerow(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def keymatch(ld):\n",
    "    keys=set([])\n",
    "    for i in ld:\n",
    "        for j in i.keys():\n",
    "            keys.add(j)\n",
    "    kl=list(keys)\n",
    "    data=[]\n",
    "    for i in kl:\n",
    "        data.append([i])\n",
    "    for i in kl:\n",
    "        for j in ld:\n",
    "            try:\n",
    "                (data[kl.index(i)]).append(j[i])\n",
    "            except:\n",
    "                (data[kl.index(i)]).append(\"\")\n",
    "    return data\n",
    "wn=[]\n",
    "for i in full:\n",
    "    wn.append(np.transpose(np.array(keymatch(i))).tolist())\n",
    "for i in range(len(wn)):\n",
    "    expcsv(tms[i],wn[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}