forked from indymedia/epicyon
flake8 format
parent
654c5104d5
commit
a9ee0fc1b2
58
filters.py
58
filters.py
|
@ -1,43 +1,48 @@
|
||||||
__filename__="filters.py"
|
__filename__ = "filters.py"
|
||||||
__author__="Bob Mottram"
|
__author__ = "Bob Mottram"
|
||||||
__license__="AGPL3+"
|
__license__ = "AGPL3+"
|
||||||
__version__="1.1.0"
|
__version__ = "1.1.0"
|
||||||
__maintainer__="Bob Mottram"
|
__maintainer__ = "Bob Mottram"
|
||||||
__email__="bob@freedombone.net"
|
__email__ = "bob@freedombone.net"
|
||||||
__status__="Production"
|
__status__ = "Production"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def addFilter(baseDir: str,nickname: str,domain: str,words: str) -> bool:
|
|
||||||
|
def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
|
||||||
"""Adds a filter for particular words within the content of a incoming posts
|
"""Adds a filter for particular words within the content of a incoming posts
|
||||||
"""
|
"""
|
||||||
filtersFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/filters.txt'
|
filtersFilename = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/filters.txt'
|
||||||
if os.path.isfile(filtersFilename):
|
if os.path.isfile(filtersFilename):
|
||||||
if words in open(filtersFilename).read():
|
if words in open(filtersFilename).read():
|
||||||
return False
|
return False
|
||||||
filtersFile=open(filtersFilename, "a+")
|
filtersFile = open(filtersFilename, "a+")
|
||||||
filtersFile.write(words+'\n')
|
filtersFile.write(words + '\n')
|
||||||
filtersFile.close()
|
filtersFile.close()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def removeFilter(baseDir: str,nickname: str,domain: str, \
|
|
||||||
|
def removeFilter(baseDir: str, nickname: str, domain: str,
|
||||||
words: str) -> bool:
|
words: str) -> bool:
|
||||||
"""Removes a word filter
|
"""Removes a word filter
|
||||||
"""
|
"""
|
||||||
filtersFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/filters.txt'
|
filtersFilename = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/filters.txt'
|
||||||
if os.path.isfile(filtersFilename):
|
if os.path.isfile(filtersFilename):
|
||||||
if words in open(filtersFilename).read():
|
if words in open(filtersFilename).read():
|
||||||
with open(filtersFilename, 'r') as fp:
|
with open(filtersFilename, 'r') as fp:
|
||||||
with open(filtersFilename+'.new', 'w') as fpnew:
|
with open(filtersFilename + '.new', 'w') as fpnew:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
line=line.replace('\n','')
|
line = line.replace('\n', '')
|
||||||
if line!=words:
|
if line != words:
|
||||||
fpnew.write(line+'\n')
|
fpnew.write(line + '\n')
|
||||||
if os.path.isfile(filtersFilename+'.new'):
|
if os.path.isfile(filtersFilename + '.new'):
|
||||||
os.rename(filtersFilename+'.new',filtersFilename)
|
os.rename(filtersFilename + '.new', filtersFilename)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def isTwitterPost(content: str) -> bool:
|
def isTwitterPost(content: str) -> bool:
|
||||||
"""Returns true if the given post content is a retweet or twitter crosspost
|
"""Returns true if the given post content is a retweet or twitter crosspost
|
||||||
"""
|
"""
|
||||||
|
@ -47,30 +52,33 @@ def isTwitterPost(content: str) -> bool:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def isFiltered(baseDir: str,nickname: str,domain: str,content: str) -> bool:
|
|
||||||
|
def isFiltered(baseDir: str, nickname: str, domain: str, content: str) -> bool:
|
||||||
"""Should the given content be filtered out?
|
"""Should the given content be filtered out?
|
||||||
This is a simple type of filter which just matches words, not a regex
|
This is a simple type of filter which just matches words, not a regex
|
||||||
You can add individual words or use word1+word2 to indicate that two
|
You can add individual words or use word1+word2 to indicate that two
|
||||||
words must be present although not necessarily adjacent
|
words must be present although not necessarily adjacent
|
||||||
"""
|
"""
|
||||||
# optionally remove retweets
|
# optionally remove retweets
|
||||||
removeTwitter=baseDir+'/accounts/'+nickname+'@'+domain+'/.removeTwitter'
|
removeTwitter = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/.removeTwitter'
|
||||||
if os.path.isfile(removeTwitter):
|
if os.path.isfile(removeTwitter):
|
||||||
if isTwitterPost(content):
|
if isTwitterPost(content):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
filtersFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/filters.txt'
|
filtersFilename = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/filters.txt'
|
||||||
if os.path.isfile(filtersFilename):
|
if os.path.isfile(filtersFilename):
|
||||||
with open(filtersFilename, 'r') as fp:
|
with open(filtersFilename, 'r') as fp:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
filterStr=line.replace('\n','')
|
filterStr = line.replace('\n', '')
|
||||||
if '+' not in filterStr:
|
if '+' not in filterStr:
|
||||||
if filterStr in content:
|
if filterStr in content:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
filterWords=filterStr.replace('"','').split('+')
|
filterWords = filterStr.replace('"', '').split('+')
|
||||||
for word in filterWords:
|
for word in filterWords:
|
||||||
if not word in content:
|
if word not in content:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in New Issue