-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.py
127 lines (103 loc) · 3.25 KB
/
files.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
125
import os
from ftplib import FTP
import io
import pickle
import random
import psycopg2
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
after_number = int(config['TIMEOUTS']['After_Number']) # The number of messages after which there will be chicken.
to_number = int(config['TIMEOUTS']['To_Number']) # The number of messages to which there will be no chicken.
timer = int(config['TIMEOUTS']['Timer']) # The time interval between regular chickens (not chickens for activity). (seconds)
def connect_to_ftp():
ftp.connect(config['FTP']['FTP'])
ftp.login(user=config['FTP']['FTP_User'],
passwd=config['FTP']['FTP_Password'])
ftp.cwd(config['FTP']['FTP_Directory'])
usingDB = False
usingFTP = False
if 'Database' in config['DATABASE']:
usingDB = True
database = config['DATABASE']['Database']
user = config['DATABASE']['DB_user']
password = config['DATABASE']['DB_password']
host = config['DATABASE']['DB_host']
port = config['DATABASE']['DB_port']
elif ('FTP' in config['FTP']):
usingFTP = True
ftp = FTP(config['FTP']['FTP'])
connect_to_ftp()
elif ('Path' in config['DEFAULT']):
path = config['DEFAULT']['Path']
token = config['DEFAULT']['Token']
def getconnection():
conn = psycopg2.connect(database=database, user=user, password=password, host=host, port=port)
cur = conn.cursor()
return (conn, cur)
def getfiles():
if usingDB:
(conn, cur) = getconnection()
cur.execute("SELECT name FROM Chickens ")
files = cur.fetchall()
conn.close()
return [e[0] for e in files]
if usingFTP:
y = ftp.nlst('.')
return [e for e in y if e not in ('.', '..')]
return os.listdir(path)
def file_to_send(files):
file = files.pop(random.choice(range(len(files))))
return open_file(file)
def open_file(file):
if usingDB:
(conn, cur) = getconnection()
cur.execute("SELECT picture FROM Chickens WHERE name = %s", (file,))
file_data = cur.fetchone()[0]
conn.close()
return file_data
if usingFTP:
connect_to_ftp()
bio = io.BytesIO()
ftp.retrbinary("RETR %s" % file, bio.write)
bio.name = file
bio.seek(0)
return bio
return open(os.path.join(path, file), 'rb')
def get_chats():
if usingDB:
(conn, cur) = getconnection()
cur.execute("SELECT data FROM Chats WHERE List = 1")
data = cur.fetchone()[0]
conn.close()
try:
chats = pickle.loads(data)
except:
chats = set()
return chats
try:
f = open('chats.txt', 'rb')
chats = pickle.load(f)
f.close()
except FileNotFoundError:
chats = set()
return chats
def add_chat(chat_id):
chats = get_chats()
chats.add(chat_id)
save_chats(chats)
def save_chats(chats):
if usingDB:
(conn, cur) = getconnection()
data = pickle.dumps(chats)
cur.execute("UPDATE Chats SET data = %s WHERE list = 1", (data,))
conn.commit()
conn.close()
else:
f = open('chats.txt', 'wb')
pickle.dump(chats, f)
f.close()
def del_chat(chat_id):
chats = get_chats()
chats.discard(chat_id)
save_chats(chats)