Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ?censor and ?safe params #270

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/__pycache__/
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Sorting

Don't bother sorting the file. Every now and then I'll sort and clean it up.
Don't bother sorting the files. Every now and then I'll sort and clean up.

## License

Expand Down
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Some interesting usage for that can be:
git config --global alias.yolo '!git commit -m "$(curl -s https://whatthecommit.com/index.txt)"'
```

## Make it safe for work

https://whatthecommit.com/?safe filters out any unsafe or swearing messages
https://whatthecommit.com/?censor censors them instead, and filters messages that wouldn't be funny if censored
https://whatthecommit.com/?censor=* censors using custom pattern

Or use one of the following VSCode Extensions:

- [WhatTheCommit](https://marketplace.visualstudio.com/items?itemName=Gaardsholt.vscode-whatthecommit)
Expand Down
90 changes: 19 additions & 71 deletions commit.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,35 @@
import os
import sys
import random
import re
import json
import signal
from typing import Dict, List

from hashlib import md5

import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.escape import xhtml_unescape
from tornado.options import define, options

define("port", default=5000, help="run on the given port", type=int)

names = ['Nick', 'Steve', 'Andy', 'Qi', 'Fanny', 'Sarah', 'Cord', 'Todd',
'Chris', 'Pasha', 'Gabe', 'Tony', 'Jason', 'Randal', 'Ali', 'Kim',
'Rainer', 'Guillaume', 'Kelan', 'David', 'John', 'Stephen', 'Tom', 'Steven',
'Jen', 'Marcus', 'Edy', 'Rachel', 'Ethan', 'Dan', 'Darren', 'Greg']

humans_file = os.path.join(os.path.dirname(__file__), 'static', 'humans.txt')
messages_file = os.path.join(os.path.dirname(__file__), 'commit_messages.txt')
messages: Dict[str, str] = {}

# Create a hash table of all commit messages
with open(messages_file, 'r', encoding='utf-8') as messages_input:
for line in messages_input.readlines():
messages[md5(line.encode('utf-8')).hexdigest()] = line

names: List[str] = []

with open(humans_file, 'r', encoding='utf-8') as humans_input:
humans_content = humans_input.read()
for line in humans_content.split("\n"):
if "Name:" in line:
data = line[6:].rstrip()
if data.find("github:") == 0:
names.append(data[7:])
else:
names.append(data.split(" ")[0])

num_re = re.compile(r"XNUM([0-9,]*)X")

def fill_line(message):
message = message.replace('XNAMEX', random.choice(names))
message = message.replace('XUPPERNAMEX', random.choice(names).upper())
message = message.replace('XLOWERNAMEX', random.choice(names).lower())

nums = num_re.findall(message)

while nums:
start = 1
end = 999
value = nums.pop(0) or str(end)
if "," in value:
position = value.index(",")
if position == 0: # XNUM,5X
end = int(value[1:])
elif position == len(value) - 1: # XNUM5,X
start = int(value[:position])
else: # XNUM1,5X
start = int(value[:position])
end = int(value[position+1:])
else:
end = int(value)
if start > end:
end = start * 2

randint = random.randint(start, end)
message = num_re.sub(str(randint), message, count=1)
import messages

return message
define("port", default=5000, help="run on the given port", type=int)

class MainHandler(tornado.web.RequestHandler):
def get(self, message_hash=None):
if not message_hash:
message_hash = random.choice(list(messages.keys()))
elif message_hash not in messages:
raise tornado.web.HTTPError(404)
safe_only = self.get_argument("safe", default=False) != False
censor = self.get_argument("censor", default=False)
if censor == "":
censor = True

message = fill_line(messages[message_hash])
found_message = messages.find_by_md5(message_hash, censor=censor)

self.output_message(message, message_hash)
if message_hash and not found_message:
raise tornado.web.HTTPError(404)

if found_message:
self.output_message(found_message, message_hash)
else:
message, generated_message_hash = (
messages.generate(safe_only=safe_only, censor=censor))
self.output_message(message, generated_message_hash)

def output_message(self, message, message_hash):
self.set_header('X-Message-Hash', message_hash)
Expand Down Expand Up @@ -134,6 +80,8 @@ def try_exit(self):
tornado.options.parse_command_line()
signal.signal(signal.SIGINT, application.signal_handler)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(os.environ.get("PORT", 5000))
port = os.environ.get("PORT", 5000)
print("ready for requests (on port %s)" % (port))
http_server.listen(port)
tornado.ioloop.PeriodicCallback(application.try_exit, 100).start()
tornado.ioloop.IOLoop.instance().start()
58 changes: 58 additions & 0 deletions commit_messages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
these are the source text files from which all the commit messages
are generated

- put commit messages with swear words in censorable.txt, this makes
them funny by using [grawlixes](https://en.wikipedia.org/wiki/Grawlix)

- put other nsfw in unsafe, because they cannot be made funny and safe
by using grawlixes

- put safe for work messages in safe.txt.

## Script for filtering of swear words

This was used to create the initial list of unsafe words.

```py
BAD_WORDS = [
"shit",
"piss",
"fuck",
"cunt",
"cocksucker",
"motherfucker",
"tits",

"cock",
"fucker",

"fart",
"turd",
"twat",

"dicksucker",
"fucking",

"sex",
"sexy",
]

safe_f = open("commit_messages/safe.txt", "w")
unsafe_f = open("commit_messages/unsafe.txt", "w")

with open("./commit_messages.txt") as original_f:
Comment on lines +40 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, for the future, this can be done in one statement as:

with (
    open("commit_messages/safe.txt", "w") as safe_f,
    open("commit_messages/unsafe.txt", "w") as unsafe_f,
    open("./commit_messages.txt") as original_f,
):

...And then you don't need any explicit .close()es.

(Python 3.10+ is required to surround the open()s with ()s, otherwise they all need to be on one line.)

for line in original_f:
bad = False

for bad_word in BAD_WORDS:
if bad_word in line.lower():
bad = True

if bad:
unsafe_f.write(line)
else:
safe_f.write(line)

safe_f.close()
unsafe_f.close()
```
44 changes: 44 additions & 0 deletions commit_messages/censorable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"Get that shit outta my master."
Code was clean until manager requested to fuck it up
Derp search/replace fuckup
Either Hot Shit or Total Bollocks
FUCKING XUPPERNAMEX
Fixed some shit
Fixed the fuck out of #XNUMX!
Fuck it, YOLO!
Fucking egotistical bastard. adds expandtab to vimrc
Fucking submodule bull shit
Fucking templates.
Herping the fucking derp right here and now.
I don't get paid enough for this shit.
I hate this fucking language.
I'm too old for this shit!
It fucking compiles \:D/
Merge pull request #67 from Lazersmoke/fix-andys-shit Fix andys shit
My boss forced me to build this feature... Pure shit.
REALLY FUCKING FIXED
Revert "fuckup".
SHIT ===> GOLD
SOAP is a piece of shit
Shit code!
Some shit.
WHO THE FUCK CAME UP WITH MAKE?
Why The Fuck?
a lot of shit
clarify further the brokenness of C++. why the fuck are we using C++?
fix some fucking errors
fixed shit that havent been fixed in last commit
fixing project shit
fuckup.
holy shit it's functional
if you're not using et, fuck off
include shit
refuckulated the carbonator
someday I gonna kill someone for this shit...
this is how we generate our shit.
I don't give a damn 'bout my reputation
arrgghh... damn this thing for not working.
download half the damn internet to parse a pdf
fix that damn sign!!!
hopefully going to get a successful build got damn it
still trying to render a damn cube
Loading