Add utm tags to announcement links
This commit is contained in:
1 parent
2364474ea6
commit
69f9c523f9
3 files changed
+57
-4
No files matched your search
@@ -22,7 +22,9 @@ from collections import OrderedDict
|
||||
|
||||
from octoprint.server import admin_permission
|
||||
from octoprint.server.util.flask import restricted_access, with_revalidation_checking, check_etag
|
||||
from octoprint.util import utmify
|
||||
from flask.ext.babel import gettext
|
||||
from octoprint import __version__ as OCTOPRINT_VERSION
|
||||
|
||||
class AnnouncementPlugin(octoprint.plugin.AssetPlugin,
|
||||
octoprint.plugin.SettingsPlugin,
|
||||
@@ -173,6 +175,7 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin,
|
||||
hash = hashlib.sha1()
|
||||
hash.update(repr(sorted(enabled)))
|
||||
hash.update(repr(sorted(forced)))
|
||||
hash.update(OCTOPRINT_VERSION)
|
||||
|
||||
for channel in sorted(channel_configs.keys()):
|
||||
hash.update(repr(channel_configs[channel]))
|
||||
@@ -386,7 +389,7 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin,
|
||||
summary=_lazy_images(entry["summary"]),
|
||||
summary_without_images=_strip_images(entry["summary"]),
|
||||
published=published,
|
||||
link=entry["link"],
|
||||
link=utmify(entry["link"], source="octoprint", medium="announcements", content=OCTOPRINT_VERSION),
|
||||
read=read)
|
||||
|
||||
def _get_channel_cache_path(self, key):
|
||||
|
||||
@@ -875,14 +875,14 @@ except ImportError:
|
||||
# no glob.escape - we need to implement our own
|
||||
_glob_escape_check = re.compile("([*?[])")
|
||||
_glob_escape_check_bytes = re.compile(b"([*?[])")
|
||||
|
||||
|
||||
def glob_escape(pathname):
|
||||
"""
|
||||
Ported from Python 3.4
|
||||
|
||||
|
||||
See https://github.com/python/cpython/commit/fd32fffa5ada8b8be8a65bd51b001d989f99a3d3
|
||||
"""
|
||||
|
||||
|
||||
drive, pathname = os.path.splitdrive(pathname)
|
||||
if isinstance(pathname, bytes):
|
||||
pathname = _glob_escape_check_bytes.sub(br"[\1]", pathname)
|
||||
@@ -900,6 +900,40 @@ except RuntimeError:
|
||||
monotonic_time = time.time
|
||||
|
||||
|
||||
def utmify(link, source=None, medium=None, name=None, term=None, content=None):
|
||||
if source is None:
|
||||
return link
|
||||
|
||||
from collections import OrderedDict
|
||||
try:
|
||||
import urlparse
|
||||
from urllib import urlencode
|
||||
except ImportError:
|
||||
# python 3
|
||||
import urllib.parse as urlparse
|
||||
from urllib.parse import urlencode
|
||||
|
||||
# inspired by https://stackoverflow.com/a/2506477
|
||||
parts = list(urlparse.urlparse(link))
|
||||
|
||||
# parts[4] is the url query
|
||||
query = OrderedDict(urlparse.parse_qs(parts[4]))
|
||||
|
||||
query["utm_source"] = source
|
||||
if medium is not None:
|
||||
query["utm_medium"] = medium
|
||||
if name is not None:
|
||||
query["utm_name"] = name
|
||||
if term is not None:
|
||||
query["utm_term"] = term
|
||||
if content is not None:
|
||||
query["utm_content"] = content
|
||||
|
||||
parts[4] = urlencode(query, doseq=True)
|
||||
|
||||
return urlparse.urlunparse(parts)
|
||||
|
||||
|
||||
class RepeatedTimer(threading.Thread):
|
||||
"""
|
||||
This class represents an action that should be run repeatedly in an interval. It is similar to python's
|
||||
|
||||
Reference in new issue
Block a user