def parse_twitter(raw): MAX_TWEETS = 30 count = 0 response = [] for tweet in raw['statuses']: try: # only return <30 english tweets with no sensitive content if (tweet['lang'] == 'en') and \ count < MAX_TWEETS and tweet['possibly_sensitive'] == False: count += 1 selection = [] selection.append({'date': tweet['created_at']}) text = tweet['text'] if sensitive_text(text) == True: # sensitive word -> exclude continue else: selection.append({'text': text}) hashtags = [] for tag in tweet['entities']['hashtags']: hashtags.append(tag['text']) selection.append({'hashtags': hashtags}) try: image_url = tweet['entities']['media'][0]['media_url'] selection.append({'image_url': image_url}) except: pass # no image response.append(selection) except: pass #possible sensitive content return response
def test_sensitive_text(): input_text = "Car, is; better*than\nugly#car#bus1" assert sensitive_text(input_text) == False # "test_sensitive_text test 1" input_text = "sadfsdaf, is; betsdfasfter*than\sex#car#bus2" assert sensitive_text(input_text) == True # "test_sensitive_text test 2" input_text = "asdfasdf, is; bettasdfer*than.ass#car#bus3" assert sensitive_text(input_text) == True # "test_sensitive_text test 3" input_text = "asdfdfs, is; betasdfter*than.any#ass#bus4" assert sensitive_text(input_text) == True # "test_sensitive_text test 4" input_text = "sfasfsdfdsaf, is; besdftter*than.any# ass#bus5" assert sensitive_text(input_text) == True # "test_sensitive_text test 5" input_text = "Be, is; better*thsfan.any# ass #bus6" assert sensitive_text(input_text) == True # "test_sensitive_text test 6" input_text = "Basdf, is; ?ass?bettsadfer*than.any# ok #bus7" assert sensitive_text(input_text) == True # "test_sensitive_text test 7" input_text = "Basdfsdaf, is; @ass?besafdtter*than.any# ok #bus8" assert sensitive_text(input_text) == True # "test_sensitive_text test 8" input_text = "# #" assert sensitive_text(input_text) == False # "test_sensitive_text test 9" input_text = "Basdfsdaf, is; @mas?besafdtter*massage.any# ok #bus8" assert sensitive_text(input_text) == True # "test_sensitive_text test 8"
def test_sensitive_text(): input_text = 'Car, is; better*than\nugly#car#bus1' assert(sensitive_text(input_text) == False) # "test_sensitive_text test 1" input_text = 'sadfsdaf, is; betsdfasfter*than\sex#car#bus2' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 2" input_text = 'asdfasdf, is; bettasdfer*than.ass#car#bus3' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 3" input_text = 'asdfdfs, is; betasdfter*than.any#ass#bus4' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 4" input_text = 'sfasfsdfdsaf, is; besdftter*than.any# ass#bus5' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 5" input_text = 'Be, is; better*thsfan.any# ass #bus6' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 6" input_text = 'Basdf, is; ?ass?bettsadfer*than.any# ok #bus7' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 7" input_text = 'Basdfsdaf, is; @ass?besafdtter*than.any# ok #bus8' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 8" input_text = '# #' assert(sensitive_text(input_text) == False) # "test_sensitive_text test 9" input_text = 'Basdfsdaf, is; @mas?besafdtter*massage.any# ok #bus8' assert(sensitive_text(input_text) == True) # "test_sensitive_text test 8"