2014-10-27 16:29:04 -06:00
# -*- coding: utf-8 -*-
2015-04-03 16:57:08 -06:00
import platform
system = platform . system ( )
2014-10-27 16:29:04 -06:00
import utils
import re
import htmlentitydefs
import time
import output
2015-01-05 06:05:31 -06:00
import languageHandler
import arrow
2015-03-17 03:39:59 -06:00
import logging
2015-06-27 00:38:09 -05:00
import config
2015-09-29 08:38:05 -05:00
from long_tweets import twishort , tweets
2015-03-17 03:39:59 -06:00
log = logging . getLogger ( " compose " )
2014-10-27 16:29:04 -06:00
def StripChars ( s ) :
""" Converts any html entities in s to their unicode-decoded equivalents and returns a string. """
entity_re = re . compile ( r " &(# \ d+| \ w+); " )
def matchFunc ( match ) :
""" Nested function to handle a match object.
If we match & blah ; and it ' s not found, &blah; will be returned.
if we match #\d+, unichr(digits) will be returned.
Else , a unicode string will be returned . """
if match . group ( 1 ) . startswith ( ' # ' ) : return unichr ( int ( match . group ( 1 ) [ 1 : ] ) )
replacement = htmlentitydefs . entitydefs . get ( match . group ( 1 ) , " & %s ; " % match . group ( 1 ) )
return replacement . decode ( ' iso-8859-1 ' )
return unicode ( entity_re . sub ( matchFunc , s ) )
chars = " abcdefghijklmnopqrstuvwxyz "
2014-11-12 20:41:29 -06:00
def compose_tweet ( tweet , db , relative_times ) :
2014-10-27 16:29:04 -06:00
""" It receives a tweet and returns a list with the user, text for the tweet or message, date and the client where user is. """
2015-04-03 16:57:08 -06:00
if system == " Windows " :
original_date = arrow . get ( tweet [ " created_at " ] , " ddd MMM DD H:m:s Z YYYY " , locale = " en " )
if relative_times == True :
ts = original_date . humanize ( locale = languageHandler . getLanguage ( ) )
else :
ts = original_date . replace ( seconds = db [ " utc_offset " ] ) . format ( _ ( u " dddd, MMMM D, YYYY H:m:s " ) , locale = languageHandler . getLanguage ( ) )
2014-10-27 16:29:04 -06:00
else :
2015-04-03 16:57:08 -06:00
ts = tweet [ " created_at " ]
2016-04-28 13:54:06 -05:00
if tweet . has_key ( " message " ) :
value = " message "
else :
value = " text "
text = StripChars ( tweet [ value ] )
2014-10-27 16:29:04 -06:00
if tweet . has_key ( " sender " ) :
source = " DM "
2014-11-12 20:41:29 -06:00
if db [ " user_name " ] == tweet [ " sender " ] [ " screen_name " ] : user = _ ( u " Dm to %s " ) % ( tweet [ " recipient " ] [ " name " ] , )
2014-10-27 16:29:04 -06:00
else : user = tweet [ " sender " ] [ " name " ]
elif tweet . has_key ( " user " ) :
user = tweet [ " user " ] [ " name " ]
2015-01-05 06:05:31 -06:00
source = re . sub ( r " (?s)<.*?> " , " " , tweet [ " source " ] )
2016-06-25 19:48:21 -05:00
try : text = " rt @ %s : %s " % ( tweet [ " retweeted_status " ] [ " user " ] [ " screen_name " ] , StripChars ( tweet [ value ] ) )
2016-04-28 13:54:06 -05:00
except KeyError : pass
#text = "%s" % (StripChars(tweet["text"]))
2014-10-27 16:29:04 -06:00
if text [ - 1 ] in chars : text = text + " . "
urls = utils . find_urls_in_text ( text )
for url in range ( 0 , len ( urls ) ) :
try : text = text . replace ( urls [ url ] , tweet [ " entities " ] [ " urls " ] [ url ] [ " expanded_url " ] )
except IndexError : pass
2016-04-28 13:54:06 -05:00
if config . app [ ' app-settings ' ] [ ' handle_longtweets ' ] : pass
# return [user+", ", text, ts+", ", source]
return [ user + " , " , text , ts + " , " , source ]
2014-10-27 16:29:04 -06:00
2015-09-29 08:38:05 -05:00
def compose_quoted_tweet ( quoted_tweet , original_tweet ) :
""" It receives a tweet and returns a list with the user, text for the tweet or message, date and the client where user is. """
text = StripChars ( quoted_tweet [ " text " ] )
quoting_user = quoted_tweet [ " user " ] [ " name " ]
source = re . sub ( r " (?s)<.*?> " , " " , quoted_tweet [ " source " ] )
try : text = " rt @ %s : %s " % ( quoted_tweet [ " retweeted_status " ] [ " user " ] [ " screen_name " ] , StripChars ( quoted_tweet [ " retweeted_status " ] [ " text " ] ) )
except KeyError : text = " %s " % ( StripChars ( quoted_tweet [ " text " ] ) )
if text [ - 1 ] in chars : text = text + " . "
original_user = original_tweet [ " user " ] [ " screen_name " ]
original_text = StripChars ( original_tweet [ " text " ] )
try : original_text = " rt @ %s : %s " % ( original_tweet [ " retweeted_status " ] [ " user " ] [ " screen_name " ] , StripChars ( original_tweet [ " retweeted_status " ] [ " text " ] ) )
except KeyError : original_text = " %s " % ( StripChars ( original_tweet [ " text " ] ) )
2015-09-29 09:38:39 -05:00
quoted_tweet [ " message " ] = _ ( u " {0} . Quoted tweet from @ {1} : {2} " ) . format ( quoted_tweet [ " text " ] , original_user , original_text )
2015-09-29 08:38:05 -05:00
quoted_tweet = tweets . clear_url ( quoted_tweet )
return quoted_tweet
2015-01-05 06:05:31 -06:00
def compose_followers_list ( tweet , db , relative_times = True ) :
2015-04-03 16:57:08 -06:00
if system == " Windows " :
original_date = arrow . get ( tweet [ " created_at " ] , " ddd MMM D H:m:s Z YYYY " , locale = " en " )
if relative_times == True :
ts = original_date . humanize ( locale = languageHandler . getLanguage ( ) )
else :
ts = original_date . replace ( seconds = db [ " utc_offset " ] ) . format ( _ ( u " dddd, MMMM D, YYYY H:m:s " ) , locale = languageHandler . getLanguage ( ) )
2014-10-27 16:29:04 -06:00
else :
2015-04-03 16:57:08 -06:00
ts = tweet [ " created_at " ]
2014-10-27 16:29:04 -06:00
if tweet . has_key ( " status " ) :
2015-04-03 16:57:08 -06:00
if len ( tweet [ " status " ] ) > 4 and system == " Windows " :
2015-01-05 06:05:31 -06:00
original_date2 = arrow . get ( tweet [ " status " ] [ " created_at " ] , " ddd MMM D H:m:s Z YYYY " , locale = " en " )
if relative_times :
ts2 = original_date2 . humanize ( locale = languageHandler . getLanguage ( ) )
2014-10-27 16:29:04 -06:00
else :
2015-01-05 06:05:31 -06:00
ts2 = original_date2 . replace ( seconds = db [ " utc_offset " ] ) . format ( _ ( u " dddd, MMMM D, YYYY H:m:s " ) , locale = languageHandler . getLanguage ( ) )
2015-04-03 16:57:08 -06:00
else :
ts2 = _ ( " Unavailable " )
2014-10-27 16:29:04 -06:00
else :
ts2 = _ ( " Unavailable " )
2015-05-16 23:15:10 -05:00
return [ _ ( u " %s (@ %s ). %s followers, %s friends, %s tweets. Last tweeted %s . Joined Twitter %s " ) % ( tweet [ " name " ] , tweet [ " screen_name " ] , tweet [ " followers_count " ] , tweet [ " friends_count " ] , tweet [ " statuses_count " ] , ts2 , ts ) ]
2014-10-27 16:29:04 -06:00
def compose_event ( data , username ) :
if data [ " event " ] == " block " :
event = _ ( " You ' ve blocked %s " ) % ( data [ " target " ] [ " name " ] )
elif data [ " event " ] == " unblock " :
event = _ ( u " You ' ve unblocked %s " ) % ( data [ " target " ] [ " name " ] )
elif data [ " event " ] == " follow " :
if data [ " target " ] [ " screen_name " ] == username :
event = _ ( u " %s (@ %s ) has followed you " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] )
elif data [ " source " ] [ " screen_name " ] == username :
event = _ ( u " You ' ve followed %s (@ %s ) " ) % ( data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] )
elif data [ " event " ] == " unfollow " :
event = _ ( u " You ' ve unfollowed %s (@ %s ) " ) % ( data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] )
elif data [ " event " ] == " favorite " :
if data [ " source " ] [ " screen_name " ] == username :
2015-11-03 10:07:06 -06:00
event = _ ( u " You ' ve liked: %s , %s " ) % ( data [ " target " ] [ " name " ] , data [ " target_object " ] [ " text " ] )
2014-10-27 16:29:04 -06:00
else :
2015-11-03 10:07:06 -06:00
event = _ ( u " %s (@ %s ) has liked: %s " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " text " ] )
2014-10-27 16:29:04 -06:00
elif data [ " event " ] == " unfavorite " :
2015-11-03 10:07:06 -06:00
if data [ " source " ] [ " screen_name " ] == username : event = _ ( u " You ' ve unliked: %s , %s " ) % ( data [ " target " ] [ " name " ] , data [ " target_object " ] [ " text " ] )
else : event = _ ( u " %s (@ %s ) has unliked: %s " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " text " ] )
2014-10-27 16:29:04 -06:00
elif data [ " event " ] == " list_created " :
event = _ ( u " You ' ve created the list %s " ) % ( data [ " target_object " ] [ " name " ] )
elif data [ " event " ] == " list_destroyed " :
event = _ ( " You ' ve deleted the list %s " ) % ( data [ " target_object " ] [ " name " ] )
elif data [ " event " ] == " list_updated " :
event = _ ( " You ' ve updated the list %s " ) % ( data [ " target_object " ] [ " name " ] )
elif data [ " event " ] == " list_member_added " :
if data [ " source " ] [ " screen_name " ] == username : event = _ ( u " You ' ve added %s (@ %s ) to the list %s " ) % ( data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] , data [ " target_object " ] [ " name " ] )
else : event = _ ( u " %s (@ %s ) has added you to the list %s " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " name " ] )
elif data [ " event " ] == " list_member_removed " :
if data [ " source " ] [ " screen_name " ] == username : event = _ ( u " You ' be removed %s (@ %s ) from the list %s " ) % ( data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] , data [ " target_object " ] [ " name " ] )
else : event = _ ( u " %s (@ %s ) has removed you from the list %s " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " name " ] )
elif data [ " event " ] == " list_user_subscribed " :
if data [ " source " ] [ " screen_name " ] == username : event = _ ( u " You ' ve subscribed to the list %s , which is owned by %s (@ %s ) " ) % ( data [ " target_object " ] [ " name " ] , data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] )
else : event = _ ( u " %s (@ %s ) has suscribed you to the list %s " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " name " ] )
elif data [ " event " ] == " list_user_unsubscribed " :
if data [ " source " ] [ " screen_name " ] == username : event = _ ( u " You ' ve unsubscribed from the list %s , which is owned by %s (@ %s ) " ) % ( data [ " target_object " ] [ " name " ] , data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] )
else : event = _ ( " You ' ve been unsubscribed from the list %s , which is owned by %s (@ %s ) " ) % ( data [ " target_object " ] [ " name " ] , data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] )
2015-03-17 03:39:59 -06:00
elif data [ " event " ] == " retweeted_retweet " :
2015-07-06 08:56:01 -05:00
if data [ " source " ] [ " screen_name " ] == username : event = _ ( u " You have retweeted a retweet from %s (@ %s ): %s " ) % ( data [ " target " ] [ " name " ] , data [ " target " ] [ " screen_name " ] , data [ " target_object " ] [ " retweeted_status " ] [ " text " ] )
else : event = _ ( u " %s (@ %s ) has retweeted your retweet: %s " ) % ( data [ " source " ] [ " name " ] , data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " retweeted_status " ] [ " text " ] )
2015-07-06 09:45:56 -05:00
elif data [ " event " ] == " quoted_tweet " :
event = _ ( u " @ {0} quoted your tweet: {1} " ) . format ( data [ " source " ] [ " screen_name " ] , data [ " target_object " ] [ " text " ] )
2015-03-17 03:39:59 -06:00
else :
event = _ ( " Unknown " )
log . error ( " event: %s \n target: %s \n source: %s \n target_object: %s " % ( data [ " event " ] , data [ " target " ] , data [ " source " ] , data [ " target_object " ] ) )
2014-10-27 16:29:04 -06:00
return [ time . strftime ( " % I: % M % p " ) , event ]
def compose_list ( list ) :
name = list [ " name " ]
if list [ " description " ] == None : description = _ ( u " No description available " )
else : description = list [ " description " ]
user = list [ " user " ] [ " name " ]
members = str ( list [ " member_count " ] )
if list [ " mode " ] == " private " : status = _ ( u " private " )
else : status = _ ( u " public " )
return [ name , description , user , members , status ]