-
Notifications
You must be signed in to change notification settings - Fork 3
/
myfacebook.py
159 lines (127 loc) · 4.86 KB
/
myfacebook.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
#-*- coding: utf-8 -*-
import time
from facepy import GraphAPI
import os
import webapp2
import jinja2
from google.appengine.ext import ndb
from google.appengine.api import users
template_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.getcwd()))
class MyGroupIDS(ndb.Expando):
strReference = ndb.StringProperty()
strGroupID = ndb.StringProperty()
isWorking = ndb.BooleanProperty(default=False)
def writeReference(self,strinput):
try:
strinput = str(strinput)
if not(strinput == None):
self.strReference = strinput
return True
else:
return False
except:
return False
def writeGroupID(self,strinput):
try:
strinput = str(strinput)
if strinput != None:
self.strGroupID = strinput
return True
else:
return False
except:
return False
def setWorked(self):
try:
self.isWorking = True
return True
except:
return False
class GroupPosts(ndb.Expando):
strReference = ndb.StringProperty()
strApiKey = ndb.StringProperty()
strMyPost = ndb.StringProperty()
def writeReference(self,strinput):
try:
strinput = str(strinput)
if not(strinput == None):
self.strReference = strinput
return True
else:
return False
except:
return False
def writeApiKey(self,strinput):
try:
strinput = str(strinput)
if not(strinput == None):
self.strApiKey = strinput
return True
else:
return False
except:
return False
def writeMyPost(self,strinput):
try:
strinput = str(strinput)
if not(strinput == None):
self.strMyPost = strinput
return True
else:
return False
except:
return False
class FacebookGroupHandler(webapp2.RequestHandler):
def get(self):
Guser = users.get_current_user()
if Guser:
vstrGraphApiKey = self.request.get('vstrGraphApiKey')
vstrFacebookPost = self.request.get('vstrFacebookPost')
vstrGroupIDs = self.request.get('vstrGroupIDs')
GroupIDList = vstrGroupIDs.split(" ")
findRequest = MyGroupIDS.query(MyGroupIDS.strReference == Guser.user_id())
thisGroupIDList = findRequest.fetch()
for thisGroup in thisGroupIDList:
if thisGroup.strGroupID in GroupIDList:
GroupIDList.remove(thisGroup.strGroupID)
for thisGroup in GroupIDList:
NewGroup = MyGroupIDS()
NewGroup.writeReference(strinput=Guser.user_id())
NewGroup.writeGroupID(strinput=thisGroup)
NewGroup.put()
if not(vstrGraphApiKey == None):
try:
myGraph = GraphAPI(vstrGraphApiKey)
if not(vstrFacebookPost == None):
findRequest = GroupPosts.query(GroupPosts.strReference == Guser.user_id())
thisGroupPostsLists = findRequest.fetch()
if len(thisGroupPostsLists) > 0:
thisGroupPost = thisGroupPostsLists[0]
else:
thisGroupPost = GroupPosts()
thisGroupPost.writeReference(strinput=Guser.user_id())
thisGroupPost.writeApiKey(strinput=vstrGraphApiKey)
thisGroupPost.writeMyPost(strinput=vstrFacebookPost)
thisGroupPost.put()
self.response.write("Facebook Group Posts successfully created")
except:
self.response.write("theres an Error creating Facebook Group Posts")
class CronFacebookGroupHandler(webapp2.RequestHandler):
def get(self):
findRequest = GroupPosts.query()
thisGroupPostList = findRequest.fetch()
for thisGroupPost in thisGroupPostList:
findRequest = MyGroupIDS.query(MyGroupIDS.strReference == thisGroupPost.strReference)
thisGroupIDList = findRequest.fetch()
myGraph = GraphAPI(thisGroupPost.strApiKey)
for thisGroupID in thisGroupIDList:
try:
myGraph.post(path=str(thisGroupID.strGroupID) + '/feed', message=thisGroupPost.strMyPost)
thisGroupID.isWorking = True
except:
thisGroupID.isWorking = False
app = webapp2.WSGIApplication([
('/social/facebook/groups', FacebookGroupHandler),
('/social/facebook/cron/groups', CronFacebookGroupHandler)
], debug=True)