делает рефакторинг кода

This commit is contained in:
Maxim Slipenko 2022-06-10 21:49:38 +03:00
parent 5f259ad275
commit 363530f4f6
Signed by: Maks1mS
GPG Key ID: 7461AF39A8705FB8
2 changed files with 69 additions and 23 deletions

View File

@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 1,
"metadata": {
"id": "pzHzXS_eFteU"
},
@ -49,7 +49,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 2,
"metadata": {
"id": "rRgABs8rF1d8"
},
@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 3,
"metadata": {
"id": "YMHU4Hs_F5-Y"
},
@ -91,16 +91,48 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "-Kdstr4TOs1H"
},
"metadata": {},
"source": [
"Основной код приложения"
"Функция для получения доступа к Google аккаунту:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def google_creds():\n",
" creds = None\n",
"\n",
" if os.path.exists(TOKEN_FILE):\n",
" creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)\n",
"\n",
" if not creds or not creds.valid:\n",
" if creds and creds.expired and creds.refresh_token:\n",
" creds.refresh(Request())\n",
" else:\n",
" flow = InstalledAppFlow.from_client_secrets_file(\n",
" CREDENTIALS_FILE, SCOPES)\n",
" creds = flow.run_console()\n",
" with open(TOKEN_FILE, 'w') as token:\n",
" token.write(creds.to_json())\n",
"\n",
" return creds"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-Kdstr4TOs1H"
},
"source": [
"Основной код приложения:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "TimQdg09F2GV"
},
@ -200,12 +232,12 @@
"id": "WnbwQVZPNDpP"
},
"source": [
"Запуск приложения"
"Запуск приложения:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
@ -216,16 +248,12 @@
},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-17-c7bc734e5e35>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-16-077678182ae9>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m flow = InstalledAppFlow.from_client_secrets_file(\n\u001b[0;32m---> 10\u001b[0;31m CREDENTIALS_FILE, SCOPES)\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0mcreds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_console\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTOKEN_FILE\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtoken\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/google_auth_oauthlib/flow.py\u001b[0m in \u001b[0;36mfrom_client_secrets_file\u001b[0;34m(cls, client_secrets_file, scopes, **kwargs)\u001b[0m\n\u001b[1;32m 199\u001b[0m \u001b[0mFlow\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mThe\u001b[0m \u001b[0mconstructed\u001b[0m \u001b[0mFlow\u001b[0m \u001b[0minstance\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 200\u001b[0m \"\"\"\n\u001b[0;32m--> 201\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mclient_secrets_file\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mjson_file\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 202\u001b[0m \u001b[0mclient_config\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjson_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 203\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'credentials.json'"
"name": "stdout",
"output_type": "stream",
"text": [
"Получаем список контактов...\n",
"Количество контактов: 48\n",
"Нечего обновлять!\n"
]
}
],
@ -240,12 +268,25 @@
"name": "Phone number migration.ipynb",
"provenance": []
},
"interpreter": {
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.10.4 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,

View File

@ -38,7 +38,7 @@ SCOPES = ['https://www.googleapis.com/auth/contacts']
########
def main():
def google_creds():
creds = None
if os.path.exists(TOKEN_FILE):
@ -54,7 +54,12 @@ def main():
with open(TOKEN_FILE, 'w') as token:
token.write(creds.to_json())
return creds
def main():
try:
creds = google_creds()
service = build('people', 'v1', credentials=creds)
print('Получаем список контактов...')
results = service.people().connections().list(resourceName='people/me',