Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 213 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,213 @@
build
dist
*egg-info*
private.py
__pycache__
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Python template
# 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

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__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/

118 changes: 55 additions & 63 deletions GoogleMyMaps/GoogleMyMaps.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
# import sys
import requests
from bs4 import BeautifulSoup
from pyjsparser import PyJsParser
from GoogleMyMaps.parsers import GoogleMyMapsParser
from .models import Map, Layer, Place


class GoogleMyMaps():

class GoogleMyMaps:
def __init__(self):
self.parser = PyJsParser()

def getFromMyMap(self, mapID):
r = requests.get(
"https://www.google.com/maps/d/edit?hl=ja&mid=" + mapID)
return r

def parseData(self, r):
soup = BeautifulSoup(r.text, "html.parser")
script = soup.find_all("script")[1].text
js = self.parser.parse(script)
pagedata = js["body"][1]["declarations"][0]["init"]["value"]

data = pagedata.replace("true", "True")
data = data.replace("false", "False")
data = data.replace("null", "None")
data = data.replace("\n", "")
# exec("data = " + data)
data = eval(data)
return data[1]

def parseLayerData(self, layerData):
# layerName = layerData[2]

places = layerData[4]
# url = places[0][0]

parsed = []
for place in places:
placeName = place[5][0][0]

info = place[4]
point = info[4]

parsed.append({
"placeName": placeName,
"point": point,
})

return parsed

def get(self, mapID, layers=[0]):
r = self.getFromMyMap(mapID)
if r.status_code != 200:
print("status_code:", r.status_code)
raise

data = self.parseData(r)
# mapID = data[1]
# mapName = data[2]

parsed = []
for layer in layers:
layerData = data[6][layer]
parsed += self.parseLayerData(layerData)

return parsed
self.parser = GoogleMyMapsParser()

def create_map(self, map_link, chosen_layers: list = None):
data = self.parser.get_map_data(map_link)
name = data[2] if len(data) > 2 else 'Unnamed Map'
chosen_layers = GoogleMyMaps._parse_layers(data[6], chosen_layers) if len(data) > 6 else []
return Map(map_link, name, chosen_layers)

@staticmethod
def _parse_layers(layers_data, chosen_layers=None):
layers = []
for index, layer_data in enumerate(layers_data):
if chosen_layers is None or index in chosen_layers:
layer_name = layer_data[2] if len(layer_data) > 2 else f'Unnamed Layer {index + 1}'
places = GoogleMyMaps._parse_places(layer_data[12][0][13]) if len(layer_data) > 12 else []
layers.append(Layer(layer_name, places))
return layers

@staticmethod
def _parse_places(places_data):
places = []
for place_data, place_icon_data in zip(places_data[0], places_data[1]):
icon = place_icon_data[0][0] if place_icon_data and len(place_icon_data) > 0 else None

place_type, coords = GoogleMyMaps._get_place_type_and_coords(place_data) if len(place_data) > 5 else (None, None)

place_info = place_data[5] if len(place_data) > 5 else None
name = place_info[0][1][0] if place_info and len(place_info[0]) > 1 else 'Unnamed Place'
photos = [photo[1] for photo in place_info[2]] if len(place_info) > 2 and place_info[2] else None
data = GoogleMyMaps._extract_place_data(place_info)

places.append(Place(place_type, name, icon, coords, photos, data))
return places

@staticmethod
def _get_place_type_and_coords(place):
if place[1] is not None:
return 'Point', place[1][0][0]
elif place[2] is not None:
return 'Line', [cord[0] for cord in place[2][0][0]]
elif place[3] is not None:
return 'Polygon', [cord[0] for cord in place[3][0][0][0][0]]

@staticmethod
def _extract_place_data(place_info):
place_data = {}
if len(place_info) > 1 and place_info[1]:
place_data[place_info[1][0]] = place_info[1][1][place_info[1][2] - 1]
if len(place_info) > 3 and place_info[3]:
for info in place_info[3]:
place_data[info[0]] = info[1][info[2] - 1]
return place_data if place_data else None
1 change: 1 addition & 0 deletions GoogleMyMaps/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .GoogleMyMaps import GoogleMyMaps
from .models import Map, Layer, Place
12 changes: 12 additions & 0 deletions GoogleMyMaps/models/Layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from . import Place


class Layer:
def __init__(self, name: str, places: list[Place]):
self.name = name
self.places = places

def __str__(self):
places_str = '\n'.join([f" {place}" for place in self.places]) if self.places else "No places"
return f'Layer: {self.name}\n' \
f'{places_str}\n'
14 changes: 14 additions & 0 deletions GoogleMyMaps/models/Map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from . import Layer


class Map:
def __init__(self, link: str, name: str, layers: list[Layer]):
self.link = link
self.name = name
self.layers = layers

def __str__(self):
layers_str = '\n'.join([f" {layer}" for layer in self.layers]) if self.layers else "No layers"
return f'Link: {self.link}\n' \
f'Map: {self.name}\n' \
f'{layers_str}\n'
Loading