-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpost.py
More file actions
106 lines (82 loc) · 3.3 KB
/
post.py
File metadata and controls
106 lines (82 loc) · 3.3 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
import datetime
import os
import vk_api
class ImportContentError(Exception): pass
class Post():
def __init__(self, time, path):
self.time = time
self.__path = path
self.__photos = []
self.__docs = []
self.__message = ''
self.__message_file = None
self.__attachments = ''
@property
def time(self):
return self.__time
@time.setter
def time(self, time):
assert isinstance(time, datetime.datetime), "Invalid post time"
self.__time = time
def __len__(self):
return len(self.__photos) + len(self.__docs)
def __eq__(self, other):
return self.__time == other.__time
def __lt__(self, other):
return self.__time < other.__time
def import_content(self):
files = os.listdir(self.__path)
if files:
for file_ in files:
if file_.endswith('.jpg') or file_.endswith('.png') or file_.endswith('.jpeg'):
self.__photos.append(file_)
elif file_.endswith('.gif'):
self.__docs.append(file_)
elif file_.endswith('.txt') and self.__message_file is None:
self.__message_file = file_
else:
raise ImportContentError('Folder "{}" is empty'.format(os.path.basename(self.__path)))
def upload_content(self, vk_session, user_id, group_id):
upload = vk_api.VkUpload(vk_session)
if self.__photos:
photos_path = [os.path.join(self.__path, photo) for photo in self.__photos]
photos = upload.photo_wall(photos_path, user_id, group_id)
atcms = ['photo' + str(photo['owner_id']) + '_' + str(photo['id']) for photo in photos]
atcms = ','.join(atcms)
self.__attachments = ','.join( (self.__attachments, atcms) )
if self.__docs:
for doc in self.__docs:
path = os.path.join(self.__path, doc)
dct = upload.document_wall(path, doc[:-4])
atcmt = 'doc' + str(dct[0]['owner_id']) + '_' + str(dct[0]['id'])
self.__attachments = ','.join( (self.__attachments, atcmt) )
if self.__message_file:
with open(os.path.join(self.__path, self.__message_file), 'rb') as f:
for line in f:
self.__message += line.decode('utf-8')
def post(self, vk_session, owner_id):
vk = vk_session.get_api()
response = vk.wall.post(owner_id=owner_id,
from_group=1,
message=self.__message,
attachments=self.__attachments)
return response
def get_time(folder):
try:
dt = datetime.datetime.strptime(folder, "%d.%m.%y %H.%M")
return dt
except:
raise ValueError('Can not convert "{}" folder to post time'.format(folder))
def make_posts(posts_path):
posts = []
for p in os.listdir(posts_path):
if p == '.DS_Store':
continue
try:
post = Post( get_time(p), os.path.join(posts_path, p) )
post.import_content()
print( 'post in {} - {} objects'.format(post.time, len(post)) )
posts.append(post)
except Exception as err:
print('Error ({})'.format(err))
return posts