делает первый коммит
This commit is contained in:
commit
31d1e59abc
171
.gitignore
vendored
Normal file
171
.gitignore
vendored
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
|
||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/python
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=python
|
||||||
|
|
||||||
|
### Python ###
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
*.py,cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
cover/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
.pybuilder/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
|
# .python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# poetry
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
|
#poetry.lock
|
||||||
|
|
||||||
|
# pdm
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
|
#pdm.lock
|
||||||
|
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||||
|
# in version control.
|
||||||
|
# https://pdm.fming.dev/#use-with-ide
|
||||||
|
.pdm.toml
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Environments
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# pytype static type analyzer
|
||||||
|
.pytype/
|
||||||
|
|
||||||
|
# Cython debug symbols
|
||||||
|
cython_debug/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/python
|
||||||
|
|
||||||
|
|
||||||
|
token.json
|
||||||
|
credentials.json
|
147
main.py
Normal file
147
main.py
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
import os.path
|
||||||
|
|
||||||
|
from google.auth.transport.requests import Request
|
||||||
|
from google.oauth2.credentials import Credentials
|
||||||
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
|
from googleapiclient.discovery import build
|
||||||
|
from googleapiclient.errors import HttpError
|
||||||
|
|
||||||
|
############################################
|
||||||
|
## Параметры приложения
|
||||||
|
|
||||||
|
OLD_NUMBER_PREFIXES = ['+38071', '071']
|
||||||
|
NEW_NUMBER_PREFIXES = ['+7949']
|
||||||
|
|
||||||
|
# Функция для получения нового номера из основы старого
|
||||||
|
#
|
||||||
|
# Вернет номер в формате +7 949 123-45-67
|
||||||
|
def new_phone_number(base: str):
|
||||||
|
num = '7949' + base
|
||||||
|
|
||||||
|
return '+{} {} {}-{}-{}'.format(
|
||||||
|
num[0],
|
||||||
|
num[1:4],
|
||||||
|
num[4:7],
|
||||||
|
num[7:9],
|
||||||
|
num[9:11]
|
||||||
|
)
|
||||||
|
|
||||||
|
PHONE_TYPE = 'mobile'
|
||||||
|
|
||||||
|
###################################################
|
||||||
|
# Параметры Google API
|
||||||
|
|
||||||
|
CREDENTIALS_FILE = 'credentials.json'
|
||||||
|
TOKEN_FILE = 'token.json'
|
||||||
|
REDIRECT_PORT = 42047
|
||||||
|
SCOPES = ['https://www.googleapis.com/auth/contacts']
|
||||||
|
|
||||||
|
########
|
||||||
|
|
||||||
|
def main():
|
||||||
|
creds = None
|
||||||
|
|
||||||
|
if os.path.exists(TOKEN_FILE):
|
||||||
|
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
|
||||||
|
|
||||||
|
if not creds or not creds.valid:
|
||||||
|
if creds and creds.expired and creds.refresh_token:
|
||||||
|
creds.refresh(Request())
|
||||||
|
else:
|
||||||
|
flow = InstalledAppFlow.from_client_secrets_file(
|
||||||
|
CREDENTIALS_FILE, SCOPES)
|
||||||
|
creds = flow.run_local_server(port=42047)
|
||||||
|
with open(TOKEN_FILE, 'w') as token:
|
||||||
|
token.write(creds.to_json())
|
||||||
|
|
||||||
|
try:
|
||||||
|
service = build('people', 'v1', credentials=creds)
|
||||||
|
print('Получаем список контактов...')
|
||||||
|
results = service.people().connections().list(resourceName='people/me',
|
||||||
|
personFields='names,phoneNumbers').execute()
|
||||||
|
connections = results.get('connections', [])
|
||||||
|
|
||||||
|
print('Количество контактов: {}'.format(len(connections)))
|
||||||
|
|
||||||
|
batch_request = {
|
||||||
|
"contacts": {},
|
||||||
|
"updateMask": "phoneNumbers"
|
||||||
|
}
|
||||||
|
|
||||||
|
for person in connections:
|
||||||
|
phoneNumbers = person.get('phoneNumbers', [])
|
||||||
|
|
||||||
|
has_old = False
|
||||||
|
|
||||||
|
old_numbers = set()
|
||||||
|
new_numbers = set()
|
||||||
|
|
||||||
|
# Получаем номера нового и старого формата
|
||||||
|
for phoneNumber in phoneNumbers:
|
||||||
|
num = phoneNumber.get('canonicalForm')
|
||||||
|
|
||||||
|
if not num:
|
||||||
|
continue
|
||||||
|
|
||||||
|
base = get_number_without_prefix(num, OLD_NUMBER_PREFIXES)
|
||||||
|
|
||||||
|
if (base):
|
||||||
|
has_old = True
|
||||||
|
old_numbers.add(base)
|
||||||
|
|
||||||
|
base = get_number_without_prefix(num, NEW_NUMBER_PREFIXES)
|
||||||
|
|
||||||
|
if (base):
|
||||||
|
new_numbers.add(base)
|
||||||
|
|
||||||
|
# Если нет старого - пропускаем
|
||||||
|
if not has_old:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Получаем старые номера, у которых нету парного нового
|
||||||
|
numbers_not_updated = list(
|
||||||
|
old_numbers.difference(new_numbers)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Если нет старых номеров, которые нужно обновить - пропускаем
|
||||||
|
if len(numbers_not_updated) == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Получаем новые номера
|
||||||
|
new_phone_numbers = [
|
||||||
|
new_phone_number(x) for x in numbers_not_updated
|
||||||
|
]
|
||||||
|
|
||||||
|
# batchUpdateContacts: https://developers.google.com/people/api/rest/v1/people/batchUpdateContacts
|
||||||
|
# PhoneNumber: https://developers.google.com/people/api/rest/v1/people#phonenumber
|
||||||
|
batch_request['contacts'][person['resourceName']] = {
|
||||||
|
"phoneNumbers": [
|
||||||
|
*person['phoneNumbers'],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"value": number,
|
||||||
|
"type": PHONE_TYPE
|
||||||
|
} for number in new_phone_numbers
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"etag": person['etag']
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len(batch_request['contacts']) > 0):
|
||||||
|
print('Обновляем контакты...')
|
||||||
|
service.people().batchUpdateContacts(body = batch_request).execute()
|
||||||
|
print('Обновление завершено!')
|
||||||
|
else:
|
||||||
|
print('Нечего обновлять!')
|
||||||
|
except HttpError as err:
|
||||||
|
print(err)
|
||||||
|
|
||||||
|
def get_number_without_prefix(number: str, prefixes: list):
|
||||||
|
for prefix in prefixes:
|
||||||
|
if number.startswith(prefix):
|
||||||
|
return number[len(prefix):]
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
google-api-python-client==2.49.0
|
||||||
|
google-auth-httplib2==0.1.0
|
||||||
|
google-auth-oauthlib==0.5.1
|
Loading…
Reference in New Issue
Block a user