forked from matheusvanzan/instagram-scraper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
59 lines (39 loc) · 1.69 KB
/
models.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
import urllib.request
import os
class Profile:
def __init__(self, username):
self.username = username
self.name = ''
self.num_posts = 0
self.num_followers = 0
self.num_following = 0
self.directory = 'data/' + username
if not os.path.exists(self.directory):
os.makedirs(self.directory)
open('{}/posts.csv'.format(self.directory), 'w+')
def __repr__(self):
return '<Profile {}>'.format(self.username)
def save(self):
with open('{}/data.csv'.format(self.directory), 'w+') as f:
f.write('"{}"; "{}"\n'.format('name', self.name))
f.write('"{}"; "{}"\n'.format('num_posts', self.num_posts))
f.write('"{}"; "{}"\n'.format('num_followers', self.num_followers))
f.write('"{}"; "{}"\n'.format('num_following', self.num_following))
class Post:
def __init__(self, profile):
self.profile = profile
self.id_ = 0
self.url = ''
self.lat = 0
self.lng = 0
self.desc = ''
def __repr__(self):
lenght = max(len(self.desc), 30)
return '<Post {} {}...>'.format(self.id_, self.desc[:lenght])
def download_img(self, src):
filename = '{}/{}.png'.format(self.profile.directory, self.id_)
urllib.request.urlretrieve(src, filename)
def save(self):
with open('{}/posts.csv'.format(self.profile.directory), 'a') as f:
f.write('"{}"; "{}"; "{}"; "{}"\n'.format(
self.id_, self.lat, self.lng, self.desc))