Created
November 15, 2019 12:50
-
-
Save impshum/c4e01bddb824812ceea11493be36c44e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import configparser | |
import praw | |
import pickledb | |
import time | |
import sys | |
from requests import get | |
import datetime | |
ignore = ['-en-', 'trite_username', '0imp', '1uvx', '--og', 'Mr_E'] | |
class C: | |
W, G, R, P, Y, C = '\033[0m', '\033[92m', '\033[91m', '\033[95m', '\033[93m', '\033[36m' | |
def intro(test_mode): | |
if test_mode: | |
t = f'{C.R}TEST MODE{C.Y}' | |
else: | |
t = '' | |
print(f"""{C.Y} | |
╔═╗╦ ╦╦═╗\│/╔╗ ╔═╗╔╦╗ | |
║ ╠═╣╠╦╝─ ─╠╩╗║ ║ ║ {t} | |
╚═╝╩ ╩╩╚═/│\╚═╝╚═╝ ╩ {C.C}v1.0{C.W} | |
""") | |
def check_four(flairs_db, reddit, secret_subreddit, username): | |
if db.exists(username): | |
get_user_flair = flairs_db.get(username) | |
if get_user_flair and 'FOUR' not in get_user_flair: | |
new_flair = f'{get_user_flair} 🕴' | |
reddit.subreddit(secret_subreddit).flair.set(username, new_flair) | |
flairs_db.set(username, f'{get_user_flair} FOUR') | |
flairs_db.dump() | |
print(f'set 🕴 flair for {username}') | |
else: | |
print(f'🕴 flair already set for {username}') | |
def four_time(flairs_db, reddit, secret_subreddit, username): | |
created = time.strftime('%-I %-M %-S').split(' ') | |
if all(x == created[0] for x in created): | |
check_four(flairs_db, reddit, secret_subreddit, username) | |
def wait_until_online(timeout, slumber): | |
offline = 1 | |
while offline: | |
try: | |
r = get('https://www.google.com', timeout=timeout).status_code | |
except ConnectionError: | |
r = None | |
if r == 200: | |
offline = 0 | |
else: | |
print('OFFLINE - WAITING FOR NETWORK') | |
time.sleep(slumber) | |
def db_insert(test_mode, db, reddit, secret_subreddit, username, username_length): | |
#now = time.strftime("%d/%m/%Y %H:%M:%S") | |
now = time.strftime("%H:%M:%S") | |
if not db.exists(username): | |
db.set(username, 0) | |
if not test_mode: | |
db.dump() | |
invite_user(reddit, secret_subreddit, username) | |
c = len(db.getall()) | |
print(f'{C.Y}{now} {C.W}{username} {C.G}invited and added to db{C.W} {C.P}{c}{C.W}') | |
# else: | |
# print(f'{C.P}{now} - {username} exists in db{C.W}') | |
def invite_user(reddit, secret_subreddit, author): | |
try: | |
reddit.subreddit(secret_subreddit).contributor.add(author) | |
if author not in ignore: | |
reddit.subreddit(secret_subreddit).flair.set(author, 'REVIVER') | |
except Exception as e: | |
print(e) | |
print(f'------------------>MISSED {author}') | |
with open('missed.txt', 'a+') as f: | |
f.write(f'{author}\n') | |
# def read_db(): | |
# for x in db.getall(): | |
# print(x, db.get(x)) | |
def give_new_flairs(db, reddit, secret_subreddit): | |
for author in db.getall(): | |
if author not in ignore: | |
reddit.subreddit(secret_subreddit).flair.set(author, 'REVIVER') | |
print(author) | |
time.sleep(2) | |
def give_old_flairs(reddit, secret_subreddit): | |
for submission in reddit.subreddit(secret_subreddit).new(limit=None): | |
if submission.author: | |
username = submission.author.name | |
if username not in ignore: | |
if submission.archived: | |
if not submission.author_flair_text: | |
reddit.subreddit(secret_subreddit).flair.set( | |
username, 'SURVIVOR') | |
print(username) | |
time.sleep(2) | |
for comment in submission.comments.list(): | |
if comment.author: | |
username = comment.author.name | |
if username not in ignore: | |
if comment.archived: | |
if not comment.author_flair_text: | |
reddit.subreddit(secret_subreddit).flair.set( | |
username, 'SURVIVOR') | |
print(username) | |
time.sleep(2) | |
def invite_missed(reddit, secret_subreddit): | |
with open('missed.txt') as f: | |
data = f.readlines() | |
count = len(data) | |
c = 0 | |
for line in data: | |
c += 1 | |
author = line.strip() | |
invite_user(reddit, secret_subreddit, author) | |
print(f'{c}/{count}: {author}') | |
time.sleep(1) | |
def runner(): | |
config = configparser.ConfigParser() | |
config.read('conf.ini') | |
reddit_user = config['REDDIT']['reddit_user'] | |
reddit_pass = config['REDDIT']['reddit_pass'] | |
client_id = config['REDDIT']['client_id'] | |
client_secret = config['REDDIT']['client_secret'] | |
target_subreddit = config['REDDIT']['target_subreddit'] | |
secret_subreddit = config['REDDIT']['secret_subreddit'] | |
username_length = int(config['REDDIT']['username_length']) | |
test_mode = int(config['REDDIT']['test_mode']) | |
intro(test_mode) | |
db = pickledb.load('data.db', False) | |
flairs_db = pickledb.load('flairs.db', False) | |
reddit = praw.Reddit( | |
username=reddit_user, | |
password=reddit_pass, | |
client_id=client_id, | |
client_secret=client_secret, | |
user_agent='4CHR inviter (by u/impshum)' | |
) | |
#give_new_flairs(db, reddit, secret_subreddit) | |
#give_old_flairs(reddit, secret_subreddit) | |
#invite_missed(reddit, secret_subreddit) | |
# sys.exit() | |
for submission in reddit.subreddit(target_subreddit).stream.submissions(): | |
if submission.author: | |
username = submission.author.name | |
if username not in ignore and len(username) == username_length: | |
db_insert(test_mode, db, reddit, | |
secret_subreddit, username, username_length) | |
four_time(flairs_db, reddit, secret_subreddit, username) | |
def main(): | |
try: | |
runner() | |
except Exception as e: | |
print(e) | |
#wait_until_online(10, 30) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment