python tweetstorm的简单脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python tweetstorm的简单脚本相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*-
class Tweetstorm(object):
MAX_TWEET_LENGTH = 134 # + 6 chars from initial string NN/NN = 140
TWEET_MAXIMUM_PERCENTAGE_COMPLETE_SENTENCE = 0.6
_separations_points = ['.', '?', '!', ':']
_tweets = []
def __init__(self, text):
self.text = text.replace('\n', '')
self._build()
def __str__(self):
return '\n'.join(self._tweets)
def get_tweets(self):
return self._tweets
def _find_max_segment_from_dots(self, text_segment):
segment = None, None
biggest_size = 0
for point in self._separations_points:
result = text_segment.rfind(point)
if result is not -1 and len(text_segment[:result]) > biggest_size:
segment = text_segment[:result + 1].strip(), len(text_segment[:result + 1])
biggest_size = segment[1]
return segment
def _find_max_segment_without_dots(self, text_segment):
segment = ''
for seg in text_segment.split(' '):
if len(segment) + len(seg) > self.MAX_TWEET_LENGTH - 3:
break
segment += ' ' + seg
return segment.strip() + '...', len(segment)
def _build(self):
initial_position = 0
end_position = self.MAX_TWEET_LENGTH
while initial_position < len(self.text):
segment = self.text[initial_position:end_position]
tweet_text, tweet_length = self._find_max_segment_from_dots(segment)
if tweet_text is None or \
tweet_length / self.MAX_TWEET_LENGTH <= self.TWEET_MAXIMUM_PERCENTAGE_COMPLETE_SENTENCE:
tweet_text, tweet_length = self._find_max_segment_without_dots(segment)
self._tweets.append(tweet_text)
initial_position += tweet_length
end_position = initial_position + self.MAX_TWEET_LENGTH
self._set_prefix()
return self._tweets
def _set_prefix(self):
for idx, tweet in enumerate(self._tweets):
self._tweets[idx] = '{}/{} {}'.format(idx + 1, len(self._tweets), tweet)
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print("Tweetstorm generator need an file as argument")
sys.exit(1)
try:
with open(sys.argv[1], 'r') as f:
text = f.read()
except Exception as e:
print(e)
sys.exit(1)
print(Tweetstorm(text))
以上是关于python tweetstorm的简单脚本的主要内容,如果未能解决你的问题,请参考以下文章