-
Notifications
You must be signed in to change notification settings - Fork 6
/
tweet.py
124 lines (103 loc) · 4.9 KB
/
tweet.py
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
# This file is part of OSMSG (https://github.com/kshitijrajsharma/OSMSG).
# MIT License
# Copyright (c) 2023 Kshitij Raj Sharma
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import os
import humanize
import pandas as pd
import tweepy
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--name",
default="stats",
help="Output stat file name",
)
parser.add_argument(
"--tweet",
required=True,
help="Main Stats Name to include in tweet",
)
parser.add_argument(
"--mention",
default="",
help="Tweeter id to mention on tweet, Provide with @",
)
parser.add_argument(
"--git",
default=None,
help="Github Commit id to include in tweet",
)
args = parser.parse_args()
if not args.git:
args.git = "master"
# Authenticate using your API keys and access tokens
auth = tweepy.OAuthHandler(os.environ["API_KEY"], os.environ["API_KEY_SECRET"])
auth.set_access_token(os.environ["ACCESS_TOKEN"], os.environ["ACCESS_TOKEN_SECRET"])
api = tweepy.API(auth)
summary_text = ""
thread_summary = ""
csv_file = os.path.join(os.getcwd(), f"{args.name}.csv")
full_path = os.path.abspath(os.path.join(os.getcwd(), csv_file))
base_dir = os.path.abspath(os.path.dirname(full_path))
rel_csv_path = os.path.relpath(base_dir, os.getcwd())
# read the .csv file and store it in a DataFrame
df = pd.read_csv(csv_file)
start_date = str(df.iloc[0]["start_date"])
end_date = str(df.iloc[0]["end_date"])
created_sum = df["nodes.create"] + df["ways.create"] + df["relations.create"]
modified_sum = df["nodes.modify"] + df["ways.modify"] + df["relations.modify"]
deleted_sum = df["nodes.delete"] + df["ways.delete"] + df["relations.delete"]
# Get the attribute of first row
summary_text = f"{len(df)} Users made {df['changesets'].sum()} changesets with {humanize.intword(df['map_changes'].sum())} map changes."
thread_summary = (
f"{humanize.intword(created_sum.sum())} OSM Elements were Created, {humanize.intword(modified_sum.sum())} Modified & {humanize.intword(deleted_sum.sum())} Deleted. Including {humanize.intword(df['building.create'].sum())} buildings & {humanize.intword(df['highway.create'].sum())} highways created."
if "building" in df.columns and "highway" in df.columns
else f"{humanize.intword(created_sum.sum())} OSM Elements were Created, {humanize.intword(modified_sum.sum())} Modified & {humanize.intword(deleted_sum.sum())} Deleted. {df.loc[0, 'name']} tops table with {humanize.intword(df.loc[0, 'map_changes'])} changes followed by {df.loc[1, 'name']}"
)
try:
api.verify_credentials()
print("Authentication OK")
except:
print("Error during authentication")
media_ids = []
thread_media_ids = []
chart_png_files = [f for f in os.listdir(base_dir) if f.endswith(".png")]
for i, chart in enumerate(chart_png_files):
file_path = os.path.join(base_dir, chart)
chart_media = api.media_upload(file_path)
if len(media_ids) < 4:
if "top_users" in chart:
thread_media_ids.append(chart_media.media_id)
else:
media_ids.append(chart_media.media_id)
else:
thread_media_ids.append(chart_media.media_id)
orginal_tweet = api.update_status(
status=f"{args.tweet} Contributions\n{start_date} to {end_date}\n{summary_text}\nFullStats:https://github.com/kshitijrajsharma/OSMSG/blob/{args.git}/{rel_csv_path} #gischat #OpenStreetMap {args.mention}",
media_ids=media_ids,
)
if len(thread_media_ids) > 0:
thread_tweet = api.update_status(
status=thread_summary,
in_reply_to_status_id=orginal_tweet.id,
auto_populate_reply_metadata=True,
media_ids=thread_media_ids,
)
if __name__ == "__main__":
main()