├── .gitignore ├── LICENSE.txt ├── Procfile ├── README.md ├── db.py ├── i18n.py ├── requirements.txt ├── runtime.txt ├── settings.py └── totes.py /.gitignore: -------------------------------------------------------------------------------- 1 | #Ignore PyCharm files 2 | .idea/ 3 | 4 | #Other files 5 | Procfile 6 | requirements.txt 7 | *.log 8 | .env 9 | totes.sqlite3 10 | /bin 11 | /share 12 | /lib 13 | /__pycache__ 14 | /i18n 15 | settings.pyc 16 | lib 17 | share 18 | bin 19 | include 20 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | worker: python totes.py 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TotesMessenger is a meta post linker bot. When links are made to other submissions, the bot will notify the linked comment about that submission. It is replacing /u/totes_meta_bot's function after they decided to [retire the bot](http://www.reddit.com/r/botsrights/comments/2xiw4p/im_retiring/). 2 | 3 | *Remember, follow the rules of reddit and do not vote when going in to linked threads.* 4 | 5 | If you have any questions, please [visit the FAQ](http://www.reddit.com/r/TotesMessenger/wiki/FAQ) first. 6 | 7 | 8 | 9 | #More Information 10 | 11 | * [Wiki](http://www.reddit.com/r/TotesMessenger/wiki) 12 | 13 | * [IRC Chatroom](https://kiwiirc.com/client/irc.snoonet.org/totesmessenger) 14 | 15 | * [Subreddit](http://www.reddit.com/r/TotesMessenger) -------------------------------------------------------------------------------- /db.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import sqlite3 4 | 5 | from urllib.parse import urlparse 6 | 7 | from settings import IGNORED_BOTH, IGNORED_LINKS, IGNORED_SOURCES, IGNORED_USERS 8 | 9 | 10 | db = sqlite3.connect('totes.sqlite3') 11 | cur = db.cursor() 12 | 13 | def create_tables(): 14 | """ 15 | Create tables. 16 | """ 17 | 18 | cur.execute(""" 19 | CREATE TABLE subreddits ( 20 | name TEXT PRIMARY KEY, 21 | skip_source BOOLEAN DEFAULT FALSE, 22 | skip_link BOOLEAN DEFAULT FALSE, 23 | language TEXT DEFAULT 'en', 24 | t TIMESTAMP DEFAULT CURRENT_TIMESTAMP 25 | ) 26 | """) 27 | 28 | cur.execute(""" 29 | CREATE TABLE users ( 30 | name TEXT PRIMARY KEY, 31 | skip_source BOOLEAN DEFAULT FALSE, 32 | skip_link BOOLEAN DEFAULT FALSE, 33 | t TIMESTAMP DEFAULT CURRENT_TIMESTAMP 34 | ) 35 | """) 36 | 37 | cur.execute(""" 38 | CREATE TABLE sources ( 39 | id TEXT PRIMARY KEY, 40 | reply TEXT UNIQUE, 41 | subreddit TEXT, 42 | author TEXT, 43 | title TEXT, 44 | skip BOOLEAN DEFAULT FALSE, 45 | t TIMESTAMP DEFAULT CURRENT_TIMESTAMP 46 | ) 47 | """) 48 | 49 | cur.execute(""" 50 | CREATE TABLE links ( 51 | id TEXT PRIMARY KEY, 52 | source TEXT, 53 | subreddit TEXT, 54 | author TEXT, 55 | title TEXT, 56 | permalink TEXT, 57 | skip BOOLEAN DEFAULT FALSE, 58 | t TIMESTAMP DEFAULT CURRENT_TIMESTAMP 59 | ) 60 | """) 61 | 62 | cur.execute(""" 63 | CREATE INDEX ON links (source) 64 | """) 65 | 66 | db.commit() 67 | print("Tables ready.") 68 | 69 | def sub_exists(sub): 70 | cur.execute("SELECT 1 FROM subreddits WHERE name=? LIMIT 1", (sub,)) 71 | return True if cur.fetchone() else False 72 | 73 | def user_exists(user): 74 | cur.execute("SELECT 1 FROM users WHERE name=? LIMIT 1", (user,)) 75 | return True if cur.fetchone() else False 76 | 77 | def populate_db(): 78 | for sub in IGNORED_SOURCES: 79 | if sub_exists(sub): 80 | print("Updating {}".format(sub)) 81 | cur.execute(""" 82 | UPDATE subreddits SET skip_source=%s 83 | WHERE name=%s 84 | """, (True, sub)) 85 | else: 86 | print("Inserting {}".format(sub)) 87 | cur.execute(""" 88 | INSERT INTO subreddits (name, skip_source) 89 | VALUES (%s, %s) 90 | """, (sub, True)) 91 | 92 | for sub in IGNORED_BOTH: 93 | if sub_exists(sub): 94 | print("Updating {}".format(sub)) 95 | cur.execute(""" 96 | UPDATE subreddits SET skip_source=%s, skip_link=%s 97 | WHERE name=%s 98 | """, (True, True, sub)) 99 | else: 100 | print("Inserting {}".format(sub)) 101 | cur.execute(""" 102 | INSERT INTO subreddits (name, skip_source, skip_link) 103 | VALUES (%s, %s, %s) 104 | """, (sub, True, True)) 105 | 106 | for sub in IGNORED_LINKS: 107 | if sub_exists(sub): 108 | print("Updating {}".format(sub)) 109 | cur.execute(""" 110 | UPDATE subreddits SET skip_link=%s 111 | WHERE name=%s 112 | """, (True, sub)) 113 | else: 114 | print("Inserting {}".format(sub)) 115 | cur.execute(""" 116 | INSERT INTO subreddits (name, skip_link) 117 | VALUES (%s, %s) 118 | """, (sub, True)) 119 | 120 | for user in IGNORED_USERS: 121 | if user_exists(user): 122 | print("Updating {}".format(user)) 123 | cur.execute(""" 124 | UPDATE users SET skip_link=%s 125 | WHERE name=%s 126 | """, (True, user)) 127 | else: 128 | print("Inserting {}".format(user)) 129 | cur.execute(""" 130 | INSERT INTO users (name, skip_link) VALUES (%s, %s) 131 | """, (user, True)) 132 | 133 | db.commit() 134 | print("Default settings setup.") 135 | 136 | if __name__ == '__main__': 137 | if 'create' in sys.argv: 138 | create_tables() 139 | 140 | if 'populate' in sys.argv: 141 | populate_db() 142 | 143 | db.close() 144 | 145 | -------------------------------------------------------------------------------- /i18n.py: -------------------------------------------------------------------------------- 1 | import codecs 2 | import os 3 | 4 | from os import path 5 | 6 | I18N_PATH = "i18n" 7 | DEFAULT_LANG = "en" 8 | 9 | 10 | class I18n: 11 | def __init__(self, langcode=None, path=None): 12 | if path is None: 13 | path = I18N_PATH 14 | 15 | if langcode is None: 16 | langcode = DEFAULT_LANG 17 | self.translations = {} 18 | import os 19 | for file in os.listdir(I18N_PATH): 20 | if file.endswith(".i18n"): 21 | fname = file.split(".") 22 | self.translations[fname[0]] = Translation(fname[0], file, path) 23 | self.checkvalidlanguage(langcode) 24 | self._langcode = langcode 25 | 26 | def get(self, str, langcode=None): 27 | if langcode is None: 28 | langcode = self._langcode 29 | self.checkvalidlanguage(langcode) 30 | return self.translations[langcode].get(str) 31 | 32 | def setlang(self, langcode): 33 | self.checkvalidlanguage(langcode) 34 | self._langcode = langcode 35 | 36 | def checkvalidlanguage(self, langcode): 37 | if langcode not in self.translations: 38 | raise TranslationNotFoundException(langcode) 39 | 40 | @property 41 | def name(self): 42 | if self._langcode is None: 43 | return None 44 | return self.translations[self._langcode].name 45 | 46 | 47 | class Translation: 48 | def __init__(self, code, file, path=None): 49 | if path is None: 50 | path = I18N_PATH 51 | self.code = code 52 | self.file = file 53 | self.alltext = self.read_from_file(path) 54 | try: 55 | self.name = self.get("language") 56 | except StringNotFoundException as e: 57 | from totes import log 58 | log.error(str(e)) 59 | self.name = "Unknown" 60 | 61 | def read_from_file(self, path): 62 | file = codecs.open(path + "/" + self.code + ".i18n", 'r', 63 | encoding='utf8') 64 | data = dict(line.split(":", 1) for line in file) 65 | file.close() 66 | return data 67 | 68 | def get(self, text): 69 | if text not in self.alltext: 70 | raise StringNotFoundException(text, self) 71 | 72 | return self.alltext[text].replace("\n", "").replace("\r", "").strip() 73 | 74 | 75 | class TranslationException(Exception): 76 | pass 77 | 78 | 79 | class TranslationNotFoundException(TranslationException): 80 | def __init__(self, code): 81 | self.code = code 82 | 83 | def __str__(self): 84 | return "Translation not found: {}".format(self.code) 85 | 86 | 87 | class StringNotFoundException(TranslationException): 88 | def __init__(self, string, translation): 89 | self.string = string 90 | self.translation = translation 91 | 92 | def __str__(self): 93 | return "Could not find '{}' in translation {}".format(self.string, 94 | self.translation.code) 95 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | praw==5.2.0 2 | -------------------------------------------------------------------------------- /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.4.2 2 | -------------------------------------------------------------------------------- /settings.py: -------------------------------------------------------------------------------- 1 | from os import environ 2 | 3 | # Testing and debug settings 4 | TEST = environ.get("TEST", "false") == "true" 5 | DEBUG = environ.get("DEBUG", "false") == "true" 6 | 7 | USER_AGENT = 'TotesMessenger v0.6 by /u/justcool393 and /u/cmd-t' 8 | 9 | # Database file 10 | DB_FILE = environ.get("DATABASE", "totes.sqlite3") 11 | 12 | # Posting settings 13 | POST_TIME = 2 * 60 # how long to wait until we should post (2 minutes in secs.) 14 | LINKS_BEFORE_TITLE_CUTOFF = 40 15 | TITLE_LIMIT = 140 - 3 # title character limit - 1 (for ellipsis) 16 | 17 | # Speed limits 18 | WAIT = int(environ.get("WAIT", 30)) # how long we wait between cycles 19 | LIMIT = int(environ.get("LIMIT", 25)) # how many posts done per cycle 20 | 21 | # Footer links 22 | INFO_LINK = "/r/TotesMessenger" 23 | CONTACT_LINK = "/message/compose?to=/r/TotesMessenger" 24 | TRANSLATION_ERROR_LINK = CONTACT_LINK + "&subject=translation error " \ 25 | "({language}\)" 26 | -------------------------------------------------------------------------------- /totes.py: -------------------------------------------------------------------------------- 1 | import ftplib 2 | import logging 3 | import os 4 | import praw 5 | import re 6 | import requests 7 | import sqlite3 8 | import sys 9 | import time 10 | import traceback 11 | 12 | from praw.exceptions import APIException, ClientException, PRAWException 13 | from requests.exceptions import RequestException 14 | 15 | # Internationalization stuff 16 | from i18n import TranslationException, I18n, DEFAULT_LANG 17 | 18 | from urllib.parse import urlparse 19 | from datetime import datetime, timezone 20 | 21 | # Settings 22 | from settings import * 23 | 24 | loglevel = logging.DEBUG if DEBUG else logging.INFO 25 | 26 | logging.basicConfig(level=loglevel, 27 | format='[%(asctime)s] [%(levelname)s] %(message)s') 28 | 29 | log = logging.getLogger('totes') 30 | logging.getLogger('prawcore').setLevel(loglevel) 31 | 32 | 33 | db = sqlite3.connect(DB_FILE) 34 | cur = db.cursor() 35 | 36 | i18n = I18n() 37 | 38 | PATH_REGEX = re.compile(r'^/r/([^/]+)/comments/([a-z0-9]{6,8})(/[^/]+/([a-z0-9]{6,8}))?') 39 | 40 | 41 | def log_error(e): 42 | log.error("Unexpected {}:\n{}".format(e.__class__.__name__, 43 | traceback.format_exc())) 44 | 45 | 46 | def link_url(url): 47 | """ 48 | """ 49 | url = urlparse(url) 50 | return "https://www.reddit.com{}".format(url.path) 51 | 52 | 53 | def escape_title(title): 54 | """ 55 | Escapes special characters in titles. Markdown uses some characters like 56 | * and _ for things such as links to make sure people can't bold titles 57 | and such. 58 | :param title: Title to escape 59 | :return: A escaped title 60 | """ 61 | escaped = "\\*[]^`_~/" 62 | for s in escaped: 63 | title = title.replace(s, "\\" + s) 64 | return title 65 | 66 | 67 | def source_exists(id): 68 | cur.execute("SELECT 1 FROM sources WHERE id=? LIMIT 1", (id,)) 69 | return True if cur.fetchone() else False 70 | 71 | 72 | def link_exists(id): 73 | cur.execute("SELECT 1 FROM links WHERE id=? LIMIT 1", (id,)) 74 | return True if cur.fetchone() else False 75 | 76 | 77 | class RecoverableException(Exception): 78 | pass 79 | 80 | 81 | class SubmissionNotFound(RecoverableException): 82 | def __init__(self, id): 83 | self.id = id 84 | 85 | def __str__(self): 86 | return "Could not find submission {}".format(id) 87 | 88 | 89 | class NotAComment(RecoverableException): 90 | pass 91 | 92 | 93 | RECOVERABLE_EXC = (RecoverableException, 94 | APIException, 95 | ClientException, 96 | PRAWException) 97 | 98 | 99 | class Source: 100 | """ 101 | Comment or thread that has been linked to from somewhere else on reddit. 102 | """ 103 | def __init__(self, reddit, url): 104 | self.reddit = reddit 105 | self.path = urlparse(url.lower()).path 106 | self.id, self.subreddit = self._parse_path() 107 | 108 | self._submission = None 109 | self.author = None 110 | self.title = None 111 | self.reply = None 112 | self.skip = False 113 | self.is_new = True 114 | 115 | def __eq__(self, other): 116 | if isinstance(other, Source): 117 | return self.id == other.id 118 | 119 | return False 120 | 121 | def __hash__(self): 122 | return hash(self.id) 123 | 124 | @property 125 | def submission(self): 126 | if self._submission: 127 | return self._submission 128 | 129 | if self.is_comment: 130 | self._submission = self.reddit.comment(self.base36) 131 | else: 132 | self._submission = self.reddit.submission(self.base36) 133 | 134 | try: 135 | self._submission.name 136 | except PRAWException: 137 | raise SubmissionNotFound(self.id) 138 | 139 | return self._submission 140 | 141 | @property 142 | def base36(self): 143 | return self.id[3:] 144 | 145 | @property 146 | def is_comment(self): 147 | return self.id.startswith('t1') 148 | 149 | @property 150 | def is_post(self): 151 | return self.id.startswith('t3') 152 | 153 | def check_skip(self): 154 | if self.skip: 155 | return True 156 | 157 | cur.execute( 158 | "SELECT * FROM users WHERE name = ? AND skip_source = ? LIMIT 1", 159 | (self.author, True)) 160 | 161 | if cur.fetchone(): 162 | self.skip = True 163 | return True 164 | 165 | cur.execute( 166 | "SELECT * FROM subreddits WHERE name = ? AND skip_source = ? LIMIT 1", 167 | (self.subreddit, True)) 168 | 169 | if cur.fetchone(): 170 | self.skip = True 171 | return True 172 | 173 | if self.submission.archived: # skip if source is archived 174 | self.skip = True 175 | return True 176 | 177 | return False 178 | 179 | def save(self): 180 | if source_exists(self.id): 181 | cur.execute(""" 182 | UPDATE sources SET 183 | reply=?, 184 | subreddit=?, 185 | author=?, 186 | title=?, 187 | skip=? 188 | WHERE id=? 189 | """, (self.reply, self.subreddit, self.author, self.title, 190 | self.skip, self.id)) 191 | else: 192 | cur.execute(""" 193 | INSERT INTO sources (id, reply, subreddit, author, title, skip) 194 | VALUES (?, ?, ?, ?, ?, ?) 195 | """, (self.id, self.reply, self.subreddit, self.author, self.title, 196 | self.skip)) 197 | 198 | # Maybe commit? 199 | db.commit() 200 | 201 | def load(self): 202 | """ 203 | Populate attributes from database and fetch corresponding submission. 204 | """ 205 | cur.execute(""" 206 | SELECT id, reply, subreddit, author, title, skip FROM sources 207 | WHERE id=? LIMIT 1 208 | """, (self.id,)) 209 | 210 | source = cur.fetchone() 211 | 212 | if source: 213 | self.id, self.reply, self.subreddit, self.author, self.title, self.skip = source 214 | self.is_new = False 215 | return # Return early cuz we don't need to perform an api call. 216 | 217 | if not self.author and self.submission.author: 218 | self.author = self.submission.author.name.lower() 219 | else: 220 | self.author = '[deleted]' 221 | 222 | if hasattr(self.submission, 'title'): 223 | self.title = self.submission.title 224 | else: 225 | self.title = '[comment]' 226 | 227 | def _parse_path(self): 228 | # Comments have path /r/sub/comments/xxx/title/xxx/ 229 | # Posts have path /r/sub/comments/xxx/title/ 230 | # TODO: Link when "/r/sub/" is not included in path 231 | match = PATH_REGEX.match(self.path) 232 | 233 | if match: 234 | subreddit, post, _, comment = match.groups() 235 | else: 236 | raise NotAComment("Source {} is not a comment or post.".format(self.path)) 237 | 238 | if comment: 239 | id = "t1_{}".format(comment) 240 | else: 241 | id = "t3_{}".format(post) 242 | 243 | return id, subreddit 244 | 245 | 246 | class Link: 247 | """ 248 | Thread that links to another place on reddit 249 | """ 250 | def __init__(self, submission, source): 251 | self.submission = submission 252 | self.id = submission.name 253 | self.subreddit = submission.subreddit.display_name.lower() 254 | self.skip = False 255 | 256 | if submission.author: 257 | self.author = submission.author.name.lower() 258 | else: 259 | self.author = '[deleted]' 260 | self.skip = True # skip the link if the user has deleted it 261 | 262 | self.title = submission.title 263 | self.permalink = submission.permalink 264 | self.source = source 265 | self.is_new = True 266 | 267 | def check_skip(self): 268 | """ 269 | Checks whether we should skip this link. This uses an all-lowercase 270 | version of the subreddit name, which is different than what is used 271 | in the post (the mixed case name). For example, in the database, 272 | the name would be "TotesMessenger", but the subreddits database table 273 | uses the lowercase name, so that is what will be used. 274 | 275 | The username will also be converted to lowercase and stored as such 276 | as this is only used internally. 277 | :return: A boolean value indicating whether we should skip this link 278 | """ 279 | if self.skip: 280 | return True 281 | 282 | cur.execute( 283 | "SELECT * FROM users WHERE name = ? AND skip_link = ? LIMIT 1", 284 | (self.author, True)) 285 | 286 | if cur.fetchone(): 287 | self.skip = True 288 | return True 289 | 290 | # TODO: convert database 291 | 292 | cur.execute( 293 | "SELECT * FROM subreddits WHERE name = ? AND skip_link = ? LIMIT 1", 294 | (self.subreddit.lower(), True)) 295 | 296 | if cur.fetchone(): 297 | self.skip = True 298 | return True 299 | 300 | return False 301 | 302 | def save(self): 303 | if link_exists(self.id): 304 | cur.execute(""" 305 | UPDATE links SET 306 | source=?, 307 | permalink=?, 308 | subreddit=?, 309 | skip=?, 310 | author=?, 311 | title=? 312 | WHERE id=? 313 | """, (self.source, self.permalink, self.subreddit, self.skip, 314 | self.author, self.title, self.id)) 315 | else: 316 | cur.execute(""" 317 | INSERT INTO links (id, source, permalink, subreddit, skip, author, title) 318 | VALUES (?, ?, ?, ?, ?, ?, ?) 319 | """, (self.id, self.source, self.permalink, self.subreddit, 320 | self.skip, self.author, self.title)) 321 | 322 | # Maybe commit less often? 323 | db.commit() 324 | 325 | def load(self): 326 | """ 327 | Populate attributes from database and fetch corresponding submission. 328 | """ 329 | cur.execute(""" 330 | SELECT id, source, permalink, subreddit, skip, author, title FROM links 331 | WHERE id=? LIMIT 1 332 | """, (self.id,)) 333 | 334 | link = cur.fetchone() 335 | 336 | if link: 337 | self.id, self.source, self.permalink, self.subreddit, self.skip, self.author, self.title = link 338 | self.is_new = False 339 | 340 | 341 | class Notification: 342 | def __init__(self, reddit, source): 343 | self.reddit = reddit 344 | self.source = source 345 | self.id = source.id 346 | self.reply = source.reply 347 | self.links = [] 348 | 349 | def set_language(self): 350 | source_subreddit = self.source.subreddit 351 | 352 | query = cur.execute( 353 | "SELECT language FROM subreddits WHERE name = ?", 354 | (source_subreddit,)) 355 | 356 | lang = query.fetchone() 357 | 358 | if lang is None: 359 | try: 360 | lang = [self.reddit.subreddit(source_subreddit).lang] 361 | except RECOVERABLE_EXC as e: 362 | log_error(e) 363 | lang = [DEFAULT_LANG] # use default if reddit fails 364 | 365 | try: 366 | i18n.setlang(lang[0]) 367 | except TranslationException: 368 | i18n.setlang(DEFAULT_LANG) 369 | 370 | def should_notify(self): 371 | query = cur.execute(""" 372 | SELECT subreddit, title, permalink FROM links 373 | WHERE source=? AND skip=? 374 | ORDER BY subreddit ASC, title ASC 375 | """, (self.id, False)) 376 | 377 | for row in query: 378 | self.links.append(row) 379 | 380 | return any(self.links) 381 | 382 | def post_reply(self): 383 | # Render body. 384 | body = self._render_comment() 385 | 386 | if TEST: 387 | log.debug(""" 388 | ========== COMMENT ============ 389 | Source: {} 390 | {} 391 | ========== /COMMENT =========== 392 | """.format(self.source.path, body)) 393 | return True 394 | 395 | if self.reply: 396 | reply = self.reddit.comment(self.reply[3:]) 397 | reply.edit(body) 398 | return True 399 | 400 | if self.source: 401 | reply = self.source.submission.reply(body) 402 | self.reply = reply.name 403 | 404 | self.source.reply = self.reply 405 | self.source.save() 406 | 407 | return True 408 | 409 | def _render_comment(self): 410 | self.set_language() 411 | 412 | translation_link = TRANSLATION_ERROR_LINK.format(language=i18n.name) 413 | translation_link = translation_link.replace(" ", "%20") 414 | 415 | footer_links = i18n.get("infolink").format(info=INFO_LINK, 416 | contact=CONTACT_LINK, 417 | translation=translation_link, 418 | language=i18n.name) 419 | parts = [] 420 | 421 | parts.append(i18n.get("linkingnotification")) 422 | 423 | cutoff_title = len(self.links) > LINKS_BEFORE_TITLE_CUTOFF 424 | 425 | for subreddit, title, permalink in self.links: 426 | if cutoff_title and len(title) > TITLE_LIMIT: 427 | title = title[:TITLE_LIMIT] + "..." 428 | parts.append("- [/r/{}] [{}]({})".format(subreddit, 429 | escape_title(title), 430 | link_url(permalink))) 431 | 432 | parts.append(" *^({}) {}*".format(i18n.get("votingwarning"), 433 | footer_links)) 434 | 435 | return "\n\n".join(parts) 436 | 437 | 438 | 439 | class Totes: 440 | 441 | def __init__(self, username, password, client_id, client_secret, user_agent, limit=25, snitch_url=None): 442 | self.username = username 443 | self.password = password 444 | self.client_id = client_id 445 | self.client_secret = client_secret 446 | self.user_agent = user_agent 447 | self.limit = limit 448 | self.snitch_url = snitch_url 449 | 450 | self._setup = False 451 | 452 | def run(self): 453 | """ 454 | Comment replies notifying posts and comments that they have been linked 455 | to from somewhere else on reddit. 456 | """ 457 | if not self._setup: 458 | raise Exception("Totes not ready yet!!!") 459 | 460 | log.info("Running.") 461 | 462 | sources = set() 463 | 464 | submissions = list(self.reddit.domain('reddit.com').new(limit=self.limit)) 465 | 466 | 467 | log.debug("Got {} new submissions".format(len(submissions))) 468 | 469 | for submission in submissions: 470 | now = datetime.now(timezone.utc).timestamp() 471 | 472 | if now - submission.created_utc < POST_TIME: 473 | log.debug("Skipping, too new") 474 | continue # skip if our post is less than POST_TIME (2 min) old 475 | 476 | try: 477 | source = Source(self.reddit, submission.url) 478 | source.load() 479 | except RECOVERABLE_EXC as e: 480 | if DEBUG: # give a stacktrace only if debugging 481 | log_error(e) 482 | else: 483 | log.error(str(e)) 484 | db.rollback() 485 | log.debug("Something wrong with source: {}".format( 486 | submission.name)) 487 | continue 488 | 489 | log.debug("Got source: {}".format(submission.name)) 490 | 491 | source.check_skip() 492 | source.save() 493 | 494 | try: 495 | link = Link(submission, source.id) 496 | link.load() 497 | except RECOVERABLE_EXC as e: 498 | log_error(e) 499 | db.rollback() 500 | log.debug("Something wrong with link: {}".format( 501 | submission.name)) 502 | continue 503 | 504 | log.debug("Got link: {}".format(submission.name)) 505 | 506 | link.check_skip() 507 | link.save() 508 | 509 | skip_any = source.skip or link.skip 510 | any_new = source.is_new or link.is_new 511 | 512 | if source.subreddit == "subredditsimulator" and link.subreddit != "subredditsimmeta": 513 | skip_any = True 514 | 515 | log.debug("Skip any: {}".format(skip_any)) 516 | log.debug("Any new: {}".format(any_new)) 517 | 518 | if any_new and not skip_any: 519 | sources.add(source) 520 | 521 | for source in sources: 522 | notification = Notification(self.reddit, source) 523 | 524 | if notification.should_notify(): 525 | try: 526 | notification.post_reply() 527 | except RECOVERABLE_EXC as e: 528 | log_error(e) 529 | db.rollback() 530 | continue 531 | 532 | if self.snitch_url: 533 | try: 534 | log.info("Snitchin'...") 535 | requests.get(self.snitch_url) 536 | except RequestException as e: 537 | log.warn("Couldn't snitch") 538 | log_error(e) 539 | 540 | log.info("Done.") 541 | 542 | def setup(self): 543 | """ 544 | Load settings and perform setup. 545 | """ 546 | self._login() 547 | 548 | self._setup = True 549 | log.info("Totes set up.") 550 | 551 | def quit(self): 552 | """ 553 | Teardown. 554 | """ 555 | log.info("Totes goodbye!") 556 | 557 | def _login(self): 558 | """ 559 | Create reddit session. 560 | """ 561 | self.reddit = praw.Reddit(client_id=self.client_id, 562 | client_secret=self.client_secret, 563 | username=self.username, 564 | password=self.password, 565 | user_agent=self.user_agent) 566 | 567 | log.info("Logged in to reddit.") 568 | 569 | if __name__ == "__main__": 570 | 571 | username = os.environ.get("REDDIT_USERNAME") 572 | password = os.environ.get("REDDIT_PASSWORD") 573 | client_id = os.environ.get("REDDIT_CLIENT_ID") 574 | client_secret = os.environ.get("REDDIT_CLIENT_SECRET") 575 | snitch_url = os.environ.get("SNITCH_URL") 576 | 577 | totes = Totes(username, password, client_id, client_secret, USER_AGENT, LIMIT, snitch_url=snitch_url) 578 | 579 | totes.setup() 580 | 581 | try: 582 | while True: 583 | try: 584 | totes.run() 585 | except RECOVERABLE_EXC as e: 586 | log_error(e) 587 | db.rollback() 588 | time.sleep(WAIT) 589 | except KeyboardInterrupt: 590 | pass 591 | 592 | totes.quit() 593 | db.close() 594 | exit(0) 595 | 596 | --------------------------------------------------------------------------------