forked from txomon/tuenti-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
td.py
171 lines (140 loc) · 4.78 KB
/
td.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
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
166
167
168
169
170
171
import argparse
import logging
import shutil
from datetime import datetime
from os import path, makedirs
from urlparse import urlsplit
import requests
try:
import gi
gi.require_version('GExiv2', '0.10')
from gi.repository.GExiv2 import Metadata as exiv_file
except:
class exiv_file:
def __init__(self, filepath):
pass
def set_date_time(self, date):
pass
def save_file(self):
pass
from tuenti import TuentiSocialMessenger
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)
def get_user_album_photos(tsm, user=None):
page = 0
while True:
if (user):
res = tsm.Profile_getAlbumPhotos({'page': page, 'pageSize': 1000, 'userId': user})
else:
res = tsm.Profile_getAlbumPhotos({'page': page, 'pageSize': 1000})
for item in res['items']:
yield item
if not res['hasMore']:
return
page += 1
def gen_file_path(file_uri):
_, _, uri_path, _, _ = urlsplit(file_uri)
_, _, name = uri_path.rpartition('/')
name = '%s.jpg' % name
if not path.exists('dl'):
makedirs('dl')
file_path = path.join('dl', name)
if path.exists(file_path):
return None
return file_path
class FileDownloader:
def __init__(self):
self.dl = requests.Session()
def download_file(self, file_uri, file_path):
r = self.dl.get(file_uri, stream=True)
if r.status_code == 200:
with open(file_path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
return True
else:
return False
def save_photo(self, photo):
file_uri = photo['photo']['fullUrl']
file_path = gen_file_path(file_uri)
ts = long(photo['photo']['timestamp'])
if not file_path:
logger.debug('Skipping file %s', file_uri)
return
logger.debug('Downloading file %s', file_path)
if self.download_file(file_uri, file_path):
logger.debug('Done')
update_ts(file_path, ts)
else:
logger.debug('Failed')
def update_ts(file, timestamp):
try:
ef = exiv_file(file)
ef.set_date_time(datetime.fromtimestamp(timestamp))
ef.save_file()
except Exception as e:
logger.exception('Something happened when writting time metadata in file %s', file);
class IDCollector():
def __init__(self):
self.todo = set()
self.done = set()
self.user_id = ''
def iterate(self):
while self.todo:
item = self.todo.pop()
self.done.add(item)
yield item
def add(self, item):
if item in self.done:
return
if len(self.todo) == 0 and len(self.done) == 1:
self.user_id = item
self.todo.add(item)
def collect_ids(self, photo):
self.add(photo['authorId'])
for comment in photo['lastComments']:
self.add(comment['authorId'])
for tag in photo['photo']['tags']:
self.add(tag['userId'])
self.add(photo['photo']['uploaderId'])
def is_photo_collectable(self, photo):
if not self.user_id:
return True
if self.user_id == photo['authorId']:
return True
for comment in photo['lastComments']:
if comment['authorId'] == self.user_id:
return True
for tag in photo['photo']['tags']:
if tag['userId'] == self.user_id:
return True
if photo['photo']['uploaderId'] == self.user_id:
return True
return False
def gen_log_message(self):
done_len = len(self.done)
logger.info('Users %d/%d', done_len, done_len + len(self.todo))
def main():
args_eng = argparse.ArgumentParser(description='Tuenti image downloader', add_help=True)
args_eng.add_argument('user', help='Tuenti login email')
args_eng.add_argument('pwd', help='Tuenti password')
args = args_eng.parse_args()
tsm = TuentiSocialMessenger.from_credentials(args.user, args.pwd)
file_downloader = FileDownloader()
collector = IDCollector()
collector.add('')
total_count = 0
saved_count = 0
for user_id in collector.iterate():
logger.debug("Inspecting user %s", user_id)
collector.gen_log_message()
for photo in get_user_album_photos(tsm, user_id):
total_count += 1
if collector.is_photo_collectable(photo):
collector.collect_ids(photo)
saved_count += 1
file_downloader.save_photo(photo)
if saved_count and not total_count % 100:
logger.info("Downloaded %d/%d photos", saved_count, total_count)
if __name__ == '__main__':
main()