From 8b6d6e3a87b624269e639129d78981404a92f427 Mon Sep 17 00:00:00 2001 From: itox Date: Thu, 10 Jun 2021 01:04:43 -0500 Subject: [PATCH] Make botterino a module installable via pip --- .gitignore | 7 ++- {Loader => Botterino/Loader}/__init__.py | 0 {Loader => Botterino/Loader}/loader.py | 4 +- .../RedditPoller}/RedditPoller.py | 2 +- .../RedditPoller}/Retry.py | 0 .../RedditPoller}/__init__.py | 0 {Utils => Botterino/Utils}/__init__.py | 0 {Utils => Botterino/Utils}/utils.py | 6 +- Botterino/__init__.py | 20 +++++++ Botterino/__main__.py | 1 + botterino.py => Botterino/botterino.py | 34 +++++------- config.py => Botterino/config.py | 20 ++++--- Botterino/failure.py | 20 +++++++ Botterino/hosterino.py | 4 +- Botterino/posterino.py | 4 +- LICENSE | 21 +++++++ README.md | 47 ++++++++++------ Utils/update.py | 55 ------------------- failure.py | 18 ------ pyproject.toml | 6 ++ requirements.txt | 7 --- Rounds/sample.yaml => sample-rounds.yaml | 25 +++++---- setup.cfg | 25 +++++++++ 23 files changed, 178 insertions(+), 148 deletions(-) rename {Loader => Botterino/Loader}/__init__.py (100%) rename {Loader => Botterino/Loader}/loader.py (89%) rename {RedditPoller => Botterino/RedditPoller}/RedditPoller.py (97%) rename {RedditPoller => Botterino/RedditPoller}/Retry.py (100%) rename {RedditPoller => Botterino/RedditPoller}/__init__.py (100%) rename {Utils => Botterino/Utils}/__init__.py (100%) rename {Utils => Botterino/Utils}/utils.py (94%) create mode 100644 Botterino/__main__.py rename botterino.py => Botterino/botterino.py (58%) rename config.py => Botterino/config.py (58%) create mode 100644 Botterino/failure.py create mode 100644 LICENSE delete mode 100644 Utils/update.py delete mode 100644 failure.py create mode 100644 pyproject.toml delete mode 100644 requirements.txt rename Rounds/sample.yaml => sample-rounds.yaml (89%) create mode 100644 setup.cfg diff --git a/.gitignore b/.gitignore index 904ac75..3e64000 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,9 @@ __pycache__/ Rounds/rounds.yaml Rounds/archive.yaml praw.ini -venv/ \ No newline at end of file +venv/ +TODO +build +dist +*.egg-info* +.vscode \ No newline at end of file diff --git a/Loader/__init__.py b/Botterino/Loader/__init__.py similarity index 100% rename from Loader/__init__.py rename to Botterino/Loader/__init__.py diff --git a/Loader/loader.py b/Botterino/Loader/loader.py similarity index 89% rename from Loader/loader.py rename to Botterino/Loader/loader.py index 5706d91..92f0bfb 100644 --- a/Loader/loader.py +++ b/Botterino/Loader/loader.py @@ -1,5 +1,5 @@ from pathlib import Path -from config import roundfile, archivefile +from ..config import roundfile, archivefile import ruamel.yaml yaml = ruamel.yaml.YAML() @@ -17,7 +17,7 @@ def getRound(): top = x.pop(k) if x: yaml.dump(x, rounds) - else: + else: open(rounds, 'w').close() y = yaml.load(archive) or {} y[k] = top diff --git a/RedditPoller/RedditPoller.py b/Botterino/RedditPoller/RedditPoller.py similarity index 97% rename from RedditPoller/RedditPoller.py rename to Botterino/RedditPoller/RedditPoller.py index 4b8cfb2..f213586 100644 --- a/RedditPoller/RedditPoller.py +++ b/Botterino/RedditPoller/RedditPoller.py @@ -1,6 +1,6 @@ import itertools -from RedditPoller.Retry import retry +from .Retry import retry POLL_LIMIT = 50 diff --git a/RedditPoller/Retry.py b/Botterino/RedditPoller/Retry.py similarity index 100% rename from RedditPoller/Retry.py rename to Botterino/RedditPoller/Retry.py diff --git a/RedditPoller/__init__.py b/Botterino/RedditPoller/__init__.py similarity index 100% rename from RedditPoller/__init__.py rename to Botterino/RedditPoller/__init__.py diff --git a/Utils/__init__.py b/Botterino/Utils/__init__.py similarity index 100% rename from Utils/__init__.py rename to Botterino/Utils/__init__.py diff --git a/Utils/utils.py b/Botterino/Utils/utils.py similarity index 94% rename from Utils/utils.py rename to Botterino/Utils/utils.py index 69934dc..200dc3c 100644 --- a/Utils/utils.py +++ b/Botterino/Utils/utils.py @@ -1,6 +1,6 @@ -from config import pg, username, debug, reddit, donotreply -from RedditPoller.RedditPoller import RedditPoller, CommentWrapper -from RedditPoller.Retry import retry +from ..config import pg, username, debug, reddit, donotreply +from ..RedditPoller.RedditPoller import RedditPoller, CommentWrapper +from ..RedditPoller.Retry import retry from geopy.point import Point from geopy.distance import distance from sty import fg diff --git a/Botterino/__init__.py b/Botterino/__init__.py index e69de29..7ad5f04 100644 --- a/Botterino/__init__.py +++ b/Botterino/__init__.py @@ -0,0 +1,20 @@ +import os +from pathlib import Path + +class files(): + def __init__(self): + self.home = os.path.expanduser('~') + self.botconfig = os.path.join(self.home, 'botterino-config') + self.roundsdir = os.path.join(self.botconfig, 'rounds') + self.rounds = os.path.join(self.roundsdir, 'rounds.yaml') + self.archive = os.path.join(self.roundsdir, 'archive.yaml') + self.prawconfig = os.path.join(self.botconfig, 'praw.ini') + +botfiles = files() +dirs = [botfiles.botconfig, botfiles.roundsdir] +files = [botfiles.rounds, botfiles.archive, botfiles.prawconfig] +for d in dirs: + Path(d).mkdir(parents=True, exist_ok=True) +for f in files: + with open(f, 'a+'): + pass diff --git a/Botterino/__main__.py b/Botterino/__main__.py new file mode 100644 index 0000000..693bcc3 --- /dev/null +++ b/Botterino/__main__.py @@ -0,0 +1 @@ +from . import botterino \ No newline at end of file diff --git a/botterino.py b/Botterino/botterino.py similarity index 58% rename from botterino.py rename to Botterino/botterino.py index 8dcc62e..f3c4e8a 100644 --- a/botterino.py +++ b/Botterino/botterino.py @@ -1,12 +1,19 @@ -from config import pg, username +from .config import pg, username +from .posterino import submitRound +from .hosterino import checkAnswers +from .Utils.utils import waitForApproval, approved, postDelay, randomColor +from .Loader.loader import getRound from sty import fg -from Utils.utils import waitForApproval, approved, postDelay, randomColor -from Utils import update -from Loader.loader import getRound -from Botterino.posterino import submitRound -from Botterino.hosterino import checkAnswers +from importlib.metadata import version +from update_checker import UpdateChecker import time -import configparser + +v = version('botterino') +checker = UpdateChecker() +result = checker.check('botterino', v) +if result: + print(f'{fg.yellow}{result}') + print(f'{fg.yellow}run pip install --upgrade botterino to update') def checkType(r): if 'tolerance' in r and 'answer' in r: @@ -17,19 +24,6 @@ def checkType(r): return 'x wrong guesses, manual correct' return 'manual' -parser = configparser.ConfigParser() -parser.read('praw.ini') -if not parser['botterino'].get('donotupdate'): - print(f'{fg.cyan}Checking for updates...') - if update.hasUpdate(): - doUpdate = input(f'{randomColor()}There is an update available! Would you like to update? Enter Y/N ').lower() == 'y' - if doUpdate: - update.doUpdate() - print(f'{fg.li_green}Successfully updated. Please restart botterino') - exit(0) - else: - print(f'{fg.yellow}You are up to date!') - while True: print(f'{fg.yellow}Waiting for {username} to win a round... 🐌') waitForApproval() diff --git a/config.py b/Botterino/config.py similarity index 58% rename from config.py rename to Botterino/config.py index 2d3c9fd..a4bf72f 100644 --- a/config.py +++ b/Botterino/config.py @@ -1,19 +1,23 @@ import praw +import os +from . import botfiles from sty import fg debug = False - -roundfile = 'Rounds/rounds.yaml' -archivefile = 'Rounds/archive.yaml' - -reddit = praw.Reddit('botterino') -pg = reddit.subreddit('itoxtestingfacility' if debug else 'picturegame') - +roundfile = botfiles.rounds +archivefile = botfiles.archive +cwd = os.getcwd() try: + os.chdir(botfiles.botconfig) + reddit = praw.Reddit('botterino') print(f'{fg.green}Successfully logged into reddit as {reddit.user.me()}') except Exception as e: - print(f'{fg.red}Unable to login to reddit. Please check praw.ini') + print(f'{fg.red}Unable to login to reddit. Please check {botfiles.prawconfig}') print(f'{fg.red}{e}') +finally: + os.chdir(cwd) + +pg = reddit.subreddit('itoxtestingfacility' if debug else 'picturegame') username = str(reddit.user.me()) diff --git a/Botterino/failure.py b/Botterino/failure.py new file mode 100644 index 0000000..ead62f6 --- /dev/null +++ b/Botterino/failure.py @@ -0,0 +1,20 @@ +from .hosterino import checkAnswers +from .config import pg +from .Utils.utils import randomColor +from .Loader import loader +from sty import fg +import time + + +r = loader.getRound() +while not r: + print(f'{fg.red}No rounds in round file! checking again in 10s') + time.sleep(10) + r = loader.getRound() + +submission = next(iter(pg.new())) +print(f'{randomColor()}Checking answers on https://reddit.com{submission.permalink}') +checkAnswers(r, submission) +after = r.get(after) +if after: + submission.reply(after) \ No newline at end of file diff --git a/Botterino/hosterino.py b/Botterino/hosterino.py index e6563b1..286b555 100644 --- a/Botterino/hosterino.py +++ b/Botterino/hosterino.py @@ -1,10 +1,10 @@ from geopy.distance import distance from geopy.point import Point -from config import donotreply, incorrect, reddit, username, pg +from .config import donotreply, incorrect, reddit, username, pg from itertools import permutations import re from sty import fg -from Utils.utils import decimal, getComments, getDistance, randomColor, randomColorWithAuthor +from .Utils.utils import decimal, getComments, getDistance, randomColor, randomColorWithAuthor def withinTolerance(guess, answer, tolerance): diff --git a/Botterino/posterino.py b/Botterino/posterino.py index 14ca730..7342b2e 100644 --- a/Botterino/posterino.py +++ b/Botterino/posterino.py @@ -1,5 +1,5 @@ -from config import pg -from Utils.utils import randomColor, getRoundPrefix +from .config import pg +from .Utils.utils import randomColor, getRoundPrefix import time def submitRound(r): diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..691379a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 itoxici + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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. \ No newline at end of file diff --git a/README.md b/README.md index 1909117..82874a6 100644 --- a/README.md +++ b/README.md @@ -9,34 +9,43 @@ It will reply with 'x' or '+correct' to any comments on your round automatically ## Pre-requisites +0. All the files you interact with will live in the botterino-config folder which is located: + 1. windows: `C:\Users\your username\botterino-config` + 2. mac: `/Users/=42", + "wheel" +] +build-backend = "setuptools.build_meta" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 568d061..0000000 --- a/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -ruamel.yaml -pyyaml -sty -geopy -praw -requests -configparser \ No newline at end of file diff --git a/Rounds/sample.yaml b/sample-rounds.yaml similarity index 89% rename from Rounds/sample.yaml rename to sample-rounds.yaml index 9b8ebfb..cdb4537 100644 --- a/Rounds/sample.yaml +++ b/sample-rounds.yaml @@ -1,27 +1,30 @@ -# indentation must be two spaces for all rounds +# indentation must be two spaces for all rounds # post a round but do not reply to any comments (host everything yourself) -# put the title in quotes (either single or double). +vegas_round: + title: 'What are my coordinates?' + url: imgur.com/notarealurl + +# put the title in quotes (either single or double). # If the title itself contains quotes , use the opposite style of quotes. # If an apostrophe is used in your title, it is counted as a single quote -vegas_round: - title: 'What are my coordinates?' - answer: 36.170439, -115.139889 + # post a round and reply to all comments # must specify answer and tolerance # answer must be in decimal format vegas_round: title: 'What are my coordinates?' - answer: 36.170439, -115.139889 - tolerance: 50.0 - url: https://i.imgur.com/qBRRrbD.jpg + answer: 36.170439, -115.139889 + tolerance: 50.0 + url: https://i.imgur.com/qBRRrbD.jpg # post a round, only reply to comments with incorrect coordinates # this means you will have to correct the winning comment yourself # useful if you want to ask an additional question +manualround: title: 'What are my coordinates? What is the name of the man to my right?' answer: 76.170439, -85.139889 tolerance: 20.0 @@ -49,9 +52,9 @@ Japanese: In this series I will be doing a music video round for 100 lanuages in order of total number of speakers - [here](https://www.visualcapitalist.com/wp-content/uploads/2020/02/the-100-most-spoken-languages.jpg) is the full list - I will be using. - + [here](https://www.visualcapitalist.com/wp-content/uploads/2020/02/the-100-most-spoken-languages.jpg) is the full list + I will be using. + Maksing only indicates the language and not necessarily the location # post a message after the round is over diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3f02e11 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,25 @@ +[metadata] +name = botterino +version = 0.0.6 +author = itoxici +author_email = itox@picturegame.co +description = Automate posting and hosting of rounds on /r/picturegame +long_description = file: README.md +long_description_content_type = text/markdown +url = https://github.com/pgitox/botterino +classifiers = + Programming Language :: Python :: 3 + License :: OSI Approved :: MIT License + Operating System :: OS Independent + +[options] +packages = find: +python_requires = >=3.8 +include_package_data = True +install_requires= + praw + geopy + pyyaml + ruamel.yaml + sty + requests \ No newline at end of file