truncate.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import math
  2. # important variables
  3. # character length per tweet
  4. oneOnes = 140-6
  5. tenOnes = 140-7
  6. tenTens = 140-8
  7. hunOnes = 140-8
  8. hunTens = 140-9
  9. hunHuns = 140-10
  10. # max length for each bracket
  11. oneslen = 9*(oneOnes)
  12. tenslen = 9*(tenOnes)+90*(tenTens)
  13. hunslen = 9*(hunOnes)+90*(hunTens)+900*(hunHuns)
  14. # finds total number of tweets needed
  15. def totaltweets(tweetlen):
  16. tweetlen = float(tweetlen)
  17. if (tweetlen <= 1):
  18. numtweets = 1
  19. elif (tweetlen <= oneslen): # from 1 - 9 individual tweets
  20. div = float(tweetlen)/oneOnes
  21. numtweets = math.ceil(div)
  22. print("numtweets "+str(numtweets))
  23. elif (tweetlen <= tenslen): # from 10 - 99 individual tweets
  24. div = (float(tweetlen)-9*tenOnes)/tenTens
  25. numtweets = math.ceil(div)+9
  26. elif (tweetlen <= hunslen): # from 100 - 999 individual tweets
  27. div = (float(tweetlen)-9*hunOnes-90*hunTens)/hunHuns
  28. numtweets = math.ceil(div)+99
  29. else:
  30. print('stfu you don\'t have that much to say')
  31. numtweets = 0
  32. numtweets = int(numtweets)
  33. return numtweets
  34. # puts the tweets into blocks and outputs them
  35. def tweetblock(numtweets, tweet, tweetlen):
  36. # how much to skip per slice
  37. if (tweetlen <= oneslen):
  38. jump = oneOnes
  39. elif (tweetlen <= tenslen):
  40. jump = tenOnes
  41. elif (tweetlen <= hunslen):
  42. jump = hunOnes
  43. index = 1 # number of tweet
  44. start = 0 # start of slice
  45. end = jump # slice length
  46. for i in range(0, numtweets):
  47. print(tweet[start:end]+" ("+str(index)+"/"+str(numtweets)+ ")")
  48. index+=1
  49. if (index==10 or index==100):
  50. start+=1 # takes into account that this would have been printed, in the change with jump
  51. jump-=1
  52. start+=jump
  53. end+=jump
  54. #print ("start" + str(start)+" end "+str(end)+ " index " + str(index))
  55. return 0
  56. # main I guess lol
  57. def main():
  58. # gets some character thing
  59. tweet = raw_input('Please enter a tweet ')
  60. tweetlen = len(tweet)
  61. print("total characters: "+str(tweetlen))
  62. numtweets = totaltweets(tweetlen)
  63. print(numtweets)
  64. tweetblock(numtweets, tweet, tweetlen)
  65. if __name__ == "__main__":
  66. main()