forked from F3Nation-Community/PAXminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoin_Channels_and_Create_Directories.py
executable file
·92 lines (81 loc) · 2.48 KB
/
Join_Channels_and_Create_Directories.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
#!/usr/bin/env python3
'''
This script was written by Beaker from F3STL. Questions? @srschaecher on twitter or [email protected].
This script ensures all AO and FirstF channels are joined by PAXminer and it also ensures that the required log and plot directories exist.
'''
from slack_sdk import WebClient
import pandas as pd
import pymysql.cursors
import matplotlib
matplotlib.use('Agg')
import configparser
import os
import sys
# Configure AWS credentials
config = configparser.ConfigParser();
config.read('../config/credentials.ini');
host = config['aws']['host']
port = int(config['aws']['port'])
user = config['aws']['user']
password = config['aws']['password']
db = sys.argv[1]
region = sys.argv[3]
# Set Slack token
key = sys.argv[2]
slack = WebClient(token=key)
firstf = sys.argv[4] #designated 1st-f channel for the region
#Define AWS Database connection criteria
mydb = pymysql.connect(
host=host,
port=port,
user=user,
password=password,
db=db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print("Setting up PAXminer environment for region " + region)
print("Joining FirstF channel")
try:
slack.conversations_join(channel=firstf)
print('Joined FirstF channel ' + firstf)
except:
print('Could not join firstf')
try:
with mydb.cursor() as cursor:
sql = "SELECT channel_id, ao FROM aos WHERE backblast = 1 and archived = 0"
cursor.execute(sql)
aos = cursor.fetchall()
aos_df = pd.DataFrame(aos, columns={'channel_id','ao'})
finally:
print('Pulling all AO channels... Stand by...')
# Join each AO channel
print("Ensuring PAXminer is a member of all AO channels...")
for index, row in aos_df.iterrows():
ao = row['ao']
channel_id = row['channel_id']
print('Joining AO ' + ao)
try:
slack.conversations_join(channel=channel_id)
except:
print('An Error Occurred in Joining ' + ao + " " + channel_id)
#Make sure log and plot directories are created
plotdir ='plots/' + db
logdir = 'logs/' + db
parent_dir = "./"
# Plot Path
plotpath = os.path.join(parent_dir, plotdir)
# Log Path
logpath = os.path.join(parent_dir, logdir)
# Create the directories
print("Creating required log and plot directories for region " + region)
try:
os.mkdir(plotpath)
print("Directory '%s' created" % plotpath)
except OSError as ploterror:
print(ploterror)
try:
os.mkdir(logpath)
print("Directory '%s' created" % logpath)
except OSError as logerror:
print(logerror)
print('End of preparations')