-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostService.py
122 lines (102 loc) · 3.06 KB
/
postService.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
import facebook
import requests
from flask import Flask, jsonify, abort, make_response, request, url_for, current_app
from flask.ext.pymongo import PyMongo
from bson.json_util import dumps
from bson.objectid import ObjectId
from datetime import timedelta, datetime
from time import sleep
import random as rand
import db_connect
import os
class Post(object):
"""docstring for Post"""
def __init__(self, data):
super(Post, self).__init__()
try:
self.message = data['message']
self.created = datetime.strptime(data['created_time'].partition('+')[0], '%Y-%m-%dT%X')
self.fb_id = data['id']
self.comments = data['comments']
self.likes = data['likes']
except KeyError:
pass
class PostService(object):
"""docstring for PostService"""
def __init__(self, test=False):
super(PostService, self).__init__()
self.app = Flask("Crushes")
self.mongo, self.crushes = db_connect.connect()
self.db = self.crushes.posts
self.CLIENT_ID = "686893277993623" if test else os.environ.get('fb_client_id')
self.CLIENT_SECRET = "518865db69daa46de95171f00a56fc25" if test else os.environ.get('fb_client_secret')
def authenticate(self):
return facebook.get_app_access_token(self.CLIENT_ID, self.CLIENT_SECRET)
def getPosts(self, **kwargs):
oauth_token = self.authenticate()
graph = facebook.GraphAPI(oauth_token)
posts = graph.get_connections("UChicagoCrushes", "posts", **kwargs)
posts_data = posts['data']
processed_posts = []
for p in posts_data:
pst = Post(p)
try:
pst.message
processed_posts.append(pst)
except Exception, e:
pass
try:
paging_data = posts['paging']
return processed_posts, paging_data
except KeyError, e:
print "Fetching Complete"
return processed_posts, False
def pgNext(self, paging):
try:
nxt = paging['next']
until = nxt.partition("&until=")
return until[2]
except Exception, e:
return False
def pgPrev(self, paging):
try:
nxt = paging['previous']
since = nxt.partition("&since=")
since = since[2].partition("&")
return since[0]
except Exception, e:
return False
def getPostsSince(self, time):
posts, paging = self.getPosts(limit=250, since=time)
sin = self.pgPrev(paging)
return posts, sin
def getPostsUntil(self, time):
posts, paging = self.getPosts(limit=250, until=time)
unt = self.pgNext(paging)
return posts, unt
def updatePosts(self, time):
with self.app.app_context():
posts, sin = self.getPostsSince(time)
if posts:
self.db.insert(map(lambda x: vars(x),posts))
cond = True
while cond:
sleep(rand.random())
posts, sin = self.getPostsSince(sin)
if sin and posts:
self.db.insert(map(lambda x: vars(x),posts))
else:
cond = False
def curatePosts(self):
with self.app.app_context():
posts, unt = self.getPostsUntil(time)
if posts:
self.db.insert(map(lambda x: vars(x),posts))
cond = True
while cond:
sleep(rand.random())
posts, unt = self.getPostsUntil(unt)
if unt and posts:
self.db.insert(map(lambda x: vars(x),posts))
else:
cond = False