From c81666d751b9ca8cd338760fdf920194fabf4ee4 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Sat, 6 Aug 2016 22:01:02 -0700 Subject: [PATCH 01/32] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5dac1a1497..ea02cdfe66 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ -# PokemonGo-Bot +# PokemonGo-Bot (Working) PokemonGo bot is a project created by the [PokemonGoF](https://github.com/PokemonGoF) team. The project is currently setup in two main branches. `dev` and `master`. -## Where to get the dll/so ? +## Please submit PR to [Dev branch](https://github.com/PokemonGoF/PokemonGo-Bot/tree/dev) + +## Where to get the dll/so ? A help channel is comming. You need grab them from internet. We use [Slack](https://slack.com) as a web chat. [Click here to join the chat!](https://pokemongo-bot.herokuapp.com) From ad6c21aa1dc0902c62e4001f9a81ce1fcd8a9f59 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Sun, 7 Aug 2016 00:01:55 -0700 Subject: [PATCH 02/32] Update README.md (#2685) --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1e56579ca8..ba131d5c39 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ To ensure that all updates are documented - [@eggins](https://github.com/eggins) ## Credits - [tejado](https://github.com/tejado) many thanks for the API +- [U6 Group](http://pgoapi.com) for the U6 - [Mila432](https://github.com/Mila432/Pokemon_Go_API) for the login secrets - [elliottcarlson](https://github.com/elliottcarlson) for the Google Auth PR - [AeonLucid](https://github.com/AeonLucid/POGOProtos) for improved protos From fddf30dd64322219c945a1aba3483f603ce55bcc Mon Sep 17 00:00:00 2001 From: garyking Date: Sun, 7 Aug 2016 12:13:59 -0400 Subject: [PATCH 03/32] Update Readme (#2847) Fix typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ba131d5c39..f269d43e6d 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ The project is currently setup in two main branches. `dev` and `master`. ## Please submit PR to [Dev branch](https://github.com/PokemonGoF/PokemonGo-Bot/tree/dev) -## Where to get the dll/so ? A help channel is comming. -You need grab them from internet. +## Where to get the DLL/SO? A help channel is coming. +You need to grab them from the Internet. We use [Slack](https://slack.com) as a web chat. [Click here to join the chat!](https://pokemongo-bot.herokuapp.com) From 2750255a974ac1d2c83db26a47b19092d45293d4 Mon Sep 17 00:00:00 2001 From: Jeremy Bi Date: Mon, 8 Aug 2016 02:22:13 +0800 Subject: [PATCH 04/32] Keep track of how many pokemon released (#2884) --- pokemongo_bot/cell_workers/transfer_pokemon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pokemongo_bot/cell_workers/transfer_pokemon.py b/pokemongo_bot/cell_workers/transfer_pokemon.py index c48e17b20d..5c1d30fae7 100644 --- a/pokemongo_bot/cell_workers/transfer_pokemon.py +++ b/pokemongo_bot/cell_workers/transfer_pokemon.py @@ -187,6 +187,7 @@ def should_release_pokemon(self, pokemon_name, cp, iv, keep_best_mode = False): def release_pokemon(self, pokemon_name, cp, iv, pokemon_id): response_dict = self.bot.api.release_pokemon(pokemon_id=pokemon_id) + self.bot.metrics.released_pokemon() self.emit_event( 'pokemon_release', formatted='Exchanged {pokemon} [CP {cp}] [IV {iv}] for candy.', From 823ba83d950ea8777aa774de87bb98543cc4055b Mon Sep 17 00:00:00 2001 From: Bernardo Vale Date: Sun, 7 Aug 2016 15:29:42 -0300 Subject: [PATCH 05/32] Setting Library path to work with encrypt.so (#2899) Setting LD_LIBRARY_PATH on Dockerfile --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index e94035732a..dce398c63e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,4 +15,6 @@ RUN cd /tmp && wget "http://pgoapi.com/pgoencrypt.tar.gz" \ VOLUME ["/usr/src/app/web"] +ENV LD_LIBRARY_PATH /usr/src/app + ENTRYPOINT ["python", "pokecli.py"] From fc4e802dd760508e45c9e2f79cdc9704a514b896 Mon Sep 17 00:00:00 2001 From: Genesis Date: Sun, 7 Aug 2016 20:42:35 +0200 Subject: [PATCH 06/32] :sparkles: Added login and username to available stats (#2494) Added a player_data property in PokemonGoBot to access player data from outside Added unit tests for login and username stats Added tests for call args when updating the window title Added a platform-specific test for window title updating on win32 platform --- CONTRIBUTORS.md | 1 + pokemongo_bot/__init__.py | 9 +++ .../cell_workers/update_title_stats.py | 12 +++- tests/update_title_stats_test.py | 57 ++++++++++++------- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dfd8a24af2..5a57ad6f8a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -51,3 +51,4 @@ * matheussampaio * Abraxas000 * lucasfevi + * Moonlight-Angel diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 24aaeb4b2d..d0f034f110 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -39,6 +39,15 @@ def position(self): def position(self, position_tuple): self.api._position_lat, self.api._position_lng, self.api._position_alt = position_tuple + @property + def player_data(self): + """ + Returns the player data as received from the API. + :return: The player data. + :rtype: dict + """ + return self._player + def __init__(self, config): self.config = config self.fort_timeouts = dict() diff --git a/pokemongo_bot/cell_workers/update_title_stats.py b/pokemongo_bot/cell_workers/update_title_stats.py index 1f9b2a0e9b..43e55c260f 100644 --- a/pokemongo_bot/cell_workers/update_title_stats.py +++ b/pokemongo_bot/cell_workers/update_title_stats.py @@ -18,11 +18,13 @@ class UpdateTitleStats(BaseTask): "type": "UpdateTitleStats", "config": { "min_interval": 10, - "stats": ["uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"] + "stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"] } } Available stats : + - login : The account login (from the credentials). + - username : The trainer name (asked at first in-game connection). - uptime : The bot uptime. - km_walked : The kilometers walked since the bot started. - level : The current character's level. @@ -107,8 +109,7 @@ def _update_title(self, title, platform): :rtype: None :raise: RuntimeError: When the given platform isn't supported. """ - if platform == "linux" or platform == "linux2"\ - or platform == "cygwin": + if platform == "linux" or platform == "linux2" or platform == "cygwin": stdout.write("\x1b]2;{}\x07".format(title)) elif platform == "darwin": stdout.write("\033]0;{}\007".format(title)) @@ -145,6 +146,9 @@ def _get_stats_title(self, player_stats): metrics = self.bot.metrics metrics.capture_stats() runtime = metrics.runtime() + login = self.bot.config.username + player_data = self.bot.player_data + username = player_data.get('username', '?') distance_travelled = metrics.distance_travelled() current_level = int(player_stats.get('level', 0)) prev_level_xp = int(player_stats.get('prev_level_xp', 0)) @@ -172,6 +176,8 @@ def _get_stats_title(self, player_stats): # Create stats strings. available_stats = { + 'login': login, + 'username': username, 'uptime': 'Uptime : {}'.format(runtime), 'km_walked': '{:,.2f}km walked'.format(distance_travelled), 'level': 'Level {}'.format(current_level), diff --git a/tests/update_title_stats_test.py b/tests/update_title_stats_test.py index 699d736b7a..ba480f0151 100644 --- a/tests/update_title_stats_test.py +++ b/tests/update_title_stats_test.py @@ -1,6 +1,7 @@ import unittest +from sys import platform as _platform from datetime import datetime, timedelta -from mock import patch, MagicMock +from mock import call, patch, MagicMock from pokemongo_bot.cell_workers.update_title_stats import UpdateTitleStats from tests import FakeBot @@ -8,11 +9,11 @@ class UpdateTitleStatsTestCase(unittest.TestCase): config = { 'min_interval': 20, - 'stats': ['pokemon_evolved', 'pokemon_encountered', 'uptime', 'pokemon_caught', - 'stops_visited', 'km_walked', 'level', 'stardust_earned', 'level_completion', - 'xp_per_hour', 'pokeballs_thrown', 'highest_cp_pokemon', 'level_stats', - 'xp_earned', 'pokemon_unseen', 'most_perfect_pokemon', 'pokemon_stats', - 'pokemon_released'] + 'stats': ['login', 'username', 'pokemon_evolved', 'pokemon_encountered', 'uptime', + 'pokemon_caught', 'stops_visited', 'km_walked', 'level', 'stardust_earned', + 'level_completion', 'xp_per_hour', 'pokeballs_thrown', 'highest_cp_pokemon', + 'level_stats', 'xp_earned', 'pokemon_unseen', 'most_perfect_pokemon', + 'pokemon_stats', 'pokemon_released'] } player_stats = { 'level': 25, @@ -23,6 +24,8 @@ class UpdateTitleStatsTestCase(unittest.TestCase): def setUp(self): self.bot = FakeBot() + self.bot._player = {'username': 'Username'} + self.bot.config.username = 'Login' self.worker = UpdateTitleStats(self.bot, self.config) def mock_metrics(self): @@ -87,22 +90,37 @@ def test_next_update_after_update_title(self, mock_datetime): now + timedelta(seconds=self.config['min_interval'])) @patch('pokemongo_bot.cell_workers.update_title_stats.stdout') - def test_update_title_linux_osx(self, mock_stdout): - self.worker._update_title('', 'linux') + def test_update_title_linux_cygwin(self, mock_stdout): + self.worker._update_title('new title linux', 'linux') self.assertEqual(mock_stdout.write.call_count, 1) + self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title linux\x07')) - self.worker._update_title('', 'linux2') + self.worker._update_title('new title linux2', 'linux2') self.assertEqual(mock_stdout.write.call_count, 2) + self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title linux2\x07')) - self.worker._update_title('', 'darwin') + self.worker._update_title('new title cygwin', 'cygwin') self.assertEqual(mock_stdout.write.call_count, 3) + self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title cygwin\x07')) + + @patch('pokemongo_bot.cell_workers.update_title_stats.stdout') + def test_update_title_darwin(self, mock_stdout): + self.worker._update_title('new title darwin', 'darwin') + + self.assertEqual(mock_stdout.write.call_count, 1) + self.assertEqual(mock_stdout.write.call_args, call('\033]0;new title darwin\007')) + + @unittest.skipUnless(_platform.startswith("win"), "requires Windows") + @patch('pokemongo_bot.cell_workers.update_title_stats.ctypes') + def test_update_title_win32(self, mock_ctypes): + self.worker._update_title('new title win32', 'win32') - @unittest.skip("Didn't find a way to mock ctypes.windll.kernel32.SetConsoleTitleA") - def test_update_title_win32(self): - self.worker._update_title('', 'win32') + self.assertEqual(mock_ctypes.windll.kernel32.SetConsoleTitleA.call_count, 1) + self.assertEqual(mock_ctypes.windll.kernel32.SetConsoleTitleA.call_args, + call('new title win32')) def test_get_stats_title_player_stats_none(self): title = self.worker._get_stats_title(None) @@ -119,12 +137,13 @@ def test_get_stats(self): self.mock_metrics() title = self.worker._get_stats_title(self.player_stats) - expected = 'Evolved 12 pokemon | Encountered 130 pokemon | Uptime : 15:42:13 | ' \ - 'Caught 120 pokemon | Visited 220 stops | 42.05km walked | Level 25 | ' \ - 'Earned 24,069 Stardust | 87,500 / 150,000 XP (58%) | 1,337 XP/h | ' \ - 'Threw 145 pokeballs | Highest CP pokemon : highest_cp | ' \ - 'Level 25 (87,500 / 150,000, 58%) | +424,242 XP | ' \ - 'Encountered 3 new pokemon | Most perfect pokemon : most_perfect | ' \ + expected = 'Login | Username | Evolved 12 pokemon | Encountered 130 pokemon | ' \ + 'Uptime : 15:42:13 | Caught 120 pokemon | Visited 220 stops | ' \ + '42.05km walked | Level 25 | Earned 24,069 Stardust | ' \ + '87,500 / 150,000 XP (58%) | 1,337 XP/h | Threw 145 pokeballs | ' \ + 'Highest CP pokemon : highest_cp | Level 25 (87,500 / 150,000, 58%) | ' \ + '+424,242 XP | Encountered 3 new pokemon | ' \ + 'Most perfect pokemon : most_perfect | ' \ 'Encountered 130 pokemon, 120 caught, 30 released, 12 evolved, ' \ '3 never seen before | Released 30 pokemon' From e5b7eda6cd7054baadbd6440bf9b94d3d5cd88e3 Mon Sep 17 00:00:00 2001 From: mjmadsen Date: Sun, 7 Aug 2016 16:55:59 -0500 Subject: [PATCH 07/32] [dev] small fixes (#2912) * Fixed emit_event typo * Update CONTRIBUTORS.md * Changed initialization location for "bot" We use bot in main exception on 128 * Update pokecli.py --- CONTRIBUTORS.md | 1 + pokecli.py | 3 ++- pokemongo_bot/cell_workers/evolve_pokemon.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 5a57ad6f8a..ee5aa7063d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -52,3 +52,4 @@ * Abraxas000 * lucasfevi * Moonlight-Angel + * mjmadsen diff --git a/pokecli.py b/pokecli.py index 38f33a035c..24a0f38ee3 100644 --- a/pokecli.py +++ b/pokecli.py @@ -52,8 +52,9 @@ logger.setLevel(logging.INFO) def main(): + bot = False + try: - bot = False logger.info('PokemonGO Bot v1.0') sys.stdout = codecs.getwriter('utf8')(sys.stdout) sys.stderr = codecs.getwriter('utf8')(sys.stderr) diff --git a/pokemongo_bot/cell_workers/evolve_pokemon.py b/pokemongo_bot/cell_workers/evolve_pokemon.py index 74eb0abf79..c3903a685d 100644 --- a/pokemongo_bot/cell_workers/evolve_pokemon.py +++ b/pokemongo_bot/cell_workers/evolve_pokemon.py @@ -59,7 +59,7 @@ def _should_run(self): if result is 1: # Request success self.emit_event( 'used_lucky_egg', - formmated='Used lucky egg ({amount_left} left).', + formatted='Used lucky egg ({amount_left} left).', data={ 'amount_left': lucky_egg_count - 1 } From 5b3fe3d187b6b5fdfdede00f064bf7bcbd5a484d Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Sun, 7 Aug 2016 15:18:09 -0700 Subject: [PATCH 08/32] Dev merge to master (#2939) * Adding plugin support (#2679) * Adding plugin support * Adding an empty __init__.py * Moving the base task to the project root (#2702) * Moving the base task to the project root * Moving the base class more * Changing the import again * Adding a heartbeat to the analytics (#2709) * Adding a heartbeat to the analytics * Heartbeat every 30 seconds, not every 5 * Don't double track clients * Fix 'local variable 'bot' referenced before assignment' * Providing an error if tasks don't work for the given api (#2732) * Fix for utf8 encoding when catching lured pokemon (#2720) * Fixing lure pokestop encoding * fixing lure encoding * Fix For catchable not being displayed on the web (#2719) * Fix For catchable not being displayed on the web * Update catch_visible_pokemon.py * Added encrypt.so compilation process to Dockerfile (#2695) * OS Detection for encrypt lib (#2768) Fix 32bit check, darwin and linux use the same file Make it a function Check if file exists, if not show error Define file_name first Fix return Check if file exists, if not show error Print info about paths Fix for 32/64bit detection * Fix Typo in unexpected_response_retry (#2531) fixes #2525 #2523 * Revert "changing license from MIT to GPLv3" This reverts commit 69fb64f2bf7c12e28c2bb6d2b636c6af55822448. * When the google analytics domain is blocked the bot crashed. (#2764) With a simple try / except this can be solved. Fix dirty catch all * Fixes #2698 - Prevents "Possibly searching too often" error after re-login. (#2771) * Fixes #2698 - Added api.activate_signature call to prevent issue after re-login. - Also replaced deprecated log call with event_manager emit to prevent exception being thrown. * Modified to use OS detected library path as per PR #2768 * Support loading plugins from .zip files (#2766) * Keep track of how many pokemon released (#2884) * Setting Library path to work with encrypt.so (#2899) Setting LD_LIBRARY_PATH on Dockerfile * :sparkles: Added login and username to available stats (#2494) Added a player_data property in PokemonGoBot to access player data from outside Added unit tests for login and username stats Added tests for call args when updating the window title Added a platform-specific test for window title updating on win32 platform * [dev] small fixes (#2912) * Fixed emit_event typo * Update CONTRIBUTORS.md * Changed initialization location for "bot" We use bot in main exception on 128 * Update pokecli.py --- CONTRIBUTORS.md | 2 + Dockerfile | 9 +- LICENSE | 676 +----------------- pokecli.py | 9 + pokemongo_bot/__init__.py | 52 +- pokemongo_bot/{cell_workers => }/base_task.py | 1 + pokemongo_bot/cell_workers/__init__.py | 3 +- .../cell_workers/catch_lured_pokemon.py | 8 +- .../cell_workers/catch_visible_pokemon.py | 8 +- .../cell_workers/collect_level_up_reward.py | 4 +- pokemongo_bot/cell_workers/evolve_pokemon.py | 5 +- pokemongo_bot/cell_workers/follow_cluster.py | 3 +- pokemongo_bot/cell_workers/follow_path.py | 4 +- pokemongo_bot/cell_workers/follow_spiral.py | 4 +- pokemongo_bot/cell_workers/handle_soft_ban.py | 4 +- pokemongo_bot/cell_workers/incubate_eggs.py | 4 +- pokemongo_bot/cell_workers/move_to_fort.py | 3 +- .../cell_workers/move_to_map_pokemon.py | 4 +- .../cell_workers/nickname_pokemon.py | 4 +- .../cell_workers/pokemon_catch_worker.py | 2 +- pokemongo_bot/cell_workers/recycle_items.py | 4 +- pokemongo_bot/cell_workers/sleep_schedule.py | 3 +- pokemongo_bot/cell_workers/spin_fort.py | 4 +- .../cell_workers/transfer_pokemon.py | 5 +- .../cell_workers/update_title_stats.py | 15 +- pokemongo_bot/health_record/bot_event.py | 48 +- pokemongo_bot/plugin_loader.py | 33 + pokemongo_bot/test/plugin_loader_test.py | 28 + pokemongo_bot/test/resources/__init__.py | 0 .../test/resources/plugin_fixture/__init__.py | 2 + .../resources/plugin_fixture/fake_task.py | 7 + .../plugin_fixture/unsupported_api_task.py | 7 + .../test/resources/plugin_fixture_test.zip | Bin 0 -> 3412 bytes pokemongo_bot/tree_config_builder.py | 33 +- tests/base_task_test.py | 2 +- tests/tree_config_builder_test.py | 55 +- tests/update_title_stats_test.py | 57 +- 37 files changed, 367 insertions(+), 745 deletions(-) rename pokemongo_bot/{cell_workers => }/base_task.py (96%) create mode 100644 pokemongo_bot/plugin_loader.py create mode 100644 pokemongo_bot/test/plugin_loader_test.py create mode 100644 pokemongo_bot/test/resources/__init__.py create mode 100644 pokemongo_bot/test/resources/plugin_fixture/__init__.py create mode 100644 pokemongo_bot/test/resources/plugin_fixture/fake_task.py create mode 100644 pokemongo_bot/test/resources/plugin_fixture/unsupported_api_task.py create mode 100644 pokemongo_bot/test/resources/plugin_fixture_test.zip diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dfd8a24af2..ee5aa7063d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -51,3 +51,5 @@ * matheussampaio * Abraxas000 * lucasfevi + * Moonlight-Angel + * mjmadsen diff --git a/Dockerfile b/Dockerfile index 58c45cd02f..dce398c63e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,14 @@ RUN echo $timezone > /etc/timezone \ RUN apt-get update \ && apt-get install -y python-protobuf +RUN cd /tmp && wget "http://pgoapi.com/pgoencrypt.tar.gz" \ + && tar zxvf pgoencrypt.tar.gz \ + && cd pgoencrypt/src \ + && make \ + && cp libencrypt.so /usr/src/app/encrypt.so VOLUME ["/usr/src/app/web"] -ENTRYPOINT ["python", "pokecli.py"] \ No newline at end of file +ENV LD_LIBRARY_PATH /usr/src/app + +ENTRYPOINT ["python", "pokecli.py"] diff --git a/LICENSE b/LICENSE index 9cecc1d466..d71a77b230 100644 --- a/LICENSE +++ b/LICENSE @@ -1,674 +1,8 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 +The MIT License (MIT) +Copyright (c) 2016 PokemonGoF Team - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - Preamble +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - {one line to give the program's name and a brief idea of what it does.} - Copyright (C) {year} {name of author} - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - {project} Copyright (C) {year} {fullname} - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/pokecli.py b/pokecli.py index 3d1b3736e7..24a0f38ee3 100644 --- a/pokecli.py +++ b/pokecli.py @@ -40,6 +40,7 @@ from pokemongo_bot import PokemonGoBot, TreeConfigBuilder from pokemongo_bot.health_record import BotEvent +from pokemongo_bot.plugin_loader import PluginLoader if sys.version_info >= (2, 7, 9): ssl._create_default_https_context = ssl._create_unverified_context @@ -51,6 +52,8 @@ logger.setLevel(logging.INFO) def main(): + bot = False + try: logger.info('PokemonGO Bot v1.0') sys.stdout = codecs.getwriter('utf8')(sys.stdout) @@ -73,6 +76,7 @@ def main(): tree = TreeConfigBuilder(bot, config.raw_tasks).build() bot.workers = tree bot.metrics.capture_stats() + bot.health_record = health_record bot.event_manager.emit( 'bot_start', @@ -384,6 +388,7 @@ def init_config(): config.release = load.get('release', {}) config.action_wait_max = load.get('action_wait_max', 4) config.action_wait_min = load.get('action_wait_min', 1) + config.plugins = load.get('plugins', []) config.raw_tasks = load.get('tasks', []) config.vips = load.get('vips', {}) @@ -439,6 +444,10 @@ def task_configuration_error(flag_name): parser.error("--catch_randomize_spin_factor is out of range! (should be 0 <= catch_randomize_spin_factor <= 1)") return None + plugin_loader = PluginLoader() + for plugin in config.plugins: + plugin_loader.load_path(plugin) + # create web dir if not exists try: os.makedirs(web_dir) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 1d318f473b..d0f034f110 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -15,6 +15,8 @@ from pgoapi.utilities import f2i, get_cell_ids import cell_workers +from base_task import BaseTask +from plugin_loader import PluginLoader from api_wrapper import ApiWrapper from cell_workers.utils import distance from event_manager import EventManager @@ -25,9 +27,9 @@ from pokemongo_bot.socketio_server.runner import SocketIoRunner from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl from worker_result import WorkerResult -from tree_config_builder import ConfigException, TreeConfigBuilder - - +from tree_config_builder import ConfigException, MismatchTaskApiVersion, TreeConfigBuilder +from sys import platform as _platform +import struct class PokemonGoBot(object): @property def position(self): @@ -37,6 +39,15 @@ def position(self): def position(self, position_tuple): self.api._position_lat, self.api._position_lng, self.api._position_alt = position_tuple + @property + def player_data(self): + """ + Returns the player data as received from the API. + :return: The player data. + :rtype: dict + """ + return self._player + def __init__(self, config): self.config = config self.fort_timeouts = dict() @@ -383,6 +394,7 @@ def _register_events(self): self.event_manager.register_event('unset_pokemon_nickname') def tick(self): + self.health_record.heartbeat() self.cell = self.get_meta_cell() self.tick_count += 1 @@ -544,11 +556,17 @@ def check_session(self, position): self.api._auth_provider._ticket_expire / 1000 - time.time() if remaining_time < 60: - self.logger.info("Session stale, re-logging in", 'yellow') + self.event_manager.emit( + 'api_error', + sender=self, + level='info', + formatted='Session stale, re-logging in.' + ) position = self.position self.api = ApiWrapper() self.position = position self.login() + self.api.activate_signature(self.get_encryption_lib()) @staticmethod def is_numeric(s): @@ -588,6 +606,29 @@ def login(self): formatted="Login successful." ) + def get_encryption_lib(self): + file_name = '' + if _platform == "linux" or _platform == "linux2" or _platform == "darwin": + file_name = 'encrypt.so' + elif _platform == "Windows" or _platform == "win32": + # Check if we are on 32 or 64 bit + if sys.maxsize > 2**32: + file_name = 'encrypt_64.dll' + else: + file_name = 'encrypt.dll' + + path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + full_path = path + '/'+ file_name + + if not os.path.isfile(full_path): + self.logger.error(file_name + ' is not found! Please place it in the bots root directory.') + self.logger.info('Platform: '+ _platform + ' Bot root directory: '+ path) + sys.exit(1) + else: + self.logger.info('Found '+ file_name +'! Platform: ' + _platform + ' Bot root directory: ' + path) + + return full_path + def _setup_api(self): # instantiate pgoapi self.api = ApiWrapper() @@ -599,8 +640,7 @@ def _setup_api(self): # chain subrequests (methods) into one RPC call self._print_character_info() - - self.api.activate_signature("encrypt.so") + self.api.activate_signature(self.get_encryption_lib()) self.logger.info('') self.update_inventory() # send empty map_cells and then our position diff --git a/pokemongo_bot/cell_workers/base_task.py b/pokemongo_bot/base_task.py similarity index 96% rename from pokemongo_bot/cell_workers/base_task.py rename to pokemongo_bot/base_task.py index ac48b9a676..22bbedf4e8 100644 --- a/pokemongo_bot/cell_workers/base_task.py +++ b/pokemongo_bot/base_task.py @@ -2,6 +2,7 @@ class BaseTask(object): + TASK_API_VERSION = 1 def __init__(self, bot, config): self.bot = bot diff --git a/pokemongo_bot/cell_workers/__init__.py b/pokemongo_bot/cell_workers/__init__.py index bc6638d1fd..68d181947a 100644 --- a/pokemongo_bot/cell_workers/__init__.py +++ b/pokemongo_bot/cell_workers/__init__.py @@ -15,7 +15,6 @@ from follow_path import FollowPath from follow_spiral import FollowSpiral from collect_level_up_reward import CollectLevelUpReward -from base_task import BaseTask from follow_cluster import FollowCluster from sleep_schedule import SleepSchedule -from update_title_stats import UpdateTitleStats \ No newline at end of file +from update_title_stats import UpdateTitleStats diff --git a/pokemongo_bot/cell_workers/catch_lured_pokemon.py b/pokemongo_bot/cell_workers/catch_lured_pokemon.py index bf2d45bb4b..10a046dce9 100644 --- a/pokemongo_bot/cell_workers/catch_lured_pokemon.py +++ b/pokemongo_bot/cell_workers/catch_lured_pokemon.py @@ -3,10 +3,12 @@ from pokemongo_bot.cell_workers.utils import fort_details from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class CatchLuredPokemon(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def work(self): lured_pokemon = self.get_lured_pokemon() if lured_pokemon: @@ -22,7 +24,7 @@ def get_lured_pokemon(self): details = fort_details(self.bot, fort_id=fort['id'], latitude=fort['latitude'], longitude=fort['longitude']) - fort_name = details.get('name', 'Unknown').encode('utf8', 'replace') + fort_name = details.get('name', 'Unknown') encounter_id = fort.get('lure_info', {}).get('encounter_id', None) @@ -30,7 +32,7 @@ def get_lured_pokemon(self): result = { 'encounter_id': encounter_id, 'fort_id': fort['id'], - 'fort_name': fort_name, + 'fort_name': u"{}".format(fort_name), 'latitude': fort['latitude'], 'longitude': fort['longitude'] } diff --git a/pokemongo_bot/cell_workers/catch_visible_pokemon.py b/pokemongo_bot/cell_workers/catch_visible_pokemon.py index 1103815a4c..1bfed225df 100644 --- a/pokemongo_bot/cell_workers/catch_visible_pokemon.py +++ b/pokemongo_bot/cell_workers/catch_visible_pokemon.py @@ -1,11 +1,13 @@ import json -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker from utils import distance class CatchVisiblePokemon(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def work(self): if 'catchable_pokemons' in self.bot.cell and len(self.bot.cell['catchable_pokemons']) > 0: # Sort all by distance from current pos- eventually this should @@ -14,9 +16,9 @@ def work(self): key= lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude']) ) - + user_web_catchable = 'web/catchable-{}.json'.format(self.bot.config.username) for pokemon in self.bot.cell['catchable_pokemons']: - with open('user_web_catchable', 'w') as outfile: + with open(user_web_catchable, 'w') as outfile: json.dump(pokemon, outfile) self.emit_event( 'catchable_pokemon', diff --git a/pokemongo_bot/cell_workers/collect_level_up_reward.py b/pokemongo_bot/cell_workers/collect_level_up_reward.py index 304818fe2b..950f450660 100644 --- a/pokemongo_bot/cell_workers/collect_level_up_reward.py +++ b/pokemongo_bot/cell_workers/collect_level_up_reward.py @@ -1,7 +1,9 @@ -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class CollectLevelUpReward(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + current_level = 0 previous_level = 0 diff --git a/pokemongo_bot/cell_workers/evolve_pokemon.py b/pokemongo_bot/cell_workers/evolve_pokemon.py index 911d6a1f67..c3903a685d 100644 --- a/pokemongo_bot/cell_workers/evolve_pokemon.py +++ b/pokemongo_bot/cell_workers/evolve_pokemon.py @@ -1,9 +1,10 @@ from pokemongo_bot.human_behaviour import sleep from pokemongo_bot.item_list import Item -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class EvolvePokemon(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 def initialize(self): self.api = self.bot.api @@ -58,7 +59,7 @@ def _should_run(self): if result is 1: # Request success self.emit_event( 'used_lucky_egg', - formmated='Used lucky egg ({amount_left} left).', + formatted='Used lucky egg ({amount_left} left).', data={ 'amount_left': lucky_egg_count - 1 } diff --git a/pokemongo_bot/cell_workers/follow_cluster.py b/pokemongo_bot/cell_workers/follow_cluster.py index 02d3880a7e..8448fcf742 100644 --- a/pokemongo_bot/cell_workers/follow_cluster.py +++ b/pokemongo_bot/cell_workers/follow_cluster.py @@ -1,9 +1,10 @@ from pokemongo_bot.step_walker import StepWalker from pokemongo_bot.cell_workers.utils import distance from pokemongo_bot.cell_workers.utils import find_biggest_cluster -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class FollowCluster(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 def initialize(self): self.is_at_destination = False diff --git a/pokemongo_bot/cell_workers/follow_path.py b/pokemongo_bot/cell_workers/follow_path.py index 04eb817593..6e183ed1d7 100644 --- a/pokemongo_bot/cell_workers/follow_path.py +++ b/pokemongo_bot/cell_workers/follow_path.py @@ -3,7 +3,7 @@ import gpxpy import gpxpy.gpx import json -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from pokemongo_bot.cell_workers.utils import distance, i2f, format_dist from pokemongo_bot.human_behaviour import sleep from pokemongo_bot.step_walker import StepWalker @@ -11,6 +11,8 @@ class FollowPath(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def initialize(self): self.ptr = 0 self._process_config() diff --git a/pokemongo_bot/cell_workers/follow_spiral.py b/pokemongo_bot/cell_workers/follow_spiral.py index 28b548d1ca..f175369e45 100644 --- a/pokemongo_bot/cell_workers/follow_spiral.py +++ b/pokemongo_bot/cell_workers/follow_spiral.py @@ -5,9 +5,11 @@ from pokemongo_bot.cell_workers.utils import distance, format_dist from pokemongo_bot.step_walker import StepWalker -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class FollowSpiral(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def initialize(self): self.steplimit = self.config.get("diameter", 4) self.step_size = self.config.get("step_size", 70) diff --git a/pokemongo_bot/cell_workers/handle_soft_ban.py b/pokemongo_bot/cell_workers/handle_soft_ban.py index 4f9d416e83..8018b7c33e 100644 --- a/pokemongo_bot/cell_workers/handle_soft_ban.py +++ b/pokemongo_bot/cell_workers/handle_soft_ban.py @@ -3,13 +3,15 @@ from pgoapi.utilities import f2i from pokemongo_bot.constants import Constants -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from pokemongo_bot.cell_workers import MoveToFort from pokemongo_bot.cell_workers.utils import distance from pokemongo_bot.worker_result import WorkerResult class HandleSoftBan(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def work(self): if not self.should_run(): return diff --git a/pokemongo_bot/cell_workers/incubate_eggs.py b/pokemongo_bot/cell_workers/incubate_eggs.py index 9e21b0d280..5761090ea5 100644 --- a/pokemongo_bot/cell_workers/incubate_eggs.py +++ b/pokemongo_bot/cell_workers/incubate_eggs.py @@ -1,8 +1,10 @@ from pokemongo_bot.human_behaviour import sleep -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class IncubateEggs(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + last_km_walked = 0 def initialize(self): diff --git a/pokemongo_bot/cell_workers/move_to_fort.py b/pokemongo_bot/cell_workers/move_to_fort.py index f43d1641e6..e4b4187d20 100644 --- a/pokemongo_bot/cell_workers/move_to_fort.py +++ b/pokemongo_bot/cell_workers/move_to_fort.py @@ -4,11 +4,12 @@ from pokemongo_bot.constants import Constants from pokemongo_bot.step_walker import StepWalker from pokemongo_bot.worker_result import WorkerResult -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from utils import distance, format_dist, fort_details class MoveToFort(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 def initialize(self): self.lure_distance = 0 diff --git a/pokemongo_bot/cell_workers/move_to_map_pokemon.py b/pokemongo_bot/cell_workers/move_to_map_pokemon.py index bce39e0143..08ff35f281 100644 --- a/pokemongo_bot/cell_workers/move_to_map_pokemon.py +++ b/pokemongo_bot/cell_workers/move_to_map_pokemon.py @@ -9,11 +9,13 @@ from pokemongo_bot.cell_workers.utils import distance, format_dist, format_time from pokemongo_bot.step_walker import StepWalker from pokemongo_bot.worker_result import WorkerResult -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker class MoveToMapPokemon(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def initialize(self): self.last_map_update = 0 self.pokemon_data = self.bot.pokemon_list diff --git a/pokemongo_bot/cell_workers/nickname_pokemon.py b/pokemongo_bot/cell_workers/nickname_pokemon.py index 29df15ae4a..cda206ad20 100644 --- a/pokemongo_bot/cell_workers/nickname_pokemon.py +++ b/pokemongo_bot/cell_workers/nickname_pokemon.py @@ -1,7 +1,9 @@ from pokemongo_bot.human_behaviour import sleep -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class NicknamePokemon(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def initialize(self): self.template = self.config.get('nickname_template','').lower().strip() if self.template == "{name}": diff --git a/pokemongo_bot/cell_workers/pokemon_catch_worker.py b/pokemongo_bot/cell_workers/pokemon_catch_worker.py index afa5578267..d676e9f0e2 100644 --- a/pokemongo_bot/cell_workers/pokemon_catch_worker.py +++ b/pokemongo_bot/cell_workers/pokemon_catch_worker.py @@ -3,7 +3,7 @@ import time from pokemongo_bot.human_behaviour import (normalized_reticle_size, sleep, spin_modifier) -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class PokemonCatchWorker(BaseTask): BAG_FULL = 'bag_full' diff --git a/pokemongo_bot/cell_workers/recycle_items.py b/pokemongo_bot/cell_workers/recycle_items.py index c28b2749b1..2c969913b0 100644 --- a/pokemongo_bot/cell_workers/recycle_items.py +++ b/pokemongo_bot/cell_workers/recycle_items.py @@ -1,9 +1,11 @@ import json import os -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from pokemongo_bot.tree_config_builder import ConfigException class RecycleItems(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def initialize(self): self.item_filter = self.config.get('item_filter', {}) self._validate_item_filter() diff --git a/pokemongo_bot/cell_workers/sleep_schedule.py b/pokemongo_bot/cell_workers/sleep_schedule.py index daaf0b8f1e..5a7d617e23 100644 --- a/pokemongo_bot/cell_workers/sleep_schedule.py +++ b/pokemongo_bot/cell_workers/sleep_schedule.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta from time import sleep from random import uniform -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class SleepSchedule(BaseTask): @@ -27,6 +27,7 @@ class SleepSchedule(BaseTask): duration_random_offset: (HH:MM) random offset of duration of sleep for this example the possible duration is 5:00-6:00 """ + SUPPORTED_TASK_API_VERSION = 1 LOG_INTERVAL_SECONDS = 600 SCHEDULING_MARGIN = timedelta(minutes=10) # Skip if next sleep is RESCHEDULING_MARGIN from now diff --git a/pokemongo_bot/cell_workers/spin_fort.py b/pokemongo_bot/cell_workers/spin_fort.py index 9572008241..e04a86dbc6 100644 --- a/pokemongo_bot/cell_workers/spin_fort.py +++ b/pokemongo_bot/cell_workers/spin_fort.py @@ -8,11 +8,13 @@ from pokemongo_bot.constants import Constants from pokemongo_bot.human_behaviour import sleep from pokemongo_bot.worker_result import WorkerResult -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from utils import distance, format_time, fort_details class SpinFort(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def should_run(self): if not self.bot.has_space_for_loot(): self.emit_event( diff --git a/pokemongo_bot/cell_workers/transfer_pokemon.py b/pokemongo_bot/cell_workers/transfer_pokemon.py index 70c5939c58..5c1d30fae7 100644 --- a/pokemongo_bot/cell_workers/transfer_pokemon.py +++ b/pokemongo_bot/cell_workers/transfer_pokemon.py @@ -1,10 +1,12 @@ import json from pokemongo_bot.human_behaviour import action_delay -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask class TransferPokemon(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + def work(self): pokemon_groups = self._release_pokemon_get_groups() for pokemon_id in pokemon_groups: @@ -185,6 +187,7 @@ def should_release_pokemon(self, pokemon_name, cp, iv, keep_best_mode = False): def release_pokemon(self, pokemon_name, cp, iv, pokemon_id): response_dict = self.bot.api.release_pokemon(pokemon_id=pokemon_id) + self.bot.metrics.released_pokemon() self.emit_event( 'pokemon_release', formatted='Exchanged {pokemon} [CP {cp}] [IV {iv}] for candy.', diff --git a/pokemongo_bot/cell_workers/update_title_stats.py b/pokemongo_bot/cell_workers/update_title_stats.py index 911d2efd4d..43e55c260f 100644 --- a/pokemongo_bot/cell_workers/update_title_stats.py +++ b/pokemongo_bot/cell_workers/update_title_stats.py @@ -2,7 +2,7 @@ from sys import stdout, platform as _platform from datetime import datetime, timedelta -from pokemongo_bot.cell_workers.base_task import BaseTask +from pokemongo_bot.base_task import BaseTask from pokemongo_bot.worker_result import WorkerResult from pokemongo_bot.tree_config_builder import ConfigException @@ -18,11 +18,13 @@ class UpdateTitleStats(BaseTask): "type": "UpdateTitleStats", "config": { "min_interval": 10, - "stats": ["uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"] + "stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"] } } Available stats : + - login : The account login (from the credentials). + - username : The trainer name (asked at first in-game connection). - uptime : The bot uptime. - km_walked : The kilometers walked since the bot started. - level : The current character's level. @@ -49,6 +51,7 @@ class UpdateTitleStats(BaseTask): stats : An array of stats to display and their display order (implicitly), see available stats above. """ + SUPPORTED_TASK_API_VERSION = 1 DEFAULT_MIN_INTERVAL = 10 DEFAULT_DISPLAYED_STATS = [] @@ -106,8 +109,7 @@ def _update_title(self, title, platform): :rtype: None :raise: RuntimeError: When the given platform isn't supported. """ - if platform == "linux" or platform == "linux2"\ - or platform == "cygwin": + if platform == "linux" or platform == "linux2" or platform == "cygwin": stdout.write("\x1b]2;{}\x07".format(title)) elif platform == "darwin": stdout.write("\033]0;{}\007".format(title)) @@ -144,6 +146,9 @@ def _get_stats_title(self, player_stats): metrics = self.bot.metrics metrics.capture_stats() runtime = metrics.runtime() + login = self.bot.config.username + player_data = self.bot.player_data + username = player_data.get('username', '?') distance_travelled = metrics.distance_travelled() current_level = int(player_stats.get('level', 0)) prev_level_xp = int(player_stats.get('prev_level_xp', 0)) @@ -171,6 +176,8 @@ def _get_stats_title(self, player_stats): # Create stats strings. available_stats = { + 'login': login, + 'username': username, 'uptime': 'Uptime : {}'.format(runtime), 'km_walked': '{:,.2f}km walked'.format(distance_travelled), 'level': 'Level {}'.format(current_level), diff --git a/pokemongo_bot/health_record/bot_event.py b/pokemongo_bot/health_record/bot_event.py index 986b5f3c70..f357a4e8e7 100644 --- a/pokemongo_bot/health_record/bot_event.py +++ b/pokemongo_bot/health_record/bot_event.py @@ -7,6 +7,7 @@ import os import uuid import requests +import time class BotEvent(object): def __init__(self, config): @@ -31,38 +32,51 @@ def __init__(self, config): context = {} ) + self.client_id = uuid.uuid4() + self.heartbeat_wait = 30 # seconds + self.last_heartbeat = time.time() + def capture_error(self): if self.config.health_record: self.client.captureException() def login_success(self): if self.config.health_record: - track_url('/loggedin') + self.last_heartbeat = time.time() + self.track_url('/loggedin') def login_failed(self): if self.config.health_record: - track_url('/login') + self.track_url('/login') def login_retry(self): if self.config.health_record: - track_url('/relogin') + self.track_url('/relogin') def logout(self): if self.config.health_record: - track_url('/logout') - + self.track_url('/logout') -def track_url(path): - data = { - 'v': '1', - 'tid': 'UA-81469507-1', - 'aip': '1', # Anonymize IPs - 'cid': uuid.uuid4(), - 't': 'pageview', - 'dp': path - } + def heartbeat(self): + if self.config.health_record: + current_time = time.time() + if current_time - self.heartbeat_wait > self.last_heartbeat: + self.last_heartbeat = current_time + self.track_url('/heartbeat') - response = requests.post( - 'http://www.google-analytics.com/collect', data=data) + def track_url(self, path): + data = { + 'v': '1', + 'tid': 'UA-81469507-1', + 'aip': '1', # Anonymize IPs + 'cid': self.client_id, + 't': 'pageview', + 'dp': path + } + try: + response = requests.post( + 'http://www.google-analytics.com/collect', data=data) - response.raise_for_status() + response.raise_for_status() + except requests.exceptions.HTTPError: + pass diff --git a/pokemongo_bot/plugin_loader.py b/pokemongo_bot/plugin_loader.py new file mode 100644 index 0000000000..3bded030b3 --- /dev/null +++ b/pokemongo_bot/plugin_loader.py @@ -0,0 +1,33 @@ +import os +import sys +import importlib + +class PluginLoader(object): + folder_cache = [] + + def _get_correct_path(self, path): + extension = os.path.splitext(path)[1] + + if extension == '.zip': + correct_path = path + else: + correct_path = os.path.dirname(path) + + return correct_path + + def load_path(self, path): + correct_path = self._get_correct_path(path) + + if correct_path not in self.folder_cache: + self.folder_cache.append(correct_path) + sys.path.append(correct_path) + + def remove_path(self, path): + correct_path = self._get_correct_path(path) + sys.path.remove(correct_path) + + def get_class(self, namespace_class): + [namespace, class_name] = namespace_class.split('.') + my_module = importlib.import_module(namespace) + return getattr(my_module, class_name) + diff --git a/pokemongo_bot/test/plugin_loader_test.py b/pokemongo_bot/test/plugin_loader_test.py new file mode 100644 index 0000000000..4d4d5ca952 --- /dev/null +++ b/pokemongo_bot/test/plugin_loader_test.py @@ -0,0 +1,28 @@ +import imp +import sys +import pkgutil +import importlib +import unittest +import os +from datetime import timedelta, datetime +from mock import patch, MagicMock +from pokemongo_bot.plugin_loader import PluginLoader +from pokemongo_bot.test.resources.plugin_fixture import FakeTask + +class PluginLoaderTest(unittest.TestCase): + def setUp(self): + self.plugin_loader = PluginLoader() + + def test_load_namespace_class(self): + package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture') + self.plugin_loader.load_path(package_path) + loaded_class = self.plugin_loader.get_class('plugin_fixture.FakeTask') + self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') + self.plugin_loader.remove_path(package_path) + + def test_load_zip(self): + package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip') + self.plugin_loader.load_path(package_path) + loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') + self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') + self.plugin_loader.remove_path(package_path) diff --git a/pokemongo_bot/test/resources/__init__.py b/pokemongo_bot/test/resources/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pokemongo_bot/test/resources/plugin_fixture/__init__.py b/pokemongo_bot/test/resources/plugin_fixture/__init__.py new file mode 100644 index 0000000000..57caf83dce --- /dev/null +++ b/pokemongo_bot/test/resources/plugin_fixture/__init__.py @@ -0,0 +1,2 @@ +from fake_task import FakeTask +from unsupported_api_task import UnsupportedApiTask diff --git a/pokemongo_bot/test/resources/plugin_fixture/fake_task.py b/pokemongo_bot/test/resources/plugin_fixture/fake_task.py new file mode 100644 index 0000000000..ff729adee4 --- /dev/null +++ b/pokemongo_bot/test/resources/plugin_fixture/fake_task.py @@ -0,0 +1,7 @@ +from pokemongo_bot.base_task import BaseTask + +class FakeTask(BaseTask): + SUPPORTED_TASK_API_VERSION = 1 + + def work(self): + return 'FakeTask' diff --git a/pokemongo_bot/test/resources/plugin_fixture/unsupported_api_task.py b/pokemongo_bot/test/resources/plugin_fixture/unsupported_api_task.py new file mode 100644 index 0000000000..871e38f82d --- /dev/null +++ b/pokemongo_bot/test/resources/plugin_fixture/unsupported_api_task.py @@ -0,0 +1,7 @@ +from pokemongo_bot.base_task import BaseTask + +class UnsupportedApiTask(BaseTask): + SUPPORTED_TASK_API_VERSION = 2 + + def work(): + return 2 diff --git a/pokemongo_bot/test/resources/plugin_fixture_test.zip b/pokemongo_bot/test/resources/plugin_fixture_test.zip new file mode 100644 index 0000000000000000000000000000000000000000..335d95e5226056902bb02114fa26a2b3dad61ca1 GIT binary patch literal 3412 zcmb7`dpuNmAIFdDjNGOgOqm!(61n7>TxMKy88Xx`+>91!# zS^64&kyMF7CH9A-CuBq$WQv$0!|~I2>_j&EdT`Jc&7K!`;P7HO7Xlsz4+J>05>PZz z6fh^oyqq5~{BlZwVvtDAIBOSouQ?5&#-VFL!{u*-G+ew*&^VT&mLJEq)lk|bfs>(- zoki#|to+?!_A|~-HQQQSNU7tZn3mANA z!?Sw^pf*Rr>#I@rq4>2?7GM+-DIhq2K_Y3>BOt3B2i0o{mN zd(<`VsMFc*N*7tf0%rCHV20Z7{n0L*dNk1Sgdc8eLOp?%Nx^OPlAS9`um8M=30ROj zUr|f_k6FDa%0g8O?*cIu?0w`%M>5QwIaJeKyE$S0P zGoyWhQ+*qg+b)c5E9@4^_EvgRf5lJl$PZ_@j>ZN{lvY(Kxf zYxMD#eN5_0N?F?k5if`&3lH!s@{g0rvJH1*Ag1&1368JUQjn9p_K+INV|i+RB#pX z4WlkIQNyI-HF3Hq)_b!@w646?vX|ta#56j{?S(4kr_Z6H9X3bKia(gV`)ND~lfurN zG)T!6cdW(P<#2zHA>Aj~SG>X=JUD7TggWwDaDS+d%pq}(`hbtl#e8mAMgEVsho4Mc z9aFkN_j~>Um8dM{xkvvwO+@tUQ{9|SOUZXIs5N~{q(daw8h1ros0gqHZ)^YXMWw5& zV+|IO-O9K)QT*fo& zy0aoe29iHTb{_Bia?Fujx&K9a#k58|(o$Rv`v~J#< zXn$Ac5$u4{Wcf!%zW;?XJ0yd3vgKBgLArnQ$ezl&hlj`WtvmuF@|)x9C+^rvs=eVP zb;KLHj9JYk)`M#wr|%L^5S`o9cGNNl!;L)P#YvhPtf6lS zKQV~w%y6jY6VdCO?4J3V!vB0!b$@+I4(8s)B$4mCI|{r0V{u0@`tHq`@XqL#nftrA))*>! z#(3z=|Bj%!Z4vhJiIPbD!q)_U{deA{&b5BJ#jeaaeac_wNjAD!Dzy?pyFPr;V}Gxq zLUPp8te`HKwn_2kt@{1eZ621gHfL`S;y(#PU23MFHbSOMS<#eQ|E|k_`16rl_o6>B z|Aj~45Tl=CI0f=+i=`d%JJZ!@=5Pw4q>V?nQjg8X+{jC#y$W%mkL;Cq zJ-zosbmHZRY%TQ@^-mi*-&zPk(^D&EMt%c&^xd3Cg@olq1Z6ELaK3W~b_@r7FTd5p z7n2tO57EAE^t>Tt6Y!Omd(uh(jc2ciFQ%}0?DA1Wf(JW`a{=303?@Xcc?1+hRzmPr zHj9hrMF?tmA=@l%m^WE$ worker.SUPPORTED_TASK_API_VERSION: + error_string = 'Is there a new version of this task?' + + if error_string != '': + raise MismatchTaskApiVersion( + 'Task {} only works with task api version {}, you are currently running version {}. {}' + .format( + task_type, + worker.SUPPORTED_TASK_API_VERSION, + BaseTask.TASK_API_VERSION, + error_string + ) + ) + instance = worker(self.bot, task_config) workers.append(instance) diff --git a/tests/base_task_test.py b/tests/base_task_test.py index 16684d900c..ee259f80dc 100644 --- a/tests/base_task_test.py +++ b/tests/base_task_test.py @@ -1,6 +1,6 @@ import unittest import json -from pokemongo_bot.cell_workers import BaseTask +from pokemongo_bot.base_task import BaseTask class FakeTask(BaseTask): def initialize(self): diff --git a/tests/tree_config_builder_test.py b/tests/tree_config_builder_test.py index cee1080280..cd8bed22e0 100644 --- a/tests/tree_config_builder_test.py +++ b/tests/tree_config_builder_test.py @@ -1,7 +1,9 @@ import unittest import json -from pokemongo_bot import PokemonGoBot, ConfigException, TreeConfigBuilder +import os +from pokemongo_bot import PokemonGoBot, ConfigException, MismatchTaskApiVersion, TreeConfigBuilder, PluginLoader, BaseTask from pokemongo_bot.cell_workers import HandleSoftBan, CatchLuredPokemon +from pokemongo_bot.test.resources.plugin_fixture import FakeTask, UnsupportedApiTask def convert_from_json(str): return json.loads(str) @@ -83,3 +85,54 @@ def test_task_with_config(self): builder = TreeConfigBuilder(self.bot, obj) tree = builder.build() self.assertTrue(tree[0].config.get('longer_eggs_first', False)) + + def test_load_plugin_task(self): + package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture') + plugin_loader = PluginLoader() + plugin_loader.load_path(package_path) + + obj = convert_from_json("""[{ + "type": "plugin_fixture.FakeTask" + }]""") + + builder = TreeConfigBuilder(self.bot, obj) + tree = builder.build() + result = tree[0].work() + self.assertEqual(result, 'FakeTask') + + def setupUnsupportedBuilder(self): + package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'pokemongo_bot', 'test', 'resources', 'plugin_fixture') + plugin_loader = PluginLoader() + plugin_loader.load_path(package_path) + + obj = convert_from_json("""[{ + "type": "plugin_fixture.UnsupportedApiTask" + }]""") + + return TreeConfigBuilder(self.bot, obj) + + def test_task_version_too_high(self): + builder = self.setupUnsupportedBuilder() + + previous_version = BaseTask.TASK_API_VERSION + BaseTask.TASK_API_VERSION = 1 + + self.assertRaisesRegexp( + MismatchTaskApiVersion, + "Task plugin_fixture.UnsupportedApiTask only works with task api version 2, you are currently running version 1. Do you need to update the bot?", + builder.build) + + BaseTask.TASK_API_VERSION = previous_version + + def test_task_version_too_low(self): + builder = self.setupUnsupportedBuilder() + + previous_version = BaseTask.TASK_API_VERSION + BaseTask.TASK_API_VERSION = 3 + + self.assertRaisesRegexp( + MismatchTaskApiVersion, + "Task plugin_fixture.UnsupportedApiTask only works with task api version 2, you are currently running version 3. Is there a new version of this task?", + builder.build) + + BaseTask.TASK_API_VERSION = previous_version diff --git a/tests/update_title_stats_test.py b/tests/update_title_stats_test.py index 699d736b7a..ba480f0151 100644 --- a/tests/update_title_stats_test.py +++ b/tests/update_title_stats_test.py @@ -1,6 +1,7 @@ import unittest +from sys import platform as _platform from datetime import datetime, timedelta -from mock import patch, MagicMock +from mock import call, patch, MagicMock from pokemongo_bot.cell_workers.update_title_stats import UpdateTitleStats from tests import FakeBot @@ -8,11 +9,11 @@ class UpdateTitleStatsTestCase(unittest.TestCase): config = { 'min_interval': 20, - 'stats': ['pokemon_evolved', 'pokemon_encountered', 'uptime', 'pokemon_caught', - 'stops_visited', 'km_walked', 'level', 'stardust_earned', 'level_completion', - 'xp_per_hour', 'pokeballs_thrown', 'highest_cp_pokemon', 'level_stats', - 'xp_earned', 'pokemon_unseen', 'most_perfect_pokemon', 'pokemon_stats', - 'pokemon_released'] + 'stats': ['login', 'username', 'pokemon_evolved', 'pokemon_encountered', 'uptime', + 'pokemon_caught', 'stops_visited', 'km_walked', 'level', 'stardust_earned', + 'level_completion', 'xp_per_hour', 'pokeballs_thrown', 'highest_cp_pokemon', + 'level_stats', 'xp_earned', 'pokemon_unseen', 'most_perfect_pokemon', + 'pokemon_stats', 'pokemon_released'] } player_stats = { 'level': 25, @@ -23,6 +24,8 @@ class UpdateTitleStatsTestCase(unittest.TestCase): def setUp(self): self.bot = FakeBot() + self.bot._player = {'username': 'Username'} + self.bot.config.username = 'Login' self.worker = UpdateTitleStats(self.bot, self.config) def mock_metrics(self): @@ -87,22 +90,37 @@ def test_next_update_after_update_title(self, mock_datetime): now + timedelta(seconds=self.config['min_interval'])) @patch('pokemongo_bot.cell_workers.update_title_stats.stdout') - def test_update_title_linux_osx(self, mock_stdout): - self.worker._update_title('', 'linux') + def test_update_title_linux_cygwin(self, mock_stdout): + self.worker._update_title('new title linux', 'linux') self.assertEqual(mock_stdout.write.call_count, 1) + self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title linux\x07')) - self.worker._update_title('', 'linux2') + self.worker._update_title('new title linux2', 'linux2') self.assertEqual(mock_stdout.write.call_count, 2) + self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title linux2\x07')) - self.worker._update_title('', 'darwin') + self.worker._update_title('new title cygwin', 'cygwin') self.assertEqual(mock_stdout.write.call_count, 3) + self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title cygwin\x07')) + + @patch('pokemongo_bot.cell_workers.update_title_stats.stdout') + def test_update_title_darwin(self, mock_stdout): + self.worker._update_title('new title darwin', 'darwin') + + self.assertEqual(mock_stdout.write.call_count, 1) + self.assertEqual(mock_stdout.write.call_args, call('\033]0;new title darwin\007')) + + @unittest.skipUnless(_platform.startswith("win"), "requires Windows") + @patch('pokemongo_bot.cell_workers.update_title_stats.ctypes') + def test_update_title_win32(self, mock_ctypes): + self.worker._update_title('new title win32', 'win32') - @unittest.skip("Didn't find a way to mock ctypes.windll.kernel32.SetConsoleTitleA") - def test_update_title_win32(self): - self.worker._update_title('', 'win32') + self.assertEqual(mock_ctypes.windll.kernel32.SetConsoleTitleA.call_count, 1) + self.assertEqual(mock_ctypes.windll.kernel32.SetConsoleTitleA.call_args, + call('new title win32')) def test_get_stats_title_player_stats_none(self): title = self.worker._get_stats_title(None) @@ -119,12 +137,13 @@ def test_get_stats(self): self.mock_metrics() title = self.worker._get_stats_title(self.player_stats) - expected = 'Evolved 12 pokemon | Encountered 130 pokemon | Uptime : 15:42:13 | ' \ - 'Caught 120 pokemon | Visited 220 stops | 42.05km walked | Level 25 | ' \ - 'Earned 24,069 Stardust | 87,500 / 150,000 XP (58%) | 1,337 XP/h | ' \ - 'Threw 145 pokeballs | Highest CP pokemon : highest_cp | ' \ - 'Level 25 (87,500 / 150,000, 58%) | +424,242 XP | ' \ - 'Encountered 3 new pokemon | Most perfect pokemon : most_perfect | ' \ + expected = 'Login | Username | Evolved 12 pokemon | Encountered 130 pokemon | ' \ + 'Uptime : 15:42:13 | Caught 120 pokemon | Visited 220 stops | ' \ + '42.05km walked | Level 25 | Earned 24,069 Stardust | ' \ + '87,500 / 150,000 XP (58%) | 1,337 XP/h | Threw 145 pokeballs | ' \ + 'Highest CP pokemon : highest_cp | Level 25 (87,500 / 150,000, 58%) | ' \ + '+424,242 XP | Encountered 3 new pokemon | ' \ + 'Most perfect pokemon : most_perfect | ' \ 'Encountered 130 pokemon, 120 caught, 30 released, 12 evolved, ' \ '3 never seen before | Released 30 pokemon' From dee28d9fb1a973b95019bc07fb1e206872bd986d Mon Sep 17 00:00:00 2001 From: Eli White Date: Sun, 7 Aug 2016 15:58:16 -0700 Subject: [PATCH 09/32] Rename load_path to load_plugin (#2947) --- pokecli.py | 2 +- pokemongo_bot/plugin_loader.py | 2 +- pokemongo_bot/test/plugin_loader_test.py | 4 ++-- tests/tree_config_builder_test.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pokecli.py b/pokecli.py index 24a0f38ee3..a77cf378c9 100644 --- a/pokecli.py +++ b/pokecli.py @@ -446,7 +446,7 @@ def task_configuration_error(flag_name): plugin_loader = PluginLoader() for plugin in config.plugins: - plugin_loader.load_path(plugin) + plugin_loader.load_plugin(plugin) # create web dir if not exists try: diff --git a/pokemongo_bot/plugin_loader.py b/pokemongo_bot/plugin_loader.py index 3bded030b3..9362197bc8 100644 --- a/pokemongo_bot/plugin_loader.py +++ b/pokemongo_bot/plugin_loader.py @@ -15,7 +15,7 @@ def _get_correct_path(self, path): return correct_path - def load_path(self, path): + def load_plugin(self, path): correct_path = self._get_correct_path(path) if correct_path not in self.folder_cache: diff --git a/pokemongo_bot/test/plugin_loader_test.py b/pokemongo_bot/test/plugin_loader_test.py index 4d4d5ca952..38a566b35c 100644 --- a/pokemongo_bot/test/plugin_loader_test.py +++ b/pokemongo_bot/test/plugin_loader_test.py @@ -15,14 +15,14 @@ def setUp(self): def test_load_namespace_class(self): package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture') - self.plugin_loader.load_path(package_path) + self.plugin_loader.load_plugin(package_path) loaded_class = self.plugin_loader.get_class('plugin_fixture.FakeTask') self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') self.plugin_loader.remove_path(package_path) def test_load_zip(self): package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip') - self.plugin_loader.load_path(package_path) + self.plugin_loader.load_plugin(package_path) loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') self.plugin_loader.remove_path(package_path) diff --git a/tests/tree_config_builder_test.py b/tests/tree_config_builder_test.py index cd8bed22e0..1992c8187c 100644 --- a/tests/tree_config_builder_test.py +++ b/tests/tree_config_builder_test.py @@ -89,7 +89,7 @@ def test_task_with_config(self): def test_load_plugin_task(self): package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture') plugin_loader = PluginLoader() - plugin_loader.load_path(package_path) + plugin_loader.load_plugin(package_path) obj = convert_from_json("""[{ "type": "plugin_fixture.FakeTask" @@ -103,7 +103,7 @@ def test_load_plugin_task(self): def setupUnsupportedBuilder(self): package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'pokemongo_bot', 'test', 'resources', 'plugin_fixture') plugin_loader = PluginLoader() - plugin_loader.load_path(package_path) + plugin_loader.load_plugin(package_path) obj = convert_from_json("""[{ "type": "plugin_fixture.UnsupportedApiTask" From 0855dac7c34cc46b69af9bd4e708e6d172498b31 Mon Sep 17 00:00:00 2001 From: Eli White Date: Sun, 7 Aug 2016 18:08:21 -0700 Subject: [PATCH 10/32] Adding some logic for pulling plugins from github (#2967) --- pokemongo_bot/plugin_loader.py | 57 +++++++++++++++++++++++- pokemongo_bot/plugins/.keep | 1 + pokemongo_bot/test/plugin_loader_test.py | 26 ++++++++++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 pokemongo_bot/plugins/.keep diff --git a/pokemongo_bot/plugin_loader.py b/pokemongo_bot/plugin_loader.py index 9362197bc8..8666b53254 100644 --- a/pokemongo_bot/plugin_loader.py +++ b/pokemongo_bot/plugin_loader.py @@ -1,6 +1,8 @@ import os import sys import importlib +import re +import requests class PluginLoader(object): folder_cache = [] @@ -15,8 +17,8 @@ def _get_correct_path(self, path): return correct_path - def load_plugin(self, path): - correct_path = self._get_correct_path(path) + def load_plugin(self, plugin): + correct_path = self._get_correct_path(plugin) if correct_path not in self.folder_cache: self.folder_cache.append(correct_path) @@ -31,3 +33,54 @@ def get_class(self, namespace_class): my_module = importlib.import_module(namespace) return getattr(my_module, class_name) +class GithubPlugin(object): + def __init__(self, plugin_name): + self.plugin_name = plugin_name + self.plugin_parts = self.get_github_parts() + + def is_valid_plugin(self): + return self.plugin_parts is not None + + def get_github_parts(self): + groups = re.match('(.*)\/(.*)#(.*)', self.plugin_name) + + if groups is None: + return None + + parts = {} + parts['user'] = groups.group(1) + parts['repo'] = groups.group(2) + parts['sha'] = groups.group(3) + + return parts + + def get_local_destination(self): + parts = self.plugin_parts + if parts is None: + raise Exception('Not a valid github plugin') + + file_name = '{}_{}_{}.zip'.format(parts['user'], parts['repo'], parts['sha']) + full_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins', file_name) + return full_path + + def get_github_download_url(self): + parts = self.plugin_parts + if parts is None: + raise Exception('Not a valid github plugin') + + github_url = 'https://github.com/{}/{}/archive/{}.zip'.format(parts['user'], parts['repo'], parts['sha']) + return github_url + + def download(self): + url = self.get_github_download_url() + dest = self.get_local_destination() + + r = requests.get(url, stream=True) + r.raise_for_status() + + with open(dest, 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + if chunk: + f.write(chunk) + r.close() + return dest diff --git a/pokemongo_bot/plugins/.keep b/pokemongo_bot/plugins/.keep new file mode 100644 index 0000000000..5d848b8301 --- /dev/null +++ b/pokemongo_bot/plugins/.keep @@ -0,0 +1 @@ +keep this so we can install plugins into this folder diff --git a/pokemongo_bot/test/plugin_loader_test.py b/pokemongo_bot/test/plugin_loader_test.py index 38a566b35c..ecf50888e6 100644 --- a/pokemongo_bot/test/plugin_loader_test.py +++ b/pokemongo_bot/test/plugin_loader_test.py @@ -6,7 +6,7 @@ import os from datetime import timedelta, datetime from mock import patch, MagicMock -from pokemongo_bot.plugin_loader import PluginLoader +from pokemongo_bot.plugin_loader import PluginLoader, GithubPlugin from pokemongo_bot.test.resources.plugin_fixture import FakeTask class PluginLoaderTest(unittest.TestCase): @@ -26,3 +26,27 @@ def test_load_zip(self): loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') self.plugin_loader.remove_path(package_path) + + def test_get_github_parts_for_valid_github(self): + github_plugin = GithubPlugin('org/repo#sha') + self.assertTrue(github_plugin.is_valid_plugin()) + self.assertEqual(github_plugin.plugin_parts['user'], 'org') + self.assertEqual(github_plugin.plugin_parts['repo'], 'repo') + self.assertEqual(github_plugin.plugin_parts['sha'], 'sha') + + def test_get_github_parts_for_invalid_github(self): + self.assertFalse(GithubPlugin('org/repo').is_valid_plugin()) + self.assertFalse(GithubPlugin('foo').is_valid_plugin()) + self.assertFalse(GithubPlugin('/Users/foo/bar.zip').is_valid_plugin()) + + def test_get_local_destination(self): + github_plugin = GithubPlugin('org/repo#sha') + path = github_plugin.get_local_destination() + expected = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip')) + self.assertEqual(path, expected) + + def test_get_github_download_url(self): + github_plugin = GithubPlugin('org/repo#sha') + url = github_plugin.get_github_download_url() + expected = 'https://github.com/org/repo/archive/sha.zip' + self.assertEqual(url, expected) From a1733b92b9cb4bd5a864ec506e7709d14d56674b Mon Sep 17 00:00:00 2001 From: mhdasding Date: Mon, 8 Aug 2016 05:16:25 +0200 Subject: [PATCH 11/32] flush after title update (#2977) --- pokemongo_bot/cell_workers/update_title_stats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pokemongo_bot/cell_workers/update_title_stats.py b/pokemongo_bot/cell_workers/update_title_stats.py index 43e55c260f..acbfaa7fe4 100644 --- a/pokemongo_bot/cell_workers/update_title_stats.py +++ b/pokemongo_bot/cell_workers/update_title_stats.py @@ -111,8 +111,10 @@ def _update_title(self, title, platform): """ if platform == "linux" or platform == "linux2" or platform == "cygwin": stdout.write("\x1b]2;{}\x07".format(title)) + stdout.flush() elif platform == "darwin": stdout.write("\033]0;{}\007".format(title)) + stdout.flush() elif platform == "win32": ctypes.windll.kernel32.SetConsoleTitleA(title) else: From e66c50951c1c0e44a21253d124c82a1bc78ae449 Mon Sep 17 00:00:00 2001 From: rbignon Date: Mon, 8 Aug 2016 05:17:05 +0200 Subject: [PATCH 12/32] correctly re-raise exception to keep backtrace (#2944) --- pokecli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pokecli.py b/pokecli.py index a77cf378c9..e00389e06e 100644 --- a/pokecli.py +++ b/pokecli.py @@ -53,7 +53,7 @@ def main(): bot = False - + try: logger.info('PokemonGO Bot v1.0') sys.stdout = codecs.getwriter('utf8')(sys.stdout) @@ -130,7 +130,7 @@ def main(): if bot: report_summary(bot) - raise e + raise def report_summary(bot): if bot.metrics.start_time is None: From bdcf2519d63555ac32246439fd17672896ee64e4 Mon Sep 17 00:00:00 2001 From: Chris Le Date: Sun, 7 Aug 2016 20:17:50 -0700 Subject: [PATCH 13/32] Update MoveToMapPokemon to use events instead of logger. (#2913) --- .gitignore | 5 + pokemongo_bot/__init__.py | 29 +++ .../cell_workers/move_to_map_pokemon.py | 231 +++++++++++++++--- pokemongo_bot/health_record/bot_event.py | 2 +- 4 files changed, 233 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index a12509c322..70ab34b250 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,9 @@ var/ .pydevproject .settings/ +# Cloud9 Users +.c9/ + # Installer logs pip-log.txt pip-delete-this-directory.txt @@ -86,6 +89,8 @@ celerybeat-schedule # virtualenv venv/ ENV/ +local/ +share/ # Spyder project settings .spyderproject diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index d0f034f110..1378e5bdb9 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -393,6 +393,35 @@ def _register_events(self): ) self.event_manager.register_event('unset_pokemon_nickname') + # Move To map pokemon + self.event_manager.register_event( + 'move_to_map_pokemon_fail', + parameters=('message',) + ) + self.event_manager.register_event( + 'move_to_map_pokemon_updated_map', + parameters=('lat', 'lon') + ) + self.event_manager.register_event( + 'move_to_map_pokemon_teleport_to', + parameters=('poke_name', 'poke_dist', 'poke_lat', 'poke_lon', + 'disappears_in') + ) + self.event_manager.register_event( + 'move_to_map_pokemon_encounter', + parameters=('poke_name', 'poke_dist', 'poke_lat', 'poke_lon', + 'disappears_in') + ) + self.event_manager.register_event( + 'move_to_map_pokemon_move_towards', + parameters=('poke_name', 'poke_dist', 'poke_lat', 'poke_lon', + 'disappears_in') + ) + self.event_manager.register_event( + 'move_to_map_pokemon_teleport_back', + parameters=('last_lat', 'last_lon') + ) + def tick(self): self.health_record.heartbeat() self.cell = self.get_meta_cell() diff --git a/pokemongo_bot/cell_workers/move_to_map_pokemon.py b/pokemongo_bot/cell_workers/move_to_map_pokemon.py index 08ff35f281..975a9b5a36 100644 --- a/pokemongo_bot/cell_workers/move_to_map_pokemon.py +++ b/pokemongo_bot/cell_workers/move_to_map_pokemon.py @@ -1,11 +1,59 @@ # -*- coding: utf-8 -*- +""" +Moves a trainer to a Pokemon. + +Events: + move_to_map_pokemon_fail + When the worker fails. + Returns: + message: Failure message. + + move_to_map_pokemon_updated_map + When worker updates the PokemonGo-Map. + Returns: + lat: Latitude + lon: Longitude + + move_to_map_pokemon_teleport_to + When trainer is teleported to a Pokemon. + Returns: + poke_name: Pokemon's name + poke_dist: Distance from the trainer + poke_lat: Latitude of the Pokemon + poke_lon: Longitude of the Pokemon + disappears_in: Number of seconds before the Pokemon disappears + + move_to_map_pokemon_encounter + When a trainer encounters a Pokemon by teleporting or walking. + Returns: + poke_name: Pokemon's name + poke_dist: Distance from the trainer + poke_lat: Latitude of the Pokemon + poke_lon: Longitude of the Pokemon + disappears_in: Number of seconds before the Pokemon disappears + + move_to_map_pokemon_move_towards + When a trainer moves toward a Pokemon. + Returns: + poke_name: Pokemon's name + poke_dist: Distance from the trainer + poke_lat: Latitude of the Pokemon + poke_lon: Longitude of the Pokemon + disappears_in: Number of seconds before the Pokemon disappears + + move_to_map_pokemon_teleport_back + When a trainer teleports back to thier previous location. + Returns: + last_lat: Trainer's last known latitude + last_lon: Trainer's last known longitude + +""" import os import time import json import base64 import requests -from pokemongo_bot import logger from pokemongo_bot.cell_workers.utils import distance, format_dist, format_time from pokemongo_bot.step_walker import StepWalker from pokemongo_bot.worker_result import WorkerResult @@ -13,7 +61,20 @@ from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker +# Update the map if more than N meters away from the center. (AND'd with +# UPDATE_MAP_MIN_TIME_MINUTES) +UPDATE_MAP_MIN_DISTANCE_METERS = 500 + +# Update the map if it hasn't been updated in n seconds. (AND'd with +# UPDATE_MAP_MIN_DISTANCE_METERS) +UPDATE_MAP_MIN_TIME_SEC = 120 + +# Number of seconds to sleep between teleporting to a snipped Pokemon. +SNIPE_SLEEP_SEC = 2 + + class MoveToMapPokemon(BaseTask): + """Task for moving a trainer to a Pokemon.""" SUPPORTED_TASK_API_VERSION = 1 def initialize(self): @@ -32,13 +93,15 @@ def get_pokemon_from_map(self): try: req = requests.get('{}/raw_data?gyms=false&scanned=false'.format(self.config['address'])) except requests.exceptions.ConnectionError: - logger.log('Could not reach PokemonGo-Map Server', 'red') + self._emit_failure('Could not get Pokemon data from PokemonGo-Map: ' + '{}. Is it running?'.format( + self.config['address'])) return [] try: raw_data = req.json() except ValueError: - logger.log('Map data was not valid', 'red') + self._emit_failure('Map data was not valid') return [] pokemon_list = [] @@ -48,7 +111,7 @@ def get_pokemon_from_map(self): try: pokemon['encounter_id'] = long(base64.b64decode(pokemon['encounter_id'])) except TypeError: - log.logger('base64 error: {}'.format(pokemon['encounter_id']), 'red') + self._emit_failure('base64 error: {}'.format(pokemon['encounter_id'])) continue pokemon['spawn_point_id'] = pokemon['spawnpoint_id'] pokemon['disappear_time'] = int(pokemon['disappear_time'] / 1000) @@ -100,14 +163,17 @@ def update_map_location(self): try: req = requests.get('{}/loc'.format(self.config['address'])) except requests.exceptions.ConnectionError: - logger.log('Could not reach PokemonGo-Map Server', 'red') + self._emit_failure('Could not update trainer location ' + 'PokemonGo-Map: {}. Is it running?'.format( + self.config['address'])) return try: loc_json = req.json() except ValueError: - return log.logger('Map location data was not valid', 'red') - + err = 'Map location data was not valid' + self._emit_failure(err) + return log.logger(err, 'red') dist = distance( self.bot.position[0], @@ -118,32 +184,38 @@ def update_map_location(self): # update map when 500m away from center and last update longer than 2 minutes away now = int(time.time()) - if dist > 500 and now - self.last_map_update > 2 * 60: - requests.post('{}/next_loc?lat={}&lon={}'.format(self.config['address'], self.bot.position[0], self.bot.position[1])) - logger.log('Updated PokemonGo-Map position') + if (dist > UPDATE_MAP_MIN_DISTANCE_METERS and + now - self.last_map_update > UPDATE_MAP_MIN_TIME_SEC): + requests.post( + '{}/next_loc?lat={}&lon={}'.format(self.config['address'], + self.bot.position[0], + self.bot.position[1])) + self.emit_event( + 'move_to_map_pokemon_updated_map', + formatted='Updated PokemonGo-Map to {lat}, {lon}', + data={ + 'lat': self.bot.position[0], + 'lon': self.bot.position[1] + } + ) self.last_map_update = now def snipe(self, pokemon): - last_position = self.bot.position[0:2] - + """Snipe a Pokemon by teleporting. + + Args: + pokemon: Pokemon to snipe. + """ self.bot.heartbeat() - - logger.log('Teleporting to {} ({})'.format(pokemon['name'], format_dist(pokemon['dist'], self.unit)), 'green') - self.bot.api.set_position(pokemon['latitude'], pokemon['longitude'], 0) - - logger.log('Encounter pokemon', 'green') + self._teleport_to(pokemon) catch_worker = PokemonCatchWorker(pokemon, self.bot) api_encounter_response = catch_worker.create_encounter_api_call() - - time.sleep(2) - logger.log('Teleporting back to previous location..', 'green') - self.bot.api.set_position(last_position[0], last_position[1], 0) - time.sleep(2) + time.sleep(SNIPE_SLEEP_SEC) + self._teleport_back() + time.sleep(SNIPE_SLEEP_SEC) self.bot.heartbeat() - catch_worker.work(api_encounter_response) self.add_caught(pokemon) - return WorkerResult.SUCCESS def dump_caught_pokemon(self): @@ -182,18 +254,111 @@ def work(self): if self.config['snipe']: return self.snipe(pokemon) + step_walker = self._move_to(pokemon) + if not step_walker.step(): + return WorkerResult.RUNNING + self._encountered(pokemon) + self.add_caught(pokemon) + return WorkerResult.SUCCESS + + def _emit_failure(self, msg): + """Emits failure to event log. + + Args: + msg: Message to emit + """ + self.emit_event( + 'move_to_map_pokemon_fail', + formatted='Failure! {message}', + data={'message': msg} + ) + + def _emit_log(self, msg): + """Emits log to event log. + + Args: + msg: Message to emit + """ + self.emit_event( + 'move_to_map_pokemon', + formatted='{message}', + data={'message': msg} + ) + + def _pokemon_event_data(self, pokemon): + """Generates parameters used for the Bot's event manager. + + Args: + pokemon: Pokemon object + + Returns: + Dictionary with Pokemon's info. + """ + now = int(time.time()) + return { + 'poke_name': pokemon['name'], + 'poke_dist': (format_dist(pokemon['dist'], self.unit)), + 'poke_lat': pokemon['latitude'], + 'poke_lon': pokemon['longitude'], + 'disappears_in': (format_time(pokemon['disappear_time'] - now)) + } + + def _teleport_to(self, pokemon): + """Teleports trainer to a Pokemon. + + Args: + pokemon: Pokemon to teleport to. + """ + self.emit_event( + 'move_to_map_pokemon_teleport_to', + formatted='Teleporting to {poke_name}. ({poke_dist})', + data=self._pokemon_event_data(pokemon) + ) + self.bot.api.set_position(pokemon['latitude'], pokemon['longitude'], 0) + self._encountered(pokemon) + + def _encountered(self, pokemon): + """Emit event when trainer encounters a Pokemon. + + Args: + pokemon: Pokemon encountered. + """ + self.emit_event( + 'move_to_map_pokemon_encounter', + formatted='Encountered Pokemon: {poke_name}', + data=self._pokemon_event_data(pokemon) + ) + + def _teleport_back(self): + """Teleports trainer back to their last position.""" + last_position = self.bot.position[0:2] + self.emit_event( + 'move_to_map_pokemon_teleport_back', + formatted=('Teleporting back to previous location ({last_lat}, ' + '{last_long})'), + data={'last_lat': last_position[0], 'last_lon': last_position[1]} + ) + self.bot.api.set_position(last_position[0], last_position[1], 0) + + def _move_to(self, pokemon): + """Moves trainer towards a Pokemon. + + Args: + pokemon: Pokemon to move to. + + Returns: + StepWalker + """ now = int(time.time()) - logger.log('Moving towards {}, {} left ({})'.format(pokemon['name'], format_dist(pokemon['dist'], self.unit), format_time(pokemon['disappear_time'] - now))) - step_walker = StepWalker( + self.emit_event( + 'move_to_map_pokemon_move_towards', + formatted=('Moving towards {poke_name}, {poke_dist}, left (' + '{disappears_in})'), + data=self._pokemon_event_data(pokemon) + ) + return StepWalker( self.bot, self.bot.config.walk, pokemon['latitude'], pokemon['longitude'] ) - - if not step_walker.step(): - return WorkerResult.RUNNING - - logger.log('Arrived at {}'.format(pokemon['name'])) - self.add_caught(pokemon) - return WorkerResult.SUCCESS diff --git a/pokemongo_bot/health_record/bot_event.py b/pokemongo_bot/health_record/bot_event.py index f357a4e8e7..55a726049d 100644 --- a/pokemongo_bot/health_record/bot_event.py +++ b/pokemongo_bot/health_record/bot_event.py @@ -16,7 +16,7 @@ def __init__(self, config): # UniversalAnalytics can be reviewed here: # https://github.com/analytics-pros/universal-analytics-python if self.config.health_record: - self.logger.info('Health check is enabled. For more logrmation:') + self.logger.info('Health check is enabled. For more information:') self.logger.info('https://github.com/PokemonGoF/PokemonGo-Bot/tree/dev#analytics') self.client = Client( dsn='https://8abac56480f34b998813d831de262514:196ae1d8dced41099f8253ea2c8fe8e6@app.getsentry.com/90254', From 95902d6ceac7b26da9bade1ce193d066f67c6c04 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 7 Aug 2016 23:59:50 -0400 Subject: [PATCH 14/32] Config/encrypt.so (#2964) * Add config option for libencrypt.so * Correctly set the config value and check for the file in said dir --- configs/config.json.example | 1 + pokecli.py | 1 + pokemongo_bot/__init__.py | 14 ++++++++------ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/configs/config.json.example b/configs/config.json.example index 20ef72e34e..ec46c15eb9 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -4,6 +4,7 @@ "password": "YOUR_PASSWORD", "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", + "libencrypt_location": "", "tasks": [ { "type": "HandleSoftBan" diff --git a/pokecli.py b/pokecli.py index e00389e06e..7cc5b5ebf1 100644 --- a/pokecli.py +++ b/pokecli.py @@ -384,6 +384,7 @@ def init_config(): if not config.password and 'password' not in load: config.password = getpass("Password: ") + config.encrypt_location = load.get('encrypt_location','') config.catch = load.get('catch', {}) config.release = load.get('release', {}) config.action_wait_max = load.get('action_wait_max', 4) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 1378e5bdb9..78498e37b0 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -636,7 +636,6 @@ def login(self): ) def get_encryption_lib(self): - file_name = '' if _platform == "linux" or _platform == "linux2" or _platform == "darwin": file_name = 'encrypt.so' elif _platform == "Windows" or _platform == "win32": @@ -646,15 +645,18 @@ def get_encryption_lib(self): else: file_name = 'encrypt.dll' - path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) - full_path = path + '/'+ file_name + if self.config.encrypt_location == '': + path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + else: + path = self.config.encrypt_location + full_path = path + '/'+ file_name if not os.path.isfile(full_path): - self.logger.error(file_name + ' is not found! Please place it in the bots root directory.') - self.logger.info('Platform: '+ _platform + ' Bot root directory: '+ path) + self.logger.error(file_name + ' is not found! Please place it in the bots root directory or set libencrypt_location in config.') + self.logger.info('Platform: '+ _platform + ' Encrypt.so directory: '+ path) sys.exit(1) else: - self.logger.info('Found '+ file_name +'! Platform: ' + _platform + ' Bot root directory: ' + path) + self.logger.info('Found '+ file_name +'! Platform: ' + _platform + ' Encrypt.so directory: ' + path) return full_path From 41ed10cc9fd0e9a96d76a160095087617f32e849 Mon Sep 17 00:00:00 2001 From: middleagedman Date: Mon, 8 Aug 2016 00:27:13 -0400 Subject: [PATCH 15/32] Fixed mispelling for "formatted" variable (#2984) --- pokecli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pokecli.py b/pokecli.py index 7cc5b5ebf1..d428026276 100644 --- a/pokecli.py +++ b/pokecli.py @@ -104,7 +104,7 @@ def main(): 'api_error', sender=bot, level='info', - formmated='Log logged in, reconnecting in {:s}'.format(wait_time) + formatted='Log logged in, reconnecting in {:s}'.format(wait_time) ) time.sleep(wait_time) except ServerBusyOrOfflineException: From 563f898f61233ff86d38d83b68d8399c1f63051b Mon Sep 17 00:00:00 2001 From: Eli White Date: Sun, 7 Aug 2016 21:32:53 -0700 Subject: [PATCH 16/32] Loading plugins from Github (#2992) * Checking github plugin file existence * Loading plugins from github --- pokemongo_bot/plugin_loader.py | 14 ++++++++- pokemongo_bot/test/plugin_loader_test.py | 37 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pokemongo_bot/plugin_loader.py b/pokemongo_bot/plugin_loader.py index 8666b53254..7a838bb209 100644 --- a/pokemongo_bot/plugin_loader.py +++ b/pokemongo_bot/plugin_loader.py @@ -18,7 +18,14 @@ def _get_correct_path(self, path): return correct_path def load_plugin(self, plugin): - correct_path = self._get_correct_path(plugin) + github_plugin = GithubPlugin(plugin) + if github_plugin.is_valid_plugin(): + if not github_plugin.is_already_downloaded(): + github_plugin.download() + + correct_path = github_plugin.get_local_destination() + else: + correct_path = self._get_correct_path(plugin) if correct_path not in self.folder_cache: self.folder_cache.append(correct_path) @@ -27,6 +34,7 @@ def load_plugin(self, plugin): def remove_path(self, path): correct_path = self._get_correct_path(path) sys.path.remove(correct_path) + self.folder_cache.remove(correct_path) def get_class(self, namespace_class): [namespace, class_name] = namespace_class.split('.') @@ -63,6 +71,10 @@ def get_local_destination(self): full_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins', file_name) return full_path + def is_already_downloaded(self): + file_path = self.get_local_destination() + return os.path.isfile(file_path) + def get_github_download_url(self): parts = self.plugin_parts if parts is None: diff --git a/pokemongo_bot/test/plugin_loader_test.py b/pokemongo_bot/test/plugin_loader_test.py index ecf50888e6..0b0f7da9d1 100644 --- a/pokemongo_bot/test/plugin_loader_test.py +++ b/pokemongo_bot/test/plugin_loader_test.py @@ -4,6 +4,8 @@ import importlib import unittest import os +import shutil +import mock from datetime import timedelta, datetime from mock import patch, MagicMock from pokemongo_bot.plugin_loader import PluginLoader, GithubPlugin @@ -27,6 +29,30 @@ def test_load_zip(self): self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') self.plugin_loader.remove_path(package_path) + def copy_zip(self): + zip_fixture = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip') + dest_path = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip')) + shutil.copyfile(zip_fixture, dest_path) + return dest_path + + def test_load_github_already_downloaded(self): + dest_path = self.copy_zip() + self.plugin_loader.load_plugin('org/repo#sha') + loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') + self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') + self.plugin_loader.remove_path(dest_path) + os.remove(dest_path) + + @mock.patch.object(GithubPlugin, 'download', copy_zip) + def test_load_github_not_downloaded(self): + self.plugin_loader.load_plugin('org/repo#sha') + loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') + self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') + dest_path = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip')) + self.plugin_loader.remove_path(dest_path) + os.remove(dest_path) + +class GithubPluginTest(unittest.TestCase): def test_get_github_parts_for_valid_github(self): github_plugin = GithubPlugin('org/repo#sha') self.assertTrue(github_plugin.is_valid_plugin()) @@ -50,3 +76,14 @@ def test_get_github_download_url(self): url = github_plugin.get_github_download_url() expected = 'https://github.com/org/repo/archive/sha.zip' self.assertEqual(url, expected) + + def test_is_already_downloaded_not_downloaded(self): + github_plugin = GithubPlugin('org/repo#sha') + self.assertFalse(github_plugin.is_already_downloaded()) + + def test_is_already_downloaded_downloaded(self): + github_plugin = GithubPlugin('org/repo#sha') + dest = github_plugin.get_local_destination() + open(dest, 'a').close() + self.assertTrue(github_plugin.is_already_downloaded()) + os.remove(dest) From 229381c318c2f230257dc56f51cd0fd5213ee2d2 Mon Sep 17 00:00:00 2001 From: raulgbcr Date: Mon, 8 Aug 2016 14:12:25 +0200 Subject: [PATCH 17/32] Fixed #3000 (#3003) Fixed syntax error on "move_to_map_pokemon.py" that makes the client crash when using this feature. --- pokemongo_bot/cell_workers/move_to_map_pokemon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pokemongo_bot/cell_workers/move_to_map_pokemon.py b/pokemongo_bot/cell_workers/move_to_map_pokemon.py index 975a9b5a36..ec3c8bf89c 100644 --- a/pokemongo_bot/cell_workers/move_to_map_pokemon.py +++ b/pokemongo_bot/cell_workers/move_to_map_pokemon.py @@ -335,7 +335,7 @@ def _teleport_back(self): self.emit_event( 'move_to_map_pokemon_teleport_back', formatted=('Teleporting back to previous location ({last_lat}, ' - '{last_long})'), + '{last_lon})'), data={'last_lat': last_position[0], 'last_lon': last_position[1]} ) self.bot.api.set_position(last_position[0], last_position[1], 0) From 1a18b9f3b34f1f75bfae9c29ea2985aa3d59653b Mon Sep 17 00:00:00 2001 From: Jaap Moolenaar Date: Mon, 8 Aug 2016 14:13:07 +0200 Subject: [PATCH 18/32] Added MaxPotion inventory count to summary. (#3015) Short Description: The Max Potion count was missing from the inventory summary. Was #2456 --- pokemongo_bot/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 78498e37b0..788e20e721 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -747,7 +747,8 @@ def _print_character_info(self): self.logger.info( 'Potion: ' + str(items_stock[101]) + ' | SuperPotion: ' + str(items_stock[102]) + - ' | HyperPotion: ' + str(items_stock[103])) + ' | HyperPotion: ' + str(items_stock[103]) + + ' | MaxPotion: ' + str(items_stock[104])) self.logger.info( 'Incense: ' + str(items_stock[401]) + From 4faf9624519e1ea9513e3f3c887435fb81c56ab0 Mon Sep 17 00:00:00 2001 From: Arthur Caranta Date: Mon, 8 Aug 2016 14:13:32 +0200 Subject: [PATCH 19/32] Added cleanup of download and files for encrypt.so after they are no longer needed (#3011) --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index dce398c63e..456ae4fb5a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,8 @@ RUN cd /tmp && wget "http://pgoapi.com/pgoencrypt.tar.gz" \ && cd pgoencrypt/src \ && make \ && cp libencrypt.so /usr/src/app/encrypt.so + && cd /tmp + && rm -rf /tmp/pgoencrypt* VOLUME ["/usr/src/app/web"] From 7cc524ed80a918170efcd782829ed6a94e2c4674 Mon Sep 17 00:00:00 2001 From: Jeremy Bi Date: Mon, 8 Aug 2016 20:16:38 +0800 Subject: [PATCH 20/32] Fix bot not returning back after telepoting (#3014) * Fix typo: last_long -> last_lon * Whitespace cleanup * Fix bug introduced by #3037: bot not returning back --- .../cell_workers/move_to_map_pokemon.py | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/pokemongo_bot/cell_workers/move_to_map_pokemon.py b/pokemongo_bot/cell_workers/move_to_map_pokemon.py index ec3c8bf89c..9ab6bbb7a2 100644 --- a/pokemongo_bot/cell_workers/move_to_map_pokemon.py +++ b/pokemongo_bot/cell_workers/move_to_map_pokemon.py @@ -7,13 +7,13 @@ When the worker fails. Returns: message: Failure message. - + move_to_map_pokemon_updated_map When worker updates the PokemonGo-Map. Returns: lat: Latitude lon: Longitude - + move_to_map_pokemon_teleport_to When trainer is teleported to a Pokemon. Returns: @@ -22,7 +22,7 @@ poke_lat: Latitude of the Pokemon poke_lon: Longitude of the Pokemon disappears_in: Number of seconds before the Pokemon disappears - + move_to_map_pokemon_encounter When a trainer encounters a Pokemon by teleporting or walking. Returns: @@ -31,7 +31,7 @@ poke_lat: Latitude of the Pokemon poke_lon: Longitude of the Pokemon disappears_in: Number of seconds before the Pokemon disappears - + move_to_map_pokemon_move_towards When a trainer moves toward a Pokemon. Returns: @@ -40,7 +40,7 @@ poke_lat: Latitude of the Pokemon poke_lon: Longitude of the Pokemon disappears_in: Number of seconds before the Pokemon disappears - + move_to_map_pokemon_teleport_back When a trainer teleports back to thier previous location. Returns: @@ -184,11 +184,11 @@ def update_map_location(self): # update map when 500m away from center and last update longer than 2 minutes away now = int(time.time()) - if (dist > UPDATE_MAP_MIN_DISTANCE_METERS and + if (dist > UPDATE_MAP_MIN_DISTANCE_METERS and now - self.last_map_update > UPDATE_MAP_MIN_TIME_SEC): requests.post( - '{}/next_loc?lat={}&lon={}'.format(self.config['address'], - self.bot.position[0], + '{}/next_loc?lat={}&lon={}'.format(self.config['address'], + self.bot.position[0], self.bot.position[1])) self.emit_event( 'move_to_map_pokemon_updated_map', @@ -202,16 +202,18 @@ def update_map_location(self): def snipe(self, pokemon): """Snipe a Pokemon by teleporting. - + Args: pokemon: Pokemon to snipe. """ + last_position = self.bot.position[0:2] self.bot.heartbeat() self._teleport_to(pokemon) catch_worker = PokemonCatchWorker(pokemon, self.bot) api_encounter_response = catch_worker.create_encounter_api_call() time.sleep(SNIPE_SLEEP_SEC) - self._teleport_back() + self._teleport_back(last_position) + self.bot.api.set_position(last_position[0], last_position[1], 0) time.sleep(SNIPE_SLEEP_SEC) self.bot.heartbeat() catch_worker.work(api_encounter_response) @@ -263,34 +265,34 @@ def work(self): def _emit_failure(self, msg): """Emits failure to event log. - + Args: msg: Message to emit """ self.emit_event( - 'move_to_map_pokemon_fail', + 'move_to_map_pokemon_fail', formatted='Failure! {message}', data={'message': msg} ) - + def _emit_log(self, msg): """Emits log to event log. - + Args: msg: Message to emit """ self.emit_event( - 'move_to_map_pokemon', + 'move_to_map_pokemon', formatted='{message}', data={'message': msg} ) - + def _pokemon_event_data(self, pokemon): """Generates parameters used for the Bot's event manager. - + Args: pokemon: Pokemon object - + Returns: Dictionary with Pokemon's info. """ @@ -298,14 +300,14 @@ def _pokemon_event_data(self, pokemon): return { 'poke_name': pokemon['name'], 'poke_dist': (format_dist(pokemon['dist'], self.unit)), - 'poke_lat': pokemon['latitude'], + 'poke_lat': pokemon['latitude'], 'poke_lon': pokemon['longitude'], 'disappears_in': (format_time(pokemon['disappear_time'] - now)) } - + def _teleport_to(self, pokemon): """Teleports trainer to a Pokemon. - + Args: pokemon: Pokemon to teleport to. """ @@ -316,7 +318,7 @@ def _teleport_to(self, pokemon): ) self.bot.api.set_position(pokemon['latitude'], pokemon['longitude'], 0) self._encountered(pokemon) - + def _encountered(self, pokemon): """Emit event when trainer encounters a Pokemon. @@ -329,23 +331,21 @@ def _encountered(self, pokemon): data=self._pokemon_event_data(pokemon) ) - def _teleport_back(self): - """Teleports trainer back to their last position.""" - last_position = self.bot.position[0:2] + def _teleport_back(self, last_position): + """Teleports trainer back to their last position.""" self.emit_event( 'move_to_map_pokemon_teleport_back', formatted=('Teleporting back to previous location ({last_lat}, ' '{last_lon})'), data={'last_lat': last_position[0], 'last_lon': last_position[1]} ) - self.bot.api.set_position(last_position[0], last_position[1], 0) - + def _move_to(self, pokemon): """Moves trainer towards a Pokemon. - + Args: pokemon: Pokemon to move to. - + Returns: StepWalker """ From 283c17e5dcb58267d895629cfa968aa65cae975d Mon Sep 17 00:00:00 2001 From: Nikos Filippakis Date: Mon, 8 Aug 2016 17:24:24 +0200 Subject: [PATCH 21/32] Fix Dockerfile installation (#3057) --- CONTRIBUTORS.md | 1 + Dockerfile | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ee5aa7063d..6dceaf0918 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -53,3 +53,4 @@ * lucasfevi * Moonlight-Angel * mjmadsen + * nikofil diff --git a/Dockerfile b/Dockerfile index 456ae4fb5a..f98d5d6942 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,8 +11,8 @@ RUN cd /tmp && wget "http://pgoapi.com/pgoencrypt.tar.gz" \ && tar zxvf pgoencrypt.tar.gz \ && cd pgoencrypt/src \ && make \ - && cp libencrypt.so /usr/src/app/encrypt.so - && cd /tmp + && cp libencrypt.so /usr/src/app/encrypt.so \ + && cd /tmp \ && rm -rf /tmp/pgoencrypt* VOLUME ["/usr/src/app/web"] From 351ea76b62a2708cae72d59977b42c6f3f204250 Mon Sep 17 00:00:00 2001 From: cmezh Date: Mon, 8 Aug 2016 22:42:17 +0700 Subject: [PATCH 22/32] Fix for #3045 (#3055) --- pokecli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pokecli.py b/pokecli.py index d428026276..65d9923798 100644 --- a/pokecli.py +++ b/pokecli.py @@ -104,7 +104,7 @@ def main(): 'api_error', sender=bot, level='info', - formatted='Log logged in, reconnecting in {:s}'.format(wait_time) + formatted='Log logged in, reconnecting in {:d}'.format(wait_time) ) time.sleep(wait_time) except ServerBusyOrOfflineException: From ae0ae815089bf1e3b17497b476d524a0468949e2 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Mon, 8 Aug 2016 09:21:44 -0700 Subject: [PATCH 23/32] Update README.md (#3090) --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index f269d43e6d..171ee4f355 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,8 @@ The project is currently setup in two main branches. `dev` and `master`. ## Please submit PR to [Dev branch](https://github.com/PokemonGoF/PokemonGo-Bot/tree/dev) -## Where to get the DLL/SO? A help channel is coming. -You need to grab them from the Internet. - We use [Slack](https://slack.com) as a web chat. [Click here to join the chat!](https://pokemongo-bot.herokuapp.com) +You can count on the community in #help channel. ## Table of Contents - [Features](#features) From 20aeb90bc0712d4f023c311844f4c1174061d600 Mon Sep 17 00:00:00 2001 From: mjmadsen Date: Mon, 8 Aug 2016 11:22:19 -0500 Subject: [PATCH 24/32] Added request to check configuration (#3089) --- .github/ISSUE_TEMPLATE.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 9976991cf9..a1d7168e75 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,3 +1,5 @@ +Please check configuration at http://jsonlint.com/ before posting an issue. + ### Expected Behavior From ff380cd0e07f225ad1ae2c739bea9f9f80e2f8ce Mon Sep 17 00:00:00 2001 From: middleagedman Date: Mon, 8 Aug 2016 12:49:26 -0400 Subject: [PATCH 25/32] Fixed Dockerfile - missing \ on command lines (#3096) * Fixed mispelling for "formatted" variable * Docker commands missing trailing \ From a5e91315ed82581a901fbf5019a3708e5e7a471b Mon Sep 17 00:00:00 2001 From: Ajurna Date: Mon, 8 Aug 2016 19:34:31 +0100 Subject: [PATCH 26/32] Fix for FileIO slowing bot performance.This puts the map writing into a thread and makes sure it only executes once. (#3100) --- pokemongo_bot/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 788e20e721..67c7f9a6b0 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -9,6 +9,8 @@ import re import sys import time +import Queue +import threading from geopy.geocoders import GoogleV3 from pgoapi import PGoApi @@ -69,6 +71,11 @@ def __init__(self, config): # Make our own copy of the workers for this instance self.workers = [] + # Theading setup for file writing + self.web_update_queue = Queue.Queue(maxsize=1) + self.web_update_thread = threading.Thread(target=self.update_web_location_worker) + self.web_update_thread.start() + def start(self): self._setup_event_system() self._setup_logging() @@ -976,7 +983,15 @@ def heartbeat(self): request.get_player() request.check_awarded_badges() request.call() - self.update_web_location() # updates every tick + try: + self.web_update_queue.put_nowait(True) # do this outside of thread every tick + except Queue.Full: + pass + + def update_web_location_worker(self): + while True: + self.web_update_queue.get() + self.update_web_location() def get_inventory_count(self, what): response_dict = self.get_inventory() From d8546d7bfa660e827c903582da0c7ac714db8406 Mon Sep 17 00:00:00 2001 From: Tushar Saini Date: Mon, 8 Aug 2016 13:39:33 -0500 Subject: [PATCH 27/32] Change word usage: "fled" to "escaped" (#3118) "fled" is confusing to lot of people and is easily confused with pokemon vanishing. "escaped" is a better term. --- pokemongo_bot/cell_workers/pokemon_catch_worker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pokemongo_bot/cell_workers/pokemon_catch_worker.py b/pokemongo_bot/cell_workers/pokemon_catch_worker.py index d676e9f0e2..d5118b391b 100644 --- a/pokemongo_bot/cell_workers/pokemon_catch_worker.py +++ b/pokemongo_bot/cell_workers/pokemon_catch_worker.py @@ -324,8 +324,8 @@ def work(self, response_dict=None): 'CATCH_POKEMON']['status'] if status is 2: self.emit_event( - 'pokemon_fled', - formatted="{pokemon} fled.", + 'pokemon_escaped', + formatted="{pokemon} escaped.", data={'pokemon': pokemon_name} ) sleep(2) From 0b3aa4f5fcb6d353e062019c2b482b6a5f2631fc Mon Sep 17 00:00:00 2001 From: HKLCF Date: Tue, 9 Aug 2016 02:40:41 +0800 Subject: [PATCH 28/32] Update the example config file (#3120) * Add config option for libencrypt.so * Add config option for libencrypt.so * Add config option for libencrypt.so * Add config option for libencrypt.so * Rename path.example.json to path.json.example --- configs/config.json.cluster.example | 1 + configs/config.json.map.example | 1 + configs/config.json.path.example | 1 + configs/config.json.pokemon.example | 1 + configs/{path.example.json => path.json.example} | 0 5 files changed, 4 insertions(+) rename configs/{path.example.json => path.json.example} (100%) diff --git a/configs/config.json.cluster.example b/configs/config.json.cluster.example index 8d0d8f854f..b32eb4f668 100644 --- a/configs/config.json.cluster.example +++ b/configs/config.json.cluster.example @@ -4,6 +4,7 @@ "password": "YOUR_PASSWORD", "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", + "libencrypt_location": "", "tasks": [ { "type": "HandleSoftBan" diff --git a/configs/config.json.map.example b/configs/config.json.map.example index e665d4c6da..1079c999f9 100644 --- a/configs/config.json.map.example +++ b/configs/config.json.map.example @@ -4,6 +4,7 @@ "password": "YOUR_PASSWORD", "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", + "libencrypt_location": "", "tasks": [ { "type": "HandleSoftBan" diff --git a/configs/config.json.path.example b/configs/config.json.path.example index afd1e3afeb..94a9fdba07 100644 --- a/configs/config.json.path.example +++ b/configs/config.json.path.example @@ -4,6 +4,7 @@ "password": "YOUR_PASSWORD", "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", + "libencrypt_location": "", "tasks": [ { "type": "HandleSoftBan" diff --git a/configs/config.json.pokemon.example b/configs/config.json.pokemon.example index 7cad1ac066..1d428a6ae7 100644 --- a/configs/config.json.pokemon.example +++ b/configs/config.json.pokemon.example @@ -4,6 +4,7 @@ "password": "YOUR_PASSWORD", "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", + "libencrypt_location": "", "tasks": [ { "type": "HandleSoftBan" diff --git a/configs/path.example.json b/configs/path.json.example similarity index 100% rename from configs/path.example.json rename to configs/path.json.example From d0f60a221a8af7eaf7b41ffeeb756a636032534e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6kay=20G=C3=BCrcan?= Date: Mon, 8 Aug 2016 21:44:49 +0300 Subject: [PATCH 29/32] typo: logrmation -> information (#2601) Fix a typo. I assume that it was "information" initially, but became "logrmation" when someone used replace all functionality to replace all infos with logs. But I might be totally wrong at this point, idk. Just didn't like the word and wanted to fix that typo. From f648be31c15d0143bccc0531571b2d00fd88ec4d Mon Sep 17 00:00:00 2001 From: pmquan Date: Mon, 8 Aug 2016 12:08:50 -0700 Subject: [PATCH 30/32] Change fled to escaped (#3129) Fix an issue after PR #3118 --- pokemongo_bot/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 67c7f9a6b0..8e9a28ce3a 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -256,7 +256,7 @@ def _register_events(self): ) ) self.event_manager.register_event( - 'pokemon_fled', + 'pokemon_escaped', parameters=('pokemon',) ) self.event_manager.register_event( From 47ab81f5e1e34a36a33cf9c46949b5e51ab6e412 Mon Sep 17 00:00:00 2001 From: Chris Wild Date: Mon, 8 Aug 2016 21:07:09 +0100 Subject: [PATCH 31/32] When JSON parsing fails, give a rough indication of why (#3137) * When JSON parsing fails, give a rough indication of why * Use the official package instead of SHA1 commit --- pokecli.py | 26 ++++++++++++++++++++++---- requirements.txt | 1 + 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pokecli.py b/pokecli.py index 65d9923798..55afa55399 100644 --- a/pokecli.py +++ b/pokecli.py @@ -42,6 +42,12 @@ from pokemongo_bot.health_record import BotEvent from pokemongo_bot.plugin_loader import PluginLoader +try: + from demjson import jsonlint +except ImportError: + # Run `pip install -r requirements.txt` to fix this + jsonlint = None + if sys.version_info >= (2, 7, 9): ssl._create_default_https_context = ssl._create_unverified_context @@ -162,16 +168,28 @@ def init_config(): # If config file exists, load variables from json load = {} + def _json_loader(filename): + try: + with open(filename, 'rb') as data: + load.update(json.load(data)) + except ValueError: + if jsonlint: + with open(filename, 'rb') as data: + lint = jsonlint() + rc = lint.main(['-v', filename]) + + logger.critical('Error with configuration file') + sys.exit(-1) + # Select a config file code parser.add_argument("-cf", "--config", help="Config File to use") config_arg = parser.parse_known_args() and parser.parse_known_args()[0].config or None + if config_arg and os.path.isfile(config_arg): - with open(config_arg) as data: - load.update(json.load(data)) + _json_loader(config_arg) elif os.path.isfile(config_file): logger.info('No config argument specified, checking for /configs/config.json') - with open(config_file) as data: - load.update(json.load(data)) + _json_loader(config_file) else: logger.info('Error: No /configs/config.json or specified config') diff --git a/requirements.txt b/requirements.txt index aabf40937d..f6a22a0233 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,3 +21,4 @@ gpxpy==1.1.1 mock==2.0.0 timeout-decorator==0.3.2 raven==5.23.0 +demjson==2.2.4 From c8a33bca367a7bd77b499e711ccc4f12142e3e5b Mon Sep 17 00:00:00 2001 From: Eli White Date: Mon, 8 Aug 2016 13:39:24 -0700 Subject: [PATCH 32/32] Handle Github Download Zip Format (#3108) * Checking github plugin file existence * Loading plugins from github * Starting install code for github plugins * Updating GithubPlugin to support extracting folders * Handling github zip formats by extracting to the correct location --- pokemongo_bot/plugin_loader.py | 64 +++++++++-- pokemongo_bot/test/plugin_loader_test.py | 105 ++++++++++++++---- .../test/resources/plugin_fixture_test.zip | Bin 3412 -> 1939 bytes pokemongo_bot/test/resources/plugin_sha/.sha | 1 + ...54eddde33061be9b329efae0cfb9bd58842655.zip | Bin 0 -> 1734 bytes 5 files changed, 140 insertions(+), 30 deletions(-) create mode 100644 pokemongo_bot/test/resources/plugin_sha/.sha create mode 100644 pokemongo_bot/test/resources/test-pgo-plugin-2d54eddde33061be9b329efae0cfb9bd58842655.zip diff --git a/pokemongo_bot/plugin_loader.py b/pokemongo_bot/plugin_loader.py index 7a838bb209..f7e12a85a7 100644 --- a/pokemongo_bot/plugin_loader.py +++ b/pokemongo_bot/plugin_loader.py @@ -3,6 +3,8 @@ import importlib import re import requests +import zipfile +import shutil class PluginLoader(object): folder_cache = [] @@ -20,10 +22,11 @@ def _get_correct_path(self, path): def load_plugin(self, plugin): github_plugin = GithubPlugin(plugin) if github_plugin.is_valid_plugin(): - if not github_plugin.is_already_downloaded(): - github_plugin.download() + if not github_plugin.is_already_installed(): + github_plugin.install() + + correct_path = github_plugin.get_plugin_folder() - correct_path = github_plugin.get_local_destination() else: correct_path = self._get_correct_path(plugin) @@ -42,6 +45,8 @@ def get_class(self, namespace_class): return getattr(my_module, class_name) class GithubPlugin(object): + PLUGINS_FOLDER = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins') + def __init__(self, plugin_name): self.plugin_name = plugin_name self.plugin_parts = self.get_github_parts() @@ -62,18 +67,45 @@ def get_github_parts(self): return parts + def get_installed_version(self): + if not self.is_already_installed(): + return None + + filename = os.path.join(self.get_plugin_folder(), '.sha') + print filename + with open(filename) as file: + return file.read().strip() + def get_local_destination(self): parts = self.plugin_parts if parts is None: raise Exception('Not a valid github plugin') file_name = '{}_{}_{}.zip'.format(parts['user'], parts['repo'], parts['sha']) - full_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins', file_name) + full_path = os.path.join(self.PLUGINS_FOLDER, file_name) return full_path - def is_already_downloaded(self): - file_path = self.get_local_destination() - return os.path.isfile(file_path) + def is_already_installed(self): + file_path = self.get_plugin_folder() + if not os.path.isdir(file_path): + return False + + sha_file = os.path.join(file_path, '.sha') + + if not os.path.isfile(sha_file): + return False + + with open(sha_file) as file: + content = file.read().strip() + + if content != self.plugin_parts['sha']: + return False + + return True + + def get_plugin_folder(self): + folder_name = '{}_{}'.format(self.plugin_parts['user'], self.plugin_parts['repo']) + return os.path.join(self.PLUGINS_FOLDER, folder_name) def get_github_download_url(self): parts = self.plugin_parts @@ -83,6 +115,24 @@ def get_github_download_url(self): github_url = 'https://github.com/{}/{}/archive/{}.zip'.format(parts['user'], parts['repo'], parts['sha']) return github_url + def install(self): + self.download() + self.extract() + + def extract(self): + dest = self.get_plugin_folder() + with zipfile.ZipFile(self.get_local_destination(), "r") as z: + z.extractall(dest) + + github_folder = os.path.join(dest, '{}-{}'.format(self.plugin_parts['repo'], self.plugin_parts['sha'])) + new_folder = os.path.join(dest, '{}'.format(self.plugin_parts['repo'])) + shutil.move(github_folder, new_folder) + + with open(os.path.join(dest, '.sha'), 'w') as file: + file.write(self.plugin_parts['sha']) + + os.remove(self.get_local_destination()) + def download(self): url = self.get_github_download_url() dest = self.get_local_destination() diff --git a/pokemongo_bot/test/plugin_loader_test.py b/pokemongo_bot/test/plugin_loader_test.py index 0b0f7da9d1..ed285ede67 100644 --- a/pokemongo_bot/test/plugin_loader_test.py +++ b/pokemongo_bot/test/plugin_loader_test.py @@ -11,6 +11,8 @@ from pokemongo_bot.plugin_loader import PluginLoader, GithubPlugin from pokemongo_bot.test.resources.plugin_fixture import FakeTask +PLUGIN_PATH = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins')) + class PluginLoaderTest(unittest.TestCase): def setUp(self): self.plugin_loader = PluginLoader() @@ -26,31 +28,39 @@ def test_load_zip(self): package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip') self.plugin_loader.load_plugin(package_path) loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') - self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') + self.assertEqual(loaded_class({}, {}).work(), 'FakeTaskZip') self.plugin_loader.remove_path(package_path) - def copy_zip(self): - zip_fixture = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip') - dest_path = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip')) - shutil.copyfile(zip_fixture, dest_path) + def copy_plugin(self): + package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture') + dest_path = os.path.join(PLUGIN_PATH, 'org_repo', 'plugin_fixture_tests') + shutil.copytree(package_path, os.path.join(dest_path)) + with open(os.path.join(os.path.dirname(dest_path), '.sha'), 'w') as file: + file.write('testsha') return dest_path def test_load_github_already_downloaded(self): - dest_path = self.copy_zip() - self.plugin_loader.load_plugin('org/repo#sha') - loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') + dest_path = self.copy_plugin() + self.plugin_loader.load_plugin('org/repo#testsha') + loaded_class = self.plugin_loader.get_class('plugin_fixture_tests.FakeTask') self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') self.plugin_loader.remove_path(dest_path) - os.remove(dest_path) + shutil.rmtree(os.path.dirname(dest_path)) + + def copy_zip(self): + zip_name = 'test-pgo-plugin-2d54eddde33061be9b329efae0cfb9bd58842655.zip' + fixture_zip = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', zip_name) + zip_dest = os.path.join(PLUGIN_PATH, 'org_test-pgo-plugin_2d54eddde33061be9b329efae0cfb9bd58842655.zip') + shutil.copyfile(fixture_zip, zip_dest) @mock.patch.object(GithubPlugin, 'download', copy_zip) def test_load_github_not_downloaded(self): - self.plugin_loader.load_plugin('org/repo#sha') - loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask') - self.assertEqual(loaded_class({}, {}).work(), 'FakeTask') - dest_path = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip')) - self.plugin_loader.remove_path(dest_path) - os.remove(dest_path) + self.plugin_loader.load_plugin('org/test-pgo-plugin#2d54eddde33061be9b329efae0cfb9bd58842655') + loaded_class = self.plugin_loader.get_class('test-pgo-plugin.PrintText') + self.assertEqual(loaded_class({}, {}).work(), 'PrintText') + dest_path = os.path.join(PLUGIN_PATH, 'org_test-pgo-plugin') + self.plugin_loader.remove_path(os.path.join(dest_path, 'test-pgo-plugin')) + shutil.rmtree(dest_path) class GithubPluginTest(unittest.TestCase): def test_get_github_parts_for_valid_github(self): @@ -65,10 +75,25 @@ def test_get_github_parts_for_invalid_github(self): self.assertFalse(GithubPlugin('foo').is_valid_plugin()) self.assertFalse(GithubPlugin('/Users/foo/bar.zip').is_valid_plugin()) + def test_get_installed_version(self): + github_plugin = GithubPlugin('org/repo#my-version') + src_fixture = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_sha') + dest = github_plugin.get_plugin_folder() + shutil.copytree(src_fixture, dest) + actual = github_plugin.get_installed_version() + shutil.rmtree(dest) + self.assertEqual('my-version', actual) + + def test_get_plugin_folder(self): + github_plugin = GithubPlugin('org/repo#sha') + expected = os.path.join(PLUGIN_PATH, 'org_repo') + actual = github_plugin.get_plugin_folder() + self.assertEqual(actual, expected) + def test_get_local_destination(self): github_plugin = GithubPlugin('org/repo#sha') path = github_plugin.get_local_destination() - expected = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip')) + expected = os.path.join(PLUGIN_PATH, 'org_repo_sha.zip') self.assertEqual(path, expected) def test_get_github_download_url(self): @@ -77,13 +102,47 @@ def test_get_github_download_url(self): expected = 'https://github.com/org/repo/archive/sha.zip' self.assertEqual(url, expected) - def test_is_already_downloaded_not_downloaded(self): + def test_is_already_installed_not_installed(self): + github_plugin = GithubPlugin('org/repo#sha') + self.assertFalse(github_plugin.is_already_installed()) + + def test_is_already_installed_version_mismatch(self): github_plugin = GithubPlugin('org/repo#sha') - self.assertFalse(github_plugin.is_already_downloaded()) + plugin_folder = github_plugin.get_plugin_folder() + os.mkdir(plugin_folder) + with open(os.path.join(plugin_folder, '.sha'), 'w') as file: + file.write('sha2') - def test_is_already_downloaded_downloaded(self): + actual = github_plugin.is_already_installed() + shutil.rmtree(plugin_folder) + self.assertFalse(actual) + + def test_is_already_installed_installed(self): github_plugin = GithubPlugin('org/repo#sha') - dest = github_plugin.get_local_destination() - open(dest, 'a').close() - self.assertTrue(github_plugin.is_already_downloaded()) - os.remove(dest) + plugin_folder = github_plugin.get_plugin_folder() + os.mkdir(plugin_folder) + with open(os.path.join(plugin_folder, '.sha'), 'w') as file: + file.write('sha') + + actual = github_plugin.is_already_installed() + shutil.rmtree(plugin_folder) + self.assertTrue(actual) + + def test_extract(self): + github_plugin = GithubPlugin('org/test-pgo-plugin#2d54eddde33061be9b329efae0cfb9bd58842655') + src = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'test-pgo-plugin-2d54eddde33061be9b329efae0cfb9bd58842655.zip') + zip_dest = github_plugin.get_local_destination() + shutil.copyfile(src, zip_dest) + github_plugin.extract() + plugin_folder = github_plugin.get_plugin_folder() + os.path.isdir(plugin_folder) + sub_folder = os.path.join(plugin_folder, 'test-pgo-plugin') + os.path.isdir(sub_folder) + sha_file = os.path.join(github_plugin.get_plugin_folder(), '.sha') + os.path.isfile(sha_file) + + with open(sha_file) as file: + content = file.read().strip() + self.assertEqual(content, '2d54eddde33061be9b329efae0cfb9bd58842655') + + shutil.rmtree(plugin_folder) diff --git a/pokemongo_bot/test/resources/plugin_fixture_test.zip b/pokemongo_bot/test/resources/plugin_fixture_test.zip index 335d95e5226056902bb02114fa26a2b3dad61ca1..78828798c32b46cb56eac6df953e7add47c175cf 100644 GIT binary patch delta 564 zcmca2HJP6$z?+$civa`#w@>8Jl2Daj5iSJ8Ul~OhKyo4s96%_teWIT+y8uvJTzBe z?UTP2rKskudemEWZEnUbYd?z(IW;{;`c(fVsP44FwpL~y9Tq!hyg8^X%D;p?TfTDjV69dB$77z~reUPTy delta 1852 zcmbQte?^KXz?+$civa|hB`5M|Ni>Nq4=?3h9{!b4gaITc!oUH9*^(3ejM+PYa-N*a zCw5$9`lT>Ano(W?Bm%-P)ew__l9L#XF(hF|O}@)$2QzE35>pp*wb=5>>zSgNnk6Um zFsn_T%fwxu92&vH02GJ2C3J$f7mJ}loA%u%egPMortW(gdJ2p5N|tt5b!_I$Ek7$@ zeOEL{^n!4u1OI}lnT|R^@;_Jqvw6M#@7~*Q&p+p@TYPEHg(u0b>km6HNbu_L&RC?) zA=+cAlYQ0H#w!PKT7Ud{i%gXhJ}3-CneNN*1o)(D|TvK($X^Z zsA>)K3CgbjKJPaBwKKXb`+@uq*F^E2PF=SA1-Y9&tvsxub#AZ$Ld+zUtRsO=&js}f~1>2B)a8U!`+N;<}#bJOulBY_=Y|AH+GN< zjqOD|Yk@AD1jLBM(JaaC2^4?>MFxS%A6Y~vvGZU?Ha(dCfU_y# zJCoA3DAyxqDr=MKrkUKFGGmGKrsymAd%M}cFj#HQ?2cRDU3TL3+{*mty}xU3RNwet z<(05E=FapTUp3oA?AcTujw&P_2x3=h?s0r|Nc{l&f#N3)w;C_pKK$hmd;Rp|AI{4p zNF0muOOA~w5Ed89Y1PQ}a5^rbB4B??P*(ZfiB^R#GD%C%w5;{ch!2cpfAjd(oI;1^ zav!(-Dp{R6bE~^ZNlWATEjcFBQ_sD2+k4_j#=F%{VOf={&$XTUu*;QO^;t*Hl{WLB z)uEF;&b}_%dSbfL>HB9^O|&^YujSyf`d?pnht`DpUR2dRUiPBYER17gkG5vZ%gBJ0 zC80t#juPi;>VXr zkcQ=D^yt!oMAu{i7IDn@IvR8v9$yOkIO-e)-u!FmSSu#Dk=Jwejp?en+f>a?>fPjh zwfSGS{0sIMXSRiQ*}a}*`=rIP_?giE+U=#A|CX7zUj6uS-a}J&4oy3rqK2k}GZ?*X zC-9{41j)xS#xcA*F1LiYqgj{de7)TV7M}ZW_?*lQi)PGdz3XnI<-2z4sx>~=R*TQa zPg8j@=_z9qOOaEu*O?QkZ=6Ng<({hFm@s$p8pd^JQhn~TvVIWeS;l5(w8<#undJ4U zRTF}Ydi_P#q@3e)-CQ>TgPkuMlHMQZ^MeEQ*)o>`xm_S6ASG0`%&M$pmM&Bdd;b8 zr*C|=V+6&S>c+C#5MZ2r1IHO7lPEJ60~;t00ZYM4EEZz0f;hk%BnK}$6<8g`pyhBl zL<(N!mb2=Dr9jeXQoEqK5K?dh1=)Pyg>ed7kP@uKLADo^WDup(Z@U diff --git a/pokemongo_bot/test/resources/plugin_sha/.sha b/pokemongo_bot/test/resources/plugin_sha/.sha new file mode 100644 index 0000000000..eaf604c9ac --- /dev/null +++ b/pokemongo_bot/test/resources/plugin_sha/.sha @@ -0,0 +1 @@ +my-version diff --git a/pokemongo_bot/test/resources/test-pgo-plugin-2d54eddde33061be9b329efae0cfb9bd58842655.zip b/pokemongo_bot/test/resources/test-pgo-plugin-2d54eddde33061be9b329efae0cfb9bd58842655.zip new file mode 100644 index 0000000000000000000000000000000000000000..a692ac3f08492882a28820569a62b59d2647006f GIT binary patch literal 1734 zcmWIWW@h1H00EUv?4Do-l(1yrWGG23F3~MW&(|%;DNWDJ(=|#lHAziLNl7&}HZU_x zO0`TfHnL1jOH4ILPD`>(N-?#tFflSSHPsIdVP#;vD!L*(0BSx5*!)Ruz2?453=EZY+*mHVk{C#!H>m@IgCcmF3;%OWIa(3v7jeA_4XI_~5Oe3#w|LWT6`t|M)BHw;5 z+n=YH5|>_-JvUN%$C}s*HSH>gtm6wN z>+z<~X`LdrR!?om#7$FL-8LR)Hxx0cST=G(^n!Ao*PN!P%_x4c&Yp4}6yT<(=;iumAbz&AQdA zZ&miDu6pyqY{&0vBgJIj^}ix^Xmn;rZ$Gc*vu3l*@sG+0XBOK;KmNYMRbe{M*{KKg z1#UkS3ZHKp;j~)+igT;V&YQQKUzpGKiP7JfyDm@l(VIh&{5k#HtSfmP)6B|$hs%cL z-Vu>b?(h`$WPNXT_7%I>tC{=(VKL78kN18PkUSq&-?GE$%Bs6dE+vRQovzVx^zsr> zAwwqCvnDcs)l9Y;wr1$LcuEumYKHRdP@m2srF(DXKlAzWbtShww4<&X7K&a;&784I zXnjTJpHmb62~TH;U-XXwHQCl5-?K;#m@VXi*oBg08y}yUmst`YuUAkBPq*IMr_P6- zSlgqoaq5hA;E6N3C-k)fIT#9^g0#JMpgF|$*O_^9fsQy&sY7xr;|q#1^Gc8%;@jiT z-Q*y0&rU{@JX(=-uSI>R&%z*2v5=-aj+ac?0i)BdZKc zIh0>MFi`i2ev^10>}y8W6xK`YPRyM&Ww+R@ItTB9c}#|t$4=>$^X