Added small tweet functionality to UPHelper

This commit is contained in:
Sebastian Lohff 2012-01-31 17:04:57 +01:00
parent 0332259f60
commit 358cd6a364
1 changed files with 11 additions and 6 deletions

View File

@ -60,20 +60,25 @@ class UPHelper(PHelper):
<9 bits length of payload> <9 bits length of payload>
<32 bit random paket id greater than 0>""" <32 bit random paket id greater than 0>"""
def __init__(self, smallTweets=False):
self.smallTweets = True
@staticmethod @staticmethod
def encode(data): def encode(data):
""" Generate list of packets with a header from data. """ """ Generate list of packets with a header from data. """
packetId = random.randint(1, 2**32) packetId = random.randint(1, 2**32)
fragments = [] fragments = []
while len(data) >= 280:
newData = data[0:280] # quick hack:
if newData[-1] == '\x00' and newData[-2] == '\x00' and len(newData) == 280: plen = self.smallTweets and 276 or 280
fragments.append(data[0:278]) while len(data) >= plen:
data = data[278:] newData = data[0:plen]
if newData[-1] == '\x00' and newData[-2] == '\x00' and len(newData) == plen:
fragments.append(data[0:plen-2])
data = data[plen-2:]
else: else:
fragments.append(newData) fragments.append(newData)
data = data[280:] data = data[plen:]
if len(data) > 0: if len(data) > 0:
fragments.append(data) fragments.append(data)