intel-screenbot

An Ingress Intel Map Bot for Hangoutsbot
git clone git://archive.git.mtrnord.blog/MTRNord/intel-screenbot.git
Log | Files | Refs | README | LICENSE

commit 7569e5d885026d1a661ffdb8bf016b32a041df2b
parent 9e034c6bde088c4167bb1277ab269d3ef3cc34a4
Author: Marcel Radzio <info@nordgedanken.de>
Date:   Mon, 14 Nov 2016 07:55:36 +0100

code cleanup

Diffstat:
M__init__.py | 60++++++++++++++++++++++++++++--------------------------------
Mscreencap_iitc.js | 178++++++++++++++++++++++++++++++-------------------------------------------------
Mscreencap_intel.js | 193++++++++++++++++++++++++++++---------------------------------------------------
3 files changed, 165 insertions(+), 266 deletions(-)

diff --git a/__init__.py b/__init__.py @@ -16,8 +16,8 @@ def _initialise(bot): plugins.register_user_command(["intel", "iitc"]) plugins.register_admin_command(["setintel", "clearintel", "show_iitcplugins", "set_iitcplugins", "clear_iitcplugins"]) _get_iitc_plugins(bot) - - + + @asyncio.coroutine def _open_file(name): logger.debug("opening screenshot file: {}".format(name)) @@ -47,7 +47,7 @@ def _parse_onlineRepos(url, ext=''): else: soup = BeautifulSoup(page, 'html.parser') return [url + '/' + node.get('href') for node in soup.find_all('a') if node.get('href').endswith(ext)] - + def _get_iitc_plugins(bot): logger.debug("getting availible plugins") if bot.config.exists(["intel_screenbot", "gitlab_token"]): @@ -120,7 +120,7 @@ def _screencap(maptype, url, filepath, filename, SACSID, CSRF, plugins, search, def setintel(bot, event, *args): - """set url for current converation for the screenshot command. + """set url for current converation for the intel or iitc command. use /bot clearintel to clear the previous url before setting a new one. """ url = bot.conversation_memory_get(event.conv_id, 'IntelURL') @@ -136,7 +136,7 @@ def setintel(bot, event, *args): def clearintel(bot, event, *args): - """clear url for current converation for the screenshot command. + """clear url for current converation for the intel or iitc command. """ url = bot.conversation_memory_get(event.conv_id, 'IntelURL') if url is None: @@ -150,21 +150,19 @@ def clearintel(bot, event, *args): def intel(bot, event, *args): - """get a screenshot of a user provided URL or the default URL of the hangout. + """get a screenshot of a search term or intel URL or the default intel URL of the hangout. """ - + if args: if len(args) > 1: url = ' '.join(str(i) for i in args) - if '"' in url: - url = url.replace('"', '') else: url = args[0] - if '"' in url: - url = url.replace('"', '') + if '"' in url: + url = url.replace('"', '') else: url = bot.conversation_memory_get(event.conv_id, 'IntelURL') - + if bot.config.exists(["intel_screenbot", "SACSID"]): SACSID = bot.config.get_by_path(["intel_screenbot", "SACSID"]) else: @@ -175,12 +173,12 @@ def intel(bot, event, *args): else: html = "<i><b>{}</b> No Intel CSRF Cookie has been added to config. Unable to authenticate".format(event.user.full_name) yield from bot.coro_send_message(event.conv, html) - + if url is None: - html = "<i><b>{}</b> No Intel URL has been set for screenshots.".format(event.user.full_name) + html = "<i><b>{}</b> No Intel URL or search term has been set for screenshots.".format(event.user.full_name) yield from bot.coro_send_message(event.conv, html) - - else: + + else: if re.match(r'^[a-zA-Z]+://', url): search = False ZoomSearch = re.finditer(r"(?:&z=).*", url) @@ -197,10 +195,10 @@ def intel(bot, event, *args): logger.info(search); url = 'https://www.ingress.com/intel' yield from bot.coro_send_message(event.conv_id, "<i>intel map is searching " + search + " and screenshooting as requested, please wait...</i>") + filename = event.conv_id + "." + str(time.time()) +".png" filepath = tempfile.NamedTemporaryFile(prefix=event.conv_id, suffix=".png", delete=False).name logger.debug("temporary screenshot file: {}".format(filepath)) - try: loop = asyncio.get_event_loop() image_data = yield from _screencap("intel", url, filepath, filename, SACSID, CSRF, "", search, bot, event) @@ -208,24 +206,22 @@ def intel(bot, event, *args): yield from bot.coro_send_message(event.conv_id, "<i>error getting screenshot</i>") logger.exception("screencap failed".format(url)) return - - + + def iitc(bot, event, *args): - """get a screenshot of a user provided URL or the default URL of the hangout. + """get a screenshot of a search term or intel URL or the default intel URL of the hangout. """ - + if args: if len(args) > 1: url = ' '.join(str(i) for i in args) - if '"' in url: - url = url.replace('"', '') else: url = args[0] - if '"' in url: - url = url.replace('"', '') + if '"' in url: + url = url.replace('"', '') else: url = bot.conversation_memory_get(event.conv_id, 'IntelURL') - + if bot.config.exists(["intel_screenbot", "SACSID"]): SACSID = bot.config.get_by_path(["intel_screenbot", "SACSID"]) else: @@ -236,12 +232,12 @@ def iitc(bot, event, *args): else: html = "<i><b>{}</b> No Intel CSRF Cookie has been added to config. Unable to authenticate".format(event.user.full_name) yield from bot.coro_send_message(event.conv, html) - + if url is None: html = "<i><b>{}</b> No Intel URL has been set for screenshots.".format(event.user.full_name) yield from bot.coro_send_message(event.conv, html) - - else: + + else: if re.match(r'^[a-zA-Z]+://', url): search = False ZoomSearch = re.finditer(r"(?:&z=).*", url) @@ -258,10 +254,10 @@ def iitc(bot, event, *args): logger.info(search); url = 'https://www.ingress.com/intel' yield from bot.coro_send_message(event.conv_id, "<i>intel map is searching " + search + " and screenshooting as requested, please wait...</i>") + filename = event.conv_id + "." + str(time.time()) +".png" filepath = tempfile.NamedTemporaryFile(prefix=event.conv_id, suffix=".png", delete=False).name logger.debug("temporary screenshot file: {}".format(filepath)) - if bot.conversation_memory_get(event.conv_id, 'iitc_plugins'): plugins = [] plugin_names = bot.conversation_memory_get(event.conv_id, 'iitc_plugins').split(", ") @@ -272,7 +268,7 @@ def iitc(bot, event, *args): plugins.append(plugin_objects["url"]) else: plugins = '' - + try: loop = asyncio.get_event_loop() image_data = yield from _screencap("iitc", url, filepath, filename, SACSID, CSRF, json.dumps(plugins), search, bot, event) @@ -299,7 +295,7 @@ def set_iitcplugins(bot, event, *args): bot.conversation_memory_set(event.conv_id, 'iitc_plugins', ', '.join(args)) html = "<i><b>{}</b> updated plugins".format(event.user.full_name) yield from bot.coro_send_message(event.conv, html) - + def clear_iitcplugins(bot, event, *args): if bot.conversation_memory_get(event.conv_id, 'iitc_plugins') is None: html = "<i><b>{}</b> nothing to clear for this conversation".format(event.user.full_name) diff --git a/screencap_iitc.js b/screencap_iitc.js @@ -1,5 +1,5 @@ -var system = require('system') -var args = require('system').args; +var system = require('system'); +var args = system.args; var page = require('webpage').create(); var fs = require('fs'); if (args.length === 1) { @@ -24,26 +24,6 @@ if (args.length === 1) { } } -addCookies(SACSID,CSRF) -afterCookieLogin(IntelURL, search) - -function waitFor ($config) { - $config._start = $config._start || new Date(); - - if ($config.timeout && new Date - $config._start > $config.timeout) { - if ($config.error) $config.error(); - if ($config.debug) console.log('timedout ' + (new Date - $config._start) + 'ms'); - return; - } - - if ($config.check()) { - if ($config.debug) console.log('success ' + (new Date - $config._start) + 'ms'); - return $config.success(); - } - - setTimeout(waitFor, $config.interval || 0, $config); -} - function addCookies(sacsid, csrf) { phantom.addCookie({ name: 'SACSID', @@ -62,20 +42,37 @@ function addCookies(sacsid, csrf) { } -/** - * Does all stuff needed after cookie authentication - * @since 3.1.0 - */ +function waitFor ($config) { + $config._start = $config._start || new Date(); + if ($config.timeout && new Date - $config._start > $config.timeout) { + if ($config.error) $config.error(); + if ($config.debug) console.log('timedout ' + (new Date - $config._start) + 'ms'); + return; + } + if ($config.check()) { + if ($config.debug) console.log('success ' + (new Date - $config._start) + 'ms'); + return $config.success(); + } + setTimeout(waitFor, $config.interval || 0, $config); +} + +function loadIitcPlugin(src) { + page.evaluate(function(src) { + var script = document.createElement('script'); + script.type='text/javascript'; + script.src=src; + document.head.insertBefore(script, document.head.lastChild); + }, src); +} + +addCookies(SACSID, CSRF); +afterCookieLogin(IntelURL, search); + function afterCookieLogin(IntelURL, search) { page.viewportSize = { width: '1920', height: '1080' }; page.open(IntelURL, function(status) { if (status !== 'success') {quit('unable to connect to remote server')} page.injectJs('https://code.jquery.com/jquery-3.1.1.min.js'); - if(!isSignedIn()) { - if(fs.exists('.iced_cookies')) { - fs.remove('.iced_cookies'); - } - } setTimeout(function() { page.evaluate(function() { localStorage['ingress.intelmap.layergroupdisplayed'] = JSON.stringify({ @@ -102,12 +99,14 @@ function afterCookieLogin(IntelURL, search) { if (search != "nix") { page.evaluate(function(search) { if (document.querySelector('#search')){ + window.setTimeout(function() { document.getElementById("search").value=search; var e = jQuery.Event("keypress"); e.which = 13; e.keyCode = 13; $("#search").trigger(e); - window.setTimeout(function() {$('.searchquery > :nth-child(2)').children()[0].click();}, 2000); + }, 2000); + window.setTimeout(function() {$('.searchquery > :nth-child(2)').children()[0].click();}, 2000); } }, search); } @@ -131,67 +130,32 @@ function afterCookieLogin(IntelURL, search) { hideDebris(); prepare('1920', '1080'); main(); - } // optional + } }); }, "5000"); }, "5000"); }); } -function loadIitcPlugin(src) { - page.evaluate(function(src) { - var script = document.createElement('script'); - script.type='text/javascript'; - script.src=src; - document.head.insertBefore(script, document.head.lastChild); - }, src); -} - -/** - * Checks if user is signed in by looking for the "Sign in" button - * @returns {boolean} - * @since 3.2.0 - */ -function isSignedIn() { - return page.evaluate(function() { - return document.getElementsByTagName('a')[0].innerText.trim() !== 'Sign in'; - }); -} - -function storeCookies() { - var cookies = page.cookies; - fs.write('.iced_cookies', '', 'w'); - for(var i in cookies) { - fs.write('.iced_cookies', cookies[i].name + '=' + cookies[i].value +'\n', 'a'); - } -} - function s(file) { page.render(file); phantom.exit(0); } function hideDebris() { - window.setTimeout(function() { - page.evaluate(function() { - if (document.querySelector('#chat')) {document.querySelector('#chat').style.display = 'none';} - if (document.querySelector('#chatcontrols')) {document.querySelector('#chatcontrols').style.display = 'none';} - if (document.querySelector('#chatinput')) {document.querySelector('#chatinput').style.display = 'none';} - if (document.querySelector('#updatestatus')) {document.querySelector('#updatestatus').style.display = 'none';} - if (document.querySelector('#sidebartoggle')) {document.querySelector('#sidebartoggle').style.display = 'none';} - if (document.querySelector('#scrollwrapper')) {document.querySelector('#scrollwrapper').style.display = 'none';} - if (document.querySelector('.leaflet-control-container')) {document.querySelector('.leaflet-control-container').style.display = 'none';} - }); - }, 2000); + window.setTimeout(function() { + page.evaluate(function() { + if (document.querySelector('#chat')) {document.querySelector('#chat').style.display = 'none';} + if (document.querySelector('#chatcontrols')) {document.querySelector('#chatcontrols').style.display = 'none';} + if (document.querySelector('#chatinput')) {document.querySelector('#chatinput').style.display = 'none';} + if (document.querySelector('#updatestatus')) {document.querySelector('#updatestatus').style.display = 'none';} + if (document.querySelector('#sidebartoggle')) {document.querySelector('#sidebartoggle').style.display = 'none';} + if (document.querySelector('#scrollwrapper')) {document.querySelector('#scrollwrapper').style.display = 'none';} + if (document.querySelector('.leaflet-control-container')) {document.querySelector('.leaflet-control-container').style.display = 'none';} + }); + }, 2000); } -/** - * Prepare map for screenshooting. Make screenshots same width and height with map_canvas - * If IITC, also set width and height - * @param {boolean} iitcz - * @param {number} widthz - * @param {number} heightz - */ function prepare(widthz, heightz) { window.setTimeout(function() { page.evaluate(function(w, h) { @@ -212,10 +176,7 @@ function prepare(widthz, heightz) { }, 4000); } -/** - * Sets element bounds - * @param selector - */ + function setElementBounds(selector) { page.clipRect = page.evaluate(function(selector) { var clipRect = document.querySelector(selector).getBoundingClientRect(); @@ -262,41 +223,38 @@ function getDateTime(format) { } function addTimestamp(time) { - page.evaluate(function(dateTime) { - var water = document.createElement('p'); - water.id='watermark-ice'; - water.innerHTML = dateTime; - water.style.position = 'absolute'; - water.style.color = '#3A539B'; - water.style.top = '0'; - water.style.zIndex = '4404'; - water.style.marginTop = '0'; - water.style.paddingTop = '0'; - water.style.left = '0'; - water.style.fontSize = '40px'; - water.style.opacity = '0.8'; - water.style.fontFamily = 'monospace'; - water.style.textShadow = '2px 2px 5px #111717'; - document.querySelectorAll('body')[0].appendChild(water); - }, time); + page.evaluate(function(dateTime) { + var water = document.createElement('p'); + water.id='watermark-ice'; + water.innerHTML = dateTime; + water.style.position = 'absolute'; + water.style.color = '#3A539B'; + water.style.top = '0'; + water.style.zIndex = '4404'; + water.style.marginTop = '0'; + water.style.paddingTop = '0'; + water.style.left = '0'; + water.style.fontSize = '40px'; + water.style.opacity = '0.8'; + water.style.fontFamily = 'monospace'; + water.style.textShadow = 'rgb(3, 3, 3) 7px 5px 9px'; + document.querySelectorAll('body')[0].appendChild(water); + }, time); } /** * Main function. */ function main() { - system.stdout.writeLine('main...'); - if (true){ - page.evaluate(function() { - if (document.getElementById('watermark-ice')) { - var oldStamp = document.getElementById('watermark-ice'); - oldStamp.parentNode.removeChild(oldStamp); - } - }); - } + page.evaluate(function() { + if (document.getElementById('watermark-ice')) { + var oldStamp = document.getElementById('watermark-ice'); + oldStamp.parentNode.removeChild(oldStamp); + } + }); window.setTimeout(function() { addTimestamp(getDateTime(0)); file = filepath; s(file); - }, 5000); + }, 3000); } diff --git a/screencap_intel.js b/screencap_intel.js @@ -1,5 +1,5 @@ var system = require('system') -var args = require('system').args; +var args = system.args; var page = require('webpage').create(); var fs = require('fs'); if (args.length === 1) { @@ -18,31 +18,12 @@ if (args.length === 1) { var IntelURL = args[3]; var filepath = args[4]; var search = args[5]; - console.log(search) - system.stdout.writeLine(filepath); } } } -addCookies(SACSID,CSRF) -afterCookieLogin(IntelURL, search) - -function waitFor ($config) { - $config._start = $config._start || new Date(); - - if ($config.timeout && new Date - $config._start > $config.timeout) { - if ($config.error) $config.error(); - if ($config.debug) console.log('timedout ' + (new Date - $config._start) + 'ms'); - return; - } - - if ($config.check()) { - if ($config.debug) console.log('success ' + (new Date - $config._start) + 'ms'); - return $config.success(); - } - - setTimeout(waitFor, $config.interval || 0, $config); -} +addCookies(SACSID,CSRF); +afterCookieLogin(IntelURL, search); function addCookies(sacsid, csrf) { phantom.addCookie({ @@ -61,26 +42,30 @@ function addCookies(sacsid, csrf) { }); } +function waitFor ($config) { + $config._start = $config._start || new Date(); + if ($config.timeout && new Date - $config._start > $config.timeout) { + if ($config.error) $config.error(); + if ($config.debug) console.log('timedout ' + (new Date - $config._start) + 'ms'); + return; + } + if ($config.check()) { + if ($config.debug) console.log('success ' + (new Date - $config._start) + 'ms'); + return $config.success(); + } + setTimeout(waitFor, $config.interval || 0, $config); +} -/** - * Does all stuff needed after cookie authentication - * @since 3.1.0 - */ function afterCookieLogin(IntelURL, search) { page.open(IntelURL, function(status) { if (status !== 'success') {quit('unable to connect to remote server')} - if(!isSignedIn()) { - if(fs.exists('.iced_cookies')) { - fs.remove('.iced_cookies'); - } - } setTimeout(function() { waitFor({ timeout: 120000, check: function () { return page.evaluate(function() { - if (document.querySelector('#percent_text').textContent == "90") { + if (document.querySelector('#percent_text').textContent.indexOf('90') != -1) { if (!document.getElementById("loading_msg").style.display){ return true; }else{ @@ -100,91 +85,58 @@ function afterCookieLogin(IntelURL, search) { main(); }, error: function () { - system.stdout.writeLine('map did not finish loading in time...'); page.evaluate(function() { document.querySelector("#filters_container").style.display= 'none'; }); hideDebris(); prepare('1920', '1080', search); main(); - } // optional + } }); }, "5000"); }); } -/** - * Checks if user is signed in by looking for the "Sign in" button - * @returns {boolean} - * @since 3.2.0 - */ -function isSignedIn() { - return page.evaluate(function() { - return document.getElementsByTagName('a')[0].innerText.trim() !== 'Sign in'; - }); -} - -function storeCookies() { - var cookies = page.cookies; - fs.write('.iced_cookies', '', 'w'); - for(var i in cookies) { - fs.write('.iced_cookies', cookies[i].name + '=' + cookies[i].value +'\n', 'a'); - } -} - function s(file) { page.render(file); phantom.exit(0); } function hideDebris() { - system.stdout.writeLine('hideDebris...'); - page.evaluate(function() { - if (document.querySelector('#comm')) {document.querySelector('#comm').style.display = 'none';} - if (document.querySelector('#player_stats')) {document.querySelector('#player_stats').style.display = 'none';} - if (document.querySelector('#game_stats')) {document.querySelector('#game_stats').style.display = 'none';} - if (document.querySelector('#geotools')) {document.querySelector('#geotools').style.display = 'none';} - if (document.querySelector('#header')) {document.querySelector('#header').style.display = 'none';} - if (document.querySelector('#snapcontrol')) {document.querySelector('#snapcontrol').style.display = 'none';} - if (document.querySelectorAll('.img_snap')[0]) {document.querySelectorAll('.img_snap')[0].style.display = 'none';} - if (document.querySelector('#display_msg_text')) {document.querySelector('#display_msg_text').style.display = 'none';} - }); - page.evaluate(function() { - var hide = document.querySelectorAll('.gmnoprint'); - for (var index = 0; index < hide.length; ++index) { - hide[index].style.display = 'none'; - } - }); + page.evaluate(function() { + if (document.querySelector('#comm')) {document.querySelector('#comm').style.display = 'none';} + if (document.querySelector('#player_stats')) {document.querySelector('#player_stats').style.display = 'none';} + if (document.querySelector('#game_stats')) {document.querySelector('#game_stats').style.display = 'none';} + if (document.querySelector('#geotools')) {document.querySelector('#geotools').style.display = 'none';} + if (document.querySelector('#header')) {document.querySelector('#header').style.display = 'none';} + if (document.querySelector('#snapcontrol')) {document.querySelector('#snapcontrol').style.display = 'none';} + if (document.querySelectorAll('.img_snap')[0]) {document.querySelectorAll('.img_snap')[0].style.display = 'none';} + if (document.querySelector('#display_msg_text')) {document.querySelector('#display_msg_text').style.display = 'none';} + }); + page.evaluate(function() { + var hide = document.querySelectorAll('.gmnoprint'); + for (var index = 0; index < hide.length; ++index) { + hide[index].style.display = 'none'; + } + }); } -/** - * Prepare map for screenshooting. Make screenshots same width and height with map_canvas - * If IITC, also set width and height - * @param {boolean} iitcz - * @param {number} widthz - * @param {number} heightz - */ function prepare(widthz, heightz, search) { - system.stdout.writeLine('prepare...'); - if (search == "nix") { - var selector = "#map_canvas"; - setElementBounds(selector); - }else{ - page.evaluate(function(search) { - if (document.querySelector('#geocode')){ - document.getElementById("address").value=search; - document.querySelector("input[value=Search]").click(); - } - }, search); - var selector = "#map_canvas"; - setElementBounds(selector); - } + if (search == "nix") { + var selector = "#map_canvas"; + setElementBounds(selector); + }else{ + page.evaluate(function(search) { + if (document.querySelector('#geocode')){ + document.getElementById("address").value=search; + document.querySelector("input[value=Search]").click(); + } + }, search); + var selector = "#map_canvas"; + setElementBounds(selector); + } } -/** - * Sets element bounds - * @param selector - */ function setElementBounds(selector) { page.clipRect = page.evaluate(function(selector) { var clipRect = document.querySelector(selector).getBoundingClientRect(); @@ -197,10 +149,6 @@ function setElementBounds(selector) { }, selector); } -/** - * Checks if human presence not detected and makes a human present - * @since 2.3.0 - */ function humanPresence() { var outside = page.evaluate(function() { return !!(document.getElementById('butterbar') && (document.getElementById('butterbar').style.display !== 'none')); @@ -247,37 +195,34 @@ function getDateTime(format) { } function addTimestamp(time) { - page.evaluate(function(dateTime) { - var water = document.createElement('p'); - water.id='watermark-ice'; - water.innerHTML = dateTime; - water.style.position = 'absolute'; - water.style.color = 'orange'; - water.style.top = '0'; - water.style.left = '0'; - water.style.fontSize = '40px'; - water.style.opacity = '0.8'; - water.style.marginTop = '0'; - water.style.paddingTop = '0'; - water.style.fontFamily = 'monospace'; - water.style.textShadow = '2px 2px 5px #111717'; - document.querySelector('#map_canvas').appendChild(water); - }, time); + page.evaluate(function(dateTime) { + var water = document.createElement('p'); + water.id='watermark-ice'; + water.innerHTML = dateTime; + water.style.position = 'absolute'; + water.style.color = 'orange'; + water.style.top = '0'; + water.style.left = '0'; + water.style.fontSize = '40px'; + water.style.opacity = '0.8'; + water.style.marginTop = '0'; + water.style.paddingTop = '0'; + water.style.fontFamily = 'monospace'; + water.style.textShadow = '2px 2px 5px #111717'; + document.querySelector('#map_canvas').appendChild(water); + }, time); } /** * Main function. */ function main() { - system.stdout.writeLine('main...'); - if (true){ - page.evaluate(function() { - if (document.getElementById('watermark-ice')) { - var oldStamp = document.getElementById('watermark-ice'); - oldStamp.parentNode.removeChild(oldStamp); - } - }); - } + page.evaluate(function() { + if (document.getElementById('watermark-ice')) { + var oldStamp = document.getElementById('watermark-ice'); + oldStamp.parentNode.removeChild(oldStamp); + } + }); humanPresence(); window.setTimeout(function() { addTimestamp(getDateTime(0));