def __init__(self, id, secret): self.client_id = id self.client_secret = secret self.auth = None self.mashape_key = None access = CredentialsLoader.get_credentials()['imgur'].get( 'access_token', None) refresh = CredentialsLoader.get_credentials()['imgur'].get( 'refresh_token', None) # imgur_credentials = self.loadimgur() if access and refresh: self.auth = AuthWrapper(access, refresh, id, secret) else: # Oauth setup print("Imgur Auth URL: ", self.get_auth_url('pin')) pin = input("Paste the pin here:") credentials = self.authorize(pin, 'pin') CredentialsLoader.set_credential('imgur', 'access_token', credentials['access_token']) CredentialsLoader.set_credential('imgur', 'refresh_token', credentials['refresh_token']) # self.saveimgur((credentials['access_token'], credentials['refresh_token'])) self.set_user_auth(credentials['access_token'], credentials['refresh_token']) self.auth = AuthWrapper(credentials['access_token'], credentials['refresh_token'], id, secret)
def bind_db(db): creds = CredentialsLoader.get_credentials()['database'] if creds['type'] == 'sqlite': db.bind(provider='sqlite', filename='../database.sqlite', create_db=True) elif creds['type'] == 'mysql': # Check for SSL arguments ssl = {} if creds.get('ssl-ca', None): ssl['ssl'] = { 'ca': creds['ssl-ca'], 'key': creds['ssl-key'], 'cert': creds['ssl-cert'] } db.bind(provider="mysql", host=creds['host'], user=creds['username'], password=creds['password'], db=creds['database'], ssl=ssl, port=int(creds.get('port', 3306))) else: raise Exception("No database configuration") db.generate_mapping(create_tables=True)
def _upload_image(self, file, media_type, nsfw, audio=False): file.seek(0) api = None params = None data = {"type": "file"} if media_type == consts.MP4 or media_type == consts.WEBM: data['video'] = ("video." + media_type, file, "video/" + media_type) data['name'] = "video." + media_type api = self.UPLOAD m = MultipartEncoder(fields=data) r = self.post_request(api, m, {'Content-Type': m.content_type}) # We get around the image file size restriction by using a client ID made by a browser # Luckily the API is similarish (rather than last time where it wasn't and also 3 steps) elif media_type == consts.GIF: s = requests.Session() api = self.IMAGE_UPLOAD params = {'client_id': CredentialsLoader.get_credentials()[self.CREDENTIALS_BLOCK]['imgur_web_id']} r = s.options(self.API_BASE + api, params=params) data['image'] = (file.name, file, "image/gif") data['name'] = file.name m = MultipartEncoder(fields=data) r = s.post(self.API_BASE + api, headers={'Content-Type': m.content_type}, data=m, params=params) # pprint(r.json()) j = r.json() if not j['data'].get('id', False): print(j) if j['data'].get('error', False): print("Error:", j['data']['error']) return None return j['data']['id']
def __init__(self): creds = CredentialsLoader.get_credentials()['gfycat'] self.gfypath = creds["gfypath"] self.gfyid = creds["gfycat_id"] self.gfysecret = creds["gfycat_secret"] self.username = creds.get('username', None) self.password = creds.get('password', None) self.token = creds.get('refresh_token', None) self.timeout = int(creds.get('token_expiration', 0))
def __init__(self): creds = CredentialsLoader.get_credentials()['gfycat'] self.gfyid = creds["gfycat_id"] self.gfysecret = creds["gfycat_secret"] self.access = creds.get('access_token', None) self.refresh = creds.get('refresh_token', None) self.timeout = int(creds.get('token_expiration', 0)) if self.refresh is None: self.authenticate(True) if self.access is None: self.get_token()
def __init__(self): creds = CredentialsLoader.get_credentials()[self.CREDENTIALS_BLOCK] self.client_id = creds["imgur_id"] self.client_secret = creds["imgur_secret"] self.access = creds.get('access_token', None) self.refresh = creds.get('refresh_token', None) self.timeout = int(creds.get('token_expiration', 0)) if self.refresh is None: self.authenticate() if self.access is None: self.get_token()
from core.credentials import CredentialsLoader user_agent = "CoffinBot v{} by /u/nGoline" spoof_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" version = "0.0.1" sleep_time = 90 username = CredentialsLoader.get_credentials()['reddit']['username'] bot_footer = "---\n\n^(I am a bot.) [^(Report an issue)]" \ "(https://www.reddit.com/message/compose/?to=nGoline&subject=CoffinBot%20Issue&message=" \ "Add a link to the video or comment in your message%2C I'm not always sure which request is being " \ "reported. Thanks for helping me out!)" nsfw_reply_template = "##NSFW\n\nHere is your video!\n{}\n\n" + bot_footer reply_template = "Here is your coffin meme!\n{}\n\n" + bot_footer reply_ban_subject = "Here is your coffin meme!" reply_ban_template = "Hi! Unfortunately, I am banned in that subreddit so I couldn't reply to your comment. " \ "I was still able to add the coffin meme to your video though!\n{}\n\n" + bot_footer unnecessary_manual_message = "\n\nJust so you know, you don't have to manually give the video URL if it is in " \ "a parent comment or the post. I would have known what you meant anyways :)\n\n" ignore_messages = [ "Welcome to Moderating!", "re: Here is your coffin meme!", "Your reddit premium subscription has expired." ] MP4 = 'mp4'
import praw from core.credentials import get_credentials, CredentialsLoader credentials = CredentialsLoader.get_credentials("../credentials.ini")['reddit'] from core.history import SwitcharooLog from core import constants as consts reddit = praw.Reddit(client_id=credentials["client_id"], client_secret=credentials["client_secret"], user_agent=consts.user_agent, username=credentials["username"], password=credentials["password"]) switcharoo = reddit.subreddit("switcharoo") last_switcharoo = SwitcharooLog(reddit) print("SwitcharooHelper Flair Sync v{} Ctrl+C to stop".format(consts.version)) for flair in switcharoo.flair(): print(flair) current_count = last_switcharoo.stats.num_of_good_roos(user=flair['user'].name) badge_count = 0 if flair['flair_css_class']: if flair['flair_css_class'][:6] == "badge-": badge_count = int(flair['flair_css_class'][6:]) if badge_count > current_count: last_switcharoo.update_user_flair(flair['user'].name, badge_count - current_count)
def get(cls): if not cls.instance: credentials = CredentialsLoader.get_credentials() cls.instance = cls(credentials['imgur']['imgur_id'], credentials['imgur']['imgur_secret']) return cls.instance
from core.credentials import CredentialsLoader user_agent = "GifReversingBot v{} by /u/pmdevita" spoof_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" imgur_spoof_cookie = CredentialsLoader.get_credentials( )['imgur']['imgur_cookie'] version = "2.6.9" sleep_time = 90 username = CredentialsLoader.get_credentials()['reddit']['username'] bot_footer = "---\n\n^(I am a bot.) [^(Report an issue)]" \ "(https://www.reddit.com/message/compose/?to=pmdevita&subject=GifReversingBot%20Issue)" nsfw_reply_template = "##NSFW\n\nHere is your gif!\n{}\n\n" + bot_footer reply_template = "Here is your gif!\n{}\n\n" + bot_footer reply_ban_subject = "Here is your gif!" reply_ban_template = "Hi! Unfortunately, I am banned in that subreddit so I couldn't reply to your comment. " \ "I was still able to reverse your gif though!\n{}\n\n" + bot_footer unnecessary_manual_message = "\n\nJust so you know, you don't have to manually give the gif URL if it is in " \ "a parent comment or the post. I would have known what you meant anyways :)\n\n" ignore_messages = ["Welcome to Moderating!"] VIDEO = 1 GIF = 2 OTHER = 3 LINK = 4
# Bootstrap the main library into the path import os import sys from pathlib import Path tools_folder = Path(os.path.dirname(os.path.realpath(__file__))) root = str(tools_folder.parent) if root not in sys.path: sys.path.append(root) from core.credentials import CredentialsLoader credentials = CredentialsLoader.get_credentials(tools_folder / "../credentials.ini")['reddit'] import praw import prawcore.exceptions from core.history import SwitcharooLog from core import constants as consts reddit = praw.Reddit(client_id=credentials["client_id"], client_secret=credentials["client_secret"], user_agent=consts.user_agent, username=credentials["username"], password=credentials["password"]) switcharoo = reddit.subreddit("switcharoo") last_switcharoo = SwitcharooLog(reddit) def pad_zeros(number, zeros): string = str(number) return "".join(["0" for i in range(max(zeros - len(string), 0))]) + string
from core.credentials import CredentialsLoader from core.history import * from core import constants as consts from core.action import PrintAction, ModAction from core.process import reprocess, check_errors, add_comment credentials = CredentialsLoader.get_credentials()['reddit'] reddit = praw.Reddit(client_id=credentials["client_id"], client_secret=credentials["client_secret"], user_agent=consts.user_agent, username=credentials["username"], password=credentials["password"]) last_switcharoo = SwitcharooLog(reddit) # Action object tracks switcharoo and performs a final action (delete/comment) mode = CredentialsLoader.get_credentials()['general']['mode'] if mode == 'production': action = ModAction(reddit) elif mode == 'development': action = PrintAction(reddit) def roo_id_to_submission(id): roo = last_switcharoo.get_roo(id) print(f"https://reddit.com{roo.submission.permalink}") def roo_id_to_comment(id): roo = last_switcharoo.get_roo(id)
def __init__(self): creds = CredentialsLoader.get_credentials()['streamable'] self.email = creds['email'] self.password = creds['password'] self.headers = {'User-Agent': consts.user_agent}
import praw import time import traceback import signal import prawcore.exceptions import os from core.credentials import CredentialsLoader reddit_creds = CredentialsLoader.get_credentials()['reddit'] credentials = CredentialsLoader.get_credentials()['general'] from core.process import process, reprocess from core.history import SwitcharooLog from core import constants as consts from core.action import PrintAction, ModAction from core.inbox import process_message, process_modmail reddit = praw.Reddit(client_id=reddit_creds["client_id"], client_secret=reddit_creds["client_secret"], user_agent=consts.user_agent, username=reddit_creds["username"], password=reddit_creds["password"]) switcharoo = reddit.subreddit("switcharoo") # Action object tracks switcharoo and performs a final action (delete/comment) mode = credentials['mode'] operator = credentials['operator']
import praw import time import pendulum import prawcore.exceptions from core.credentials import CredentialsLoader from core import constants as consts from core import parse credentials = CredentialsLoader.get_credentials()['reddit'] reddit = praw.Reddit(client_id=credentials["client_id"], client_secret=credentials["client_secret"], user_agent=consts.user_agent.format(consts.version), username=credentials["username"], password=credentials["password"]) switcharoo = reddit.subreddit("switcharoo") def get_newest_id(subreddit, index=0): """Retrieves the newest post's id. Used for starting the last switcharoo history trackers""" return [i for i in subreddit.new(params={"limit": "1"})][index].url print("Paste the URL of the switcharoo comment you'd like to start at\nOr leave blank to start at the newest") url = input() if not url: url = get_newest_id(switcharoo, 1) roo_count = 0
import requests from requests_toolbelt import MultipartEncoder import re from io import BytesIO from core.hosts import GifHost, Gif, GifFile from core.credentials import CredentialsLoader from core import constants as consts from core.file import is_valid catbox_hash = CredentialsLoader.get_credentials()['catbox']['hash'] class CatboxGif(Gif): process_id = True def _get_id(self, id, url): # Safety check if id: ext = id.split(".")[-1] else: ext = url.split(".")[-1] id = self.host.regex.findall(url)[0] if ext.lower() in [consts.MP4, consts.GIF, consts.WEBM]: # We should do file checks for safety because we could actually get some kind of nasty file return id return None def analyze(self): r = requests.get(self.url) file = BytesIO(r.content)
""" class Gif(db.Entity): id = PrimaryKey(int, auto=True) origin_host = Required(int) origin_id = Required(str) reversed_host = Required(int) reversed_id = Required(str) time = Required(date) nsfw = Optional(bool) total_requests = Optional(int) last_requested_date = Optional(date) creds = CredentialsLoader.get_credentials()['database'] if creds['type'] == 'sqlite': db.bind(provider='sqlite', filename='../database.sqlite', create_db=True) elif creds['type'] == 'mysql': db.bind(provider="mysql", host=creds['host'], user=creds['username'], passwd=creds['password'], db=creds['database']) else: raise Exception("No database configuration") db.generate_mapping(create_tables=True) def check_database(original_gif): # Have we reversed this gif before? with db_session:
from pprint import pprint import praw.exceptions import prawcore.exceptions from datetime import datetime, timedelta from core import parse from core.issues import IssueTracker from core.strings import NewIssueDeleteStrings from core.reddit import ReplyObject from core.constants import ONLY_BAD, ONLY_IGNORED, ALL_ROOS from core.history import SwitcharooLog, Switcharoo from core.credentials import CredentialsLoader from core.action import decide_subreddit_privated, increment_user_fixes import core.operator creds = CredentialsLoader.get_credentials()['general'] DRY_RUN = creds['dry_run'].lower() != "false" def process(reddit, submission, last_switcharoo, action): # First, add this submission to the database tracker = IssueTracker() tracker.submission_processing = True roo = last_switcharoo.add(submission.id, link_post=not submission.is_self, user=submission.author.name, roo_issues=tracker, time=datetime.utcfromtimestamp(submission.created_utc)) # Create an issue tracker made from it's errors tracker = check_errors(reddit, last_switcharoo, roo, init_db=True, submission=submission) # If it has issues, perform an action to correct it if tracker.has_issues(): action.process(tracker, ReplyObject(submission), last_switcharoo.last_good(before_roo=roo, offset=0))
import requests from io import BytesIO from imgurpython.imgur.models.gallery_image import GalleryImage from imgurpython.helpers.error import ImgurClientError import core.hosts.imgur from core import constants as consts from core.gif import Gif from core.regex import REPatterns from core.hosts.imgur import ImgurClient from core.hosts.gfycat import Gfycat as GfycatClient from core.hosts.streamable import StreamableClient from core.credentials import CredentialsLoader from core.file import get_duration creds = CredentialsLoader.get_credentials() imgur = ImgurClient.get() gfycat = GfycatClient.get() streamable = StreamableClient.get() class GifHost: type = None def __init__(self, context): self.context = context self.url = None def analyze(self): raise NotImplemented