-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
97 lines (84 loc) · 3.58 KB
/
search.py
File metadata and controls
97 lines (84 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#################################
# Programmer: Kenneth Sinder
# Date Created: 2017-02-10
# Filename: search.py
# Description: Main search script
#################################
import argparse
import os
from dbconnect import ConversationRetrievalService
DESCRIPTION = 'Skype conversation search tool'
class Searcher(object):
"""
Skype message searcher.
Implements `filter([str]) -> str`.
"""
conversation_service = None
messages = []
ci = False
def __init__(self, conversation_service, username, case_insensitive=False):
""" (class, str, [bool]) -> Searcher
Prepares a new Searcher object by constructing the given
`conversation_service` class (which must implement `retrieve([bool])`
to retrieve a list of chat message dicts, and take in a username
in its constructor) and calling `retrieve` on the service instance
to populate messages internally.
"""
self.conversation_service = conversation_service(username)
self.messages = self.conversation_service.retrieve(True)
self.ci = case_insensitive
def filter(self, string=''):
""" ([str]) -> list of dict
Returns a new list of chat messages only containing
the given `string` in the message body. Default behaviour
without `string` parameter is to return all messages unfiltered.
"""
match_function = self._is_ci_match if self.ci else self._is_match
return os.linesep.join([self._convert_message(m) \
for m in self.messages if match_function(string, m)])
def _is_match(self, string: str, msg: dict) -> bool:
""" (str, dict) -> bool
Returns True iff `s` is a substring of the message body
within `msg`.
"""
return string in msg['message']
def _is_ci_match(self, string: str, msg: dict) -> bool:
""" (str, dict) -> bool
Returns true iff `s` is a case-insensitive substring of the
message body within `msg`.
"""
return string.upper() in msg['message'].upper()
def _convert_message(self, message) -> str:
""" (dict) -> str
Convert a `message` represented as a dictionary to an appropriate
string format for printing.
"""
result = "From: {0} ({1})" + os.linesep + "Date: {2}" + \
os.linesep + "Conversation ID: {3}" + os.linesep
result += "Message:" + os.linesep + "{4}" + os.linesep
result = result.format(message['display_name'], message['username'], \
message['local_datetime'], \
message['conversation_id'], \
message['message'])
return result
def main():
""" () -> None
Main program entry point.
"""
# Deal with the two command-line arguments
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('username', metavar='username', type=str,
help='Skype username')
parser.add_argument('query', metavar='query', type=str, help='Search query')
parser.add_argument('-c', '--case-insensitive', dest='case_insensitive', \
help='Do a case-sensitive search', default=False, action='store_true')
args = parser.parse_args()
# Create Searcher object
searcher = Searcher(ConversationRetrievalService, args.username, args.case_insensitive)
# Filter and print the result
try:
print(searcher.filter(args.query))
except UnicodeEncodeError:
print(r"You must set up Unicode in Command Prompt. Please see README.md for details.")
if __name__ == '__main__':
main()