'''Tweethread core module'''


from os import getenv
import sys
import tweepy
import nltk
import math

def get_environment_variable(environment_variable_name):
    ''' Returns an environment variable's value or raises an error'''
    key = getenv(environment_variable_name)
    if not key:
        raise LookupError('Please set the {} environment variable'
                          .format(environment_variable_name))
    return key


def get_tweepy_api():
    '''Returns a tweepy api object with the authentication set by environment variables'''
    consumer_key = get_environment_variable('TWITTER_CONSUMER_KEY')
    consumer_secret = get_environment_variable('TWITTER_CONSUMER_SECRET')
    access_token = get_environment_variable('TWITTER_ACCESS_TOKEN')
    access_token_secret = get_environment_variable('TWITTER_ACCESS_TOKEN_SECRET')

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    print(api.me().name)
    return tweepy.API(auth)


def split_seq(seq, size):
        newseq = []
        splitsize = 1.0/size*len(seq)
        for i in range(size):
                newseq.append(seq[int(round(i*splitsize)):int(round((i+1)*splitsize))])
        return newseq


def get_number_of_chunks(i, f):
	x=math.ceil(len(i)/float(f))
	return int(x)

def get_sentences(p):
	if not p:
		raise ValueError("Parameter `p` is empty")
	try:
		nltk.data.find('tokenizers/punkt/english.pickle')
	except LookupError:
		nltk.download('punkt')
	sentences = nltk.tokenize.sent_tokenize(p)
	sentencearray=[]
	while len(sentences) != 0:
		x=sentences.pop(0)
		if len(x) > 136:
			sentencearray.append(x)
		elif len(sentences) > 1:
			if len(x + sentences[0] + ' ') > 136:
				sentencearray.append(x)			
			else:
				if sentences == '.':
					sentences[0]=x + sentences[0]
				else:
					sentences[0]=x + ' ' + sentences[0]
		else:
			pass 
	return sentencearray

def break_into_tweets(i):
	tweets=[]
	x=get_sentences(i)
	index=1
	for each in x:
		if len(each) > 136:
			sentence=each.split(" ")
			y=split_seq(sentence,get_number_of_chunks(each,136))
			for every in y:
				tweets.append(str(index) + '/ ' + ' '.join(every))
				index = index + 1
		else:
			tweets.append(str(index) + '/ ' + each)
			index = index + 1

	return tweets


def main():
    '''tweetstorm's main function, to be run in the command line'''
    mode = sys.argv[1]
    filename = sys.argv[2]
    try:
        with open(filename) as file:
            text = file.read()
    except FileNotFoundError:
        text = filename

    tweets = break_into_tweets(text)
    api = get_tweepy_api()
    index=1
    tweetid=0
    for tweet in tweets:
        if mode == 'tweet':
	    if index != 1:
               api.update_status(tweet,str(tweetid))
               index=index + 1
            else:
               tweet=api.update_status(tweet)
               tweetid=tweet.id_str
               index=index + 1
        elif mode == 'parse':
            print tweet
        else:
            if index == 1:
              print "Tweethread doesn't recognize a [[" + mode + "]] mode...please choose either 'tweet' or 'parse'"
              index = index + 1
            else:
              pass

@@ -2167,36 +2167,16 @@
 .append(
-str(index) + '/ ' + 
 ' '.join
@@ -2235,28 +2235,8 @@
 end(
-str(index) + '/ ' + 
 each
@@ -2364,53 +2364,422 @@
     
-mode = sys.argv%5B1%5D%0A    filename = sys.argv%5B2%5D
+numbering_scheme='default'%0A    mode = sys.argv%5B1%5D%0A    if sys.argv%5B2%5D == 'nonum':%0A        print %22Tweet numbering is turned off%22%0A        numbering_scheme='none'%0A        filename = sys.argv%5B3%5D%0A    else: %0A        if len(sys.argv) %3E 3:%0A           print %22Tweethread has no %5B%5B%22 + sys.argv%5B2%5D + '%5D%5D option. Using default numbering method.'%0A           filename = sys.argv%5B3%5D%0A        else:%0A           filename = sys.argv%5B2%5D 
 %0A   
@@ -2906,17 +2906,16 @@
 ilename%0A
-%0A
     twee
@@ -2943,16 +2943,243 @@
 s(text)%0A
+    index = 1%0A    numbered_tweets=%5B%5D%0A    if numbering_scheme == 'default':%0A        for tweet in tweets:%0A            numbered_tweets.append(str(index) + '/' + tweet)%0A            index = index + 1 %0A        tweets=numbered_tweets%0A
     api 

Update core.py