Skip to content

Commit

Permalink
New option --directory to sort per-user
Browse files Browse the repository at this point in the history
This fixes #39.
  • Loading branch information
nomeata committed Nov 17, 2015
1 parent 0b6a4e6 commit e6a449e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.textile
Expand Up @@ -28,6 +28,9 @@ h3. "Thunderbird":https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=

You can add the @-m@ or @-t@ flag to get the output as a possibly-MBOX-compatible file rather than the default plaintext format, but the feature hasn't been thoroughly tested. Put the file in a @Local@ folder, and hopefully Thunderbird (or pine/alpine/realpine) can read/process it from there. Please report back if it works or if there are any issues!

h3. Directory output

By specifying @--directory@ instead of @--filename@, the chats will be sorted into files named @username.txt@ according to your conversation partner.

h3. Debugging

Expand Down
44 changes: 38 additions & 6 deletions src/arrow_fetcher.py
Expand Up @@ -11,6 +11,8 @@
import urllib
import urllib2
import logging
import os
import os.path

from bs4 import BeautifulSoup, NavigableString

Expand Down Expand Up @@ -172,6 +174,29 @@ def write_messages(self, file_name):
f.write(unicode(message))
f.close()

def write_directory(self, directory):
if not os.path.exists(directory):
os.makedirs(directory)

open_files = {}

self.messages.sort(key = lambda message: message.timestamp) # sort by time
for message in self.messages:
if message.recipient == self.username:
other = message.sender
else:
other = message.recipient

if other not in open_files:
filename = "%s/%s.txt" % (directory, other)
logging.debug("Writing %s" % filename)
open_files[other] = codecs.open(filename, encoding='utf-8', mode='w')

open_files[other].write(unicode(message))

for f in open_files.values():
f.close

def _fetch_thread(self, thread_url):
message_list = []
logging.info("Fetching thread: " + self.secure_base_url + thread_url)
Expand Down Expand Up @@ -267,9 +292,10 @@ def _strip_tags(self, html, invalid_tags=['em', 'a', 'span', 'strong', 'div', 'p
return soup.encode_contents().decode('UTF-8')

class OkcupidState:
def __init__(self, username, filename, mbox, debug, indexfile):
def __init__(self, username, filename, directory, mbox, debug, indexfile):
self.username = username
self.filename = filename
self.directory = directory
self.mbox = mbox
self.debug = debug
self.indexfile = indexfile
Expand All @@ -292,9 +318,13 @@ def fetch(self):
arrow_fetcher.dedupe_threads()
try:
arrow_fetcher.fetch_threads()
arrow_fetcher.write_messages(self.filename)
if self.filename:
arrow_fetcher.write_messages(self.filename)
if self.directory:
arrow_fetcher.write_directory(self.directory)
except KeyboardInterrupt:
if self.debug: # Write progress so far to the output file if we're debugging
if self.debug and self.filename:
# Write progress so far to the output file if we're debugging
arrow_fetcher.write_messages(self.filename)
raise KeyboardInterrupt

Expand Down Expand Up @@ -326,6 +356,8 @@ def main():
help="a link from an OkCupid email, which contains your login credentials; use instead of a password")
parser.add_option("-f", "--filename", dest="filename",
help="the file to which you want to write the data")
parser.add_option("", "--directory", dest="directory",
help="the directory to which you want to write the data")
parser.add_option("-m", "--mbox", dest="mbox",
help="format output as MBOX rather than as plaintext",
action='store_const', const=True, default=False)
Expand Down Expand Up @@ -359,13 +391,13 @@ def main():
if options.autologin and options.password:
logging.error("Don't specify both autologin and password")
options_ok = False
if not options.filename:
logging.error("Please specify the destination file with either '-f' or '--filename'")
if not options.filename and not options.directory:
logging.error("Please specify the destination file or directory with either '-f', '--filename' or '--directory'")
options_ok = False
if not options_ok:
logging.error("See 'okcmd --help' for all options.")
else:
state = OkcupidState(options.username, options.filename, options.mbox, options.debug, options.indexfile)
state = OkcupidState(options.username, options.filename, options.directory, options.mbox, options.debug, options.indexfile)
if options.indexfile:
state.use_indexfile(options.indexfile)
elif options.username and options.password:
Expand Down

0 comments on commit e6a449e

Please sign in to comment.