Skip to content

Commit

Permalink
Add forever flag. Close #4. v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdisadeghi committed May 31, 2018
1 parent 06ad6e4 commit d9872ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Now you can run the following command:
::

pip install clashogram
clashogram.py --coc-token <COC_API_TOKEN> --clan-tag <CLAN_TAG> --bot-token <TELEGRAM_BOT_TOKEN> --channel-name <TELEGRAM_CHANNEL_NAME>
clashogram.py --coc-token <COC_API_TOKEN> --clan-tag <CLAN_TAG> --bot-token <TELEGRAM_BOT_TOKEN> --channel-name <TELEGRAM_CHANNEL_NAME> --forever

In order to have messages in a different locale do the following and
then run the program:
Expand Down
2 changes: 1 addition & 1 deletion README_RU.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ clashogram |Build Status| |Build status| |Pypi status|
::

pip install clashogram
clashogram.py --coc-token <COC_API_TOKEN> --clan-tag <CLAN_TAG> --bot-token <TELEGRAM_BOT_TOKEN> --channel-name <TELEGRAM_CHANNEL_NAME>
clashogram.py --coc-token <COC_API_TOKEN> --clan-tag <CLAN_TAG> --bot-token <TELEGRAM_BOT_TOKEN> --channel-name <TELEGRAM_CHANNEL_NAME> --forever

В случае, если вам требуется изменить язык сообщений, используйте команды:

Expand Down
22 changes: 17 additions & 5 deletions clashogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
'Reads TELEGRAM_CHANNEL env var.',
envvar='TELEGRAM_CHANNEL',
prompt=True)
def main(coc_token, clan_tag, bot_token, channel_name):
@click.option('--forever',
is_flag=True,
help='Try to connect to the CoC server, no matter what.')
def main(coc_token, clan_tag, bot_token, channel_name, forever):
"""Publish war updates to a telegram channel."""
monitor_currentwar(coc_token, clan_tag, bot_token, channel_name)
monitor_currentwar(coc_token, clan_tag, bot_token, channel_name, forever)


def monitor_currentwar(coc_token, clan_tag, bot_token, channel_name):
def monitor_currentwar(coc_token, clan_tag, bot_token, channel_name, forever):
"""Send war news to telegram channel."""
with shelve.open('warlog.db', writeback=True) as db:
coc_api = CoCAPI(coc_token)
Expand All @@ -66,8 +69,16 @@ def monitor_currentwar(coc_token, clan_tag, bot_token, channel_name):
db.close()
raise
except Exception as err:
if '500' in str(err) and forever:
print('CoC internal server error, retrying.')
time.sleep(POLL_INTERVAL)
continue
if '502' in str(err) and forever:
print('CoC bad gateway, retrying.')
time.sleep(POLL_INTERVAL)
continue
if '503' in str(err):
print('COC maintenance error, ignoring.')
print('CoC maintenance error, retrying.')
time.sleep(POLL_INTERVAL)
continue
monitor.send(
Expand Down Expand Up @@ -132,7 +143,8 @@ def get_claninfo(self, clan_tag):

def call_api(self, endpoint):
s = requests.Session()
s.mount('https://api.clashofclans.com', HTTPAdapter(max_retries=5))
s.mount('https://api.clashofclans.com',
HTTPAdapter(max_retries=5))
res = s.get(endpoint,
headers={'Authorization': 'Bearer %s' % self.coc_token})
if res.status_code == requests.codes.ok:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


setup(name='Clashogram',
version='0.2.4',
version='0.3.0',
description='Clash of Clans war moniting for telegram channels.',
long_description=open('README.rst', encoding='utf-8').read(),
author='Mehdi Sadeghi',
Expand Down

0 comments on commit d9872ec

Please sign in to comment.