-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessage.py
More file actions
165 lines (144 loc) · 6.1 KB
/
message.py
File metadata and controls
165 lines (144 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""create a message for a tweet"""
import contextlib
import random
import pyshorteners
import requests
from constants import (BAR_CHART, CHART_DECREASING, CHART_INCREASING,
DOWN_ARROW, END_MESSAGE, FIRE, GITHUB_LINK, GLOBE,
HASHTAGS, NUMBERS, PAGE_FACE_UP, PERSON,
REPLY_MESSAGE_FOCUS, REPLY_MESSAGE_PEAK,
REPLY_MESSAGE_TOP, REPLY_MESSAGE_TRENDING, RIGHT_ARROW,
SHOPPING_CART, TROPHY, VIDEO_GAME)
def create_message_top_games(games):
"""Create a message for a tweet."""
message = f'Top 10 most played #games on #Steam currently {TROPHY}{GLOBE}\n\n'
games_names = []
for item in games.items():
rank = get_rank(item[1]['rank'])
games_names.append(item[0])
current_players = item[1]['current_players']
message += ((f'{rank}-{item[0]} (Currently : {current_players} {PERSON})\n'))
# add a hashtag for each game (without spaces and special characters)
message += '#' + ''.join(e for e in item[0] if e.isalnum()) + '\n'
return add_hashtags(message, games_names)
def create_message_peak_of_the_day(games):
"""Create a message for a tweet."""
message = f'Peak of players today on #Steam !{GLOBE}{TROPHY}\n\n'
games_names = []
sorted_games = sorted(
games.items(), key=lambda x: x[1]['peak_players'], reverse=True)
sorted_games = dict(sorted_games)
for i, item in enumerate(sorted_games.items()):
rank = get_rank(i+1)
games_names.append(item[0])
peak_players = item[1]['peak_players']
message += (
(f'{rank} {item[0]} (Peak of players: {peak_players} {PERSON}{FIRE})\n'))
# add a hashtag for each game (without spaces and special characters)
message += '#' + ''.join(e for e in item[0] if e.isalnum()) + '\n'
return add_hashtags(message, games_names)
def create_message_trending_games(games):
"""Create a message for a tweet."""
message = f'Top 5 trending #games on #Steam currently {TROPHY}\n\n'
games_names = []
for item in games.items():
rank = get_rank(item[1]['rank'])
evolution = item[1]['evolution']
games_names.append(item[0])
message += (
(f'{rank} {item[0]} (Last 24 hours : {evolution} {CHART_INCREASING})\n'))
# add a hashtag for each game (without spaces and special characters)
message += '#' + ''.join(e for e in item[0] if e.isalnum()) + '\n'
return add_hashtags(message, games_names)
def create_message_game_focus(game):
"""Create a message for a tweet."""
message = f'Focus on {game["name"]} over the last month {VIDEO_GAME}{BAR_CHART}\n'
message += '#' + \
''.join(e for e in game['name'] if e.isalnum()) + ' #Steam\n\n'
message += f'Peak of players : {game["peak_players"]} {PERSON}{FIRE}\n'
message += f'The average number of players : {int(float(game["avg_players"]))}'\
f'{PERSON}{GLOBE}\n'
if game['gain'].startswith('+'):
message += f'Evolution gain compared with the last month : {game["gain"]} '\
f'{CHART_INCREASING}\n'
else:
message += f'Evolution loss compared with the last month : {game["gain"]} '\
f'{CHART_DECREASING}\n'
if game['description'] != 'No description available.':
message += f'Description {PAGE_FACE_UP} : \n'
for text in game['description']:
message += f'{text} '
if text.endswith('.') or text.endswith('!') or text.endswith('?'):
message += '\n'
else:
message += f'{game["description"]} for {game["name"]}\n'
message += '\n\n'
message += f'Link : {game["steam_link"]} {SHOPPING_CART}\n'
message += create_reply_message([game['name']], "focus")[0]
return message
def create_reply_message(games, tweet_type):
"""Create a reply message for a tweet."""
if tweet_type == "trending":
return add_hashtags(REPLY_MESSAGE_TRENDING, games)
if tweet_type == "peak":
return add_hashtags(REPLY_MESSAGE_PEAK, games)
if tweet_type == "focus":
return add_hashtags(REPLY_MESSAGE_FOCUS, games)
return add_hashtags(REPLY_MESSAGE_TOP, games)
def create_links_message(links, games):
"""Create a message to display links of games"""
message = f'Links of the #games in the #leaderboard #today '\
f'{TROPHY}{VIDEO_GAME}\n\n'
for i, link in enumerate(links):
with contextlib.suppress(requests.exceptions.ReadTimeout):
link = pyshorteners.Shortener().tinyurl.short(link)
line = f'{games[i]} {RIGHT_ARROW} {link} {SHOPPING_CART}\n'
message += line
message += '\n'
message += END_MESSAGE
return message
def add_hashtags(message, games):
"""add hashtags to the message"""
message += '\n'
hashtag = ''
for i in range(1, 7):
while hashtag in message:
hashtag = random.choice(HASHTAGS)
message += f'{hashtag} '
if i % 2 == 0:
message += '\n'
message += '\n'
# return the main message and the games names
return message, games
def get_rank(rank):
"""getting the rank of the game in emojis"""
if rank == 1:
rank = "\U0001F947"
elif rank == 2:
rank = "\U0001F948"
elif rank == 3:
rank = "\U0001F949"
else:
rank = NUMBERS[rank]
return rank
def cut_message(message):
"""cut the message into many messages with 280 characters"""
messages = message.split('\n')
final_messages = []
while messages != []:
message = ''
if len(messages[0]) > 250:
messages[0] = f'{messages[0][:250]}...'
# cut messages into 280 characters messages
while len(message) + len(messages[0]) < 240:
message += messages[0] + '\n'
messages.pop(0)
if messages == []:
break
with contextlib.suppress(IndexError):
if message[-2] != '\n':
message += '\n'
message += GITHUB_LINK + ' ' + DOWN_ARROW
final_messages.append(message)
# final messages contains messages cut into 280 characters
return final_messages