Skip to content

Commit

Permalink
Make botterino a module installable via pip
Browse files Browse the repository at this point in the history
  • Loading branch information
itox committed Jun 11, 2021
1 parent fa4706c commit 8b6d6e3
Show file tree
Hide file tree
Showing 23 changed files with 178 additions and 148 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ __pycache__/
Rounds/rounds.yaml
Rounds/archive.yaml
praw.ini
venv/
venv/
TODO
build
dist
*.egg-info*
.vscode
File renamed without changes.
4 changes: 2 additions & 2 deletions Loader/loader.py → Botterino/Loader/loader.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import itertools

from RedditPoller.Retry import retry
from .Retry import retry

POLL_LIMIT = 50

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions Utils/utils.py → Botterino/Utils/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions Botterino/__init__.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions Botterino/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import botterino
34 changes: 14 additions & 20 deletions botterino.py → Botterino/botterino.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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()
Expand Down
20 changes: 12 additions & 8 deletions config.py → Botterino/config.py
Original file line number Diff line number Diff line change
@@ -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())

Expand Down
20 changes: 20 additions & 0 deletions Botterino/failure.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions Botterino/hosterino.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Botterino/posterino.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 29 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<your username/botterino-config`
3. linux: `~/botterino-config`
These files are created for you the first time you run the bot
1. You must have [Python](https://www.python.org/downloads/) installed on your computer
3. You must install items in 'requirements.txt'
1. Run the command `pip install -r requirements.txt`
4. You must [create a Reddit app](https://www.reddit.com/prefs/apps/)
1. If on windows it is best to install python from the [microsoft store](https://www.microsoft.com/en-us/p/python-39/9p7qfqmjrfp7?activetab=pivot:overviewtab)
2. Install the bot
1. open a terminal or command prompt window and type the following command: `pip install botterino`
3. You must [create a Reddit app](https://www.reddit.com/prefs/apps/) and add authentication details in
`botterino-config/praw.ini`; see `sample-praw.ini` for an example
1. Give app any name you choose, such as 'botterino'
2. Choose 'script' as app type
3. Fill in 'redirect URI' with `http://localhost:8080` (This is irrelevant but it's a required field)
4. Once created, you'll have a 'secret', copy/paste that as `client_secret` in praw.ini
4. Once created, you'll have a 'secret', copy/paste that as `client_secret` in botterino-config/praw.ini
5. You'll also have a less obvious client id, in the top left under the app name and the words 'personal use script' - copy/paste that into `client_id` in praw.ini
5. Fill out the rest of 'praw.ini' with your Reddit username/password as well as anything you want for `user_agent`
4. Fill out the rest of 'botterino-config/praw.ini' with your Reddit username/password as well as anything you want for `user_agent`

---

## Usage

- add round(s) in rounds.yaml
- python botterino.py
- add round(s) in botterino-config/rounds/rounds.yaml
- run with `python -m botterino`

### Normal hosting

Rounds are kept in the 'Rounds/rounds.yaml' file, see 'Rounds/sample.yaml' for some examples
Rounds are kept in the 'rounds/rounds.yaml' file, see 'sample.yaml' for some examples

#### Steps
1. Add round(s) to 'Rounds/rounds.yaml'
2. Start app with `python botterino.py`
1. Add round(s) to 'rounds/rounds.yaml'
2. Open a terminal or command prompt and type `python -m botterino`
1. Then all you have to do is win. Unil you win, botterino will do nothing
2. When you win, the top round in rounds.yaml is posted as soon as you are approved to host

Any new rounds added to 'Rounds/rounds.yaml' while the app is running will automatically be added to the queue, no need to restart.
Once a round is complete, it will be moved to 'Rounds/archive.yaml'.
Any new rounds added to 'botterino-config/rounds/rounds.yaml' while the app is running will automatically be added to the queue, no need to restart.
Once a round is complete, it will be moved to 'botterino-config/rounds/archive.yaml'.

### Live rounds

Expand All @@ -47,19 +56,21 @@ This is useful for cases where
2. Bot posts for you but crashes during hosting

#### Steps
1. Edit answer and tolerance in 'failure.py'
2. Run with `python failure.py`
1. Will use the top round in rounds.yaml
1. `url` field should be omitted
2. Run with `python -m botterino.failure`

---

## Misc

### Disable checking for updates
If the prompt for updating annoys you, add the following line to 'praw.ini'
```donotupdate=true```
### Update
Update this botterino with
`pip install --upgrade botterino`

### Colors on windows
This botterino uses colorful output. If you see strange output like this on windows
![Strange windows output](https://cdn.discordapp.com/attachments/768582651669381191/830607745769930762/unknown.png)
then download [Windows terminal](https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?rtc=1) from the Microsoft store.
then download [Windows terminal](https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?rtc=1)
from the Microsoft store.

55 changes: 0 additions & 55 deletions Utils/update.py

This file was deleted.

18 changes: 0 additions & 18 deletions failure.py

This file was deleted.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
]
build-backend = "setuptools.build_meta"
7 changes: 0 additions & 7 deletions requirements.txt

This file was deleted.

Loading

0 comments on commit 8b6d6e3

Please sign in to comment.