-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
118 lines (93 loc) · 3.88 KB
/
crawler.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
import requests
def get_on_facebook(route, token):
url = "https://graph.facebook.com/v2.10/%s&access_token=%s" % (route,token)
response = requests.get(url)
response.status_code == 200 or quit("Can't get post from page. " + response.reason)
return response.json()
def get_comments(_id, token):
GET_COMMENTS_URL = "%s?fields=comments" % _id
return get_on_facebook(GET_COMMENTS_URL, token)['comments']['data']
def send_pay(merchant_id, payer_id, amount):
"""Send a payment request to tag pay backoffice
:bid: data of transaction
:returns: True if payment is approved.
"""
#TODO put configuration on other file
endpoint = 'http://c821dd2e.ngrok.io/api/transaction'
body = {
'FacebookSellerId' : merchant_id,
'FacebookBuyerId' : payer_id,
'Amount' : int(float(amount)*100)
}
response = requests.post(endpoint, json=body);
return response.status_code == 201
class Bid():
"""Comment with Bid"""
def __init__(self):
"""TODO: to be defined1. """
self.comment_id = ''
self.amount = 0
self.payer_id = ''
class Merchant():
def __init__(self, _id, token):
self.payments = []
self.paid = []
self._id = _id
self.token = token
def get_selling_posts(self):
GET_POSTS_URL = "%s/posts?fields=message" % self._id
posts_on_page = get_on_facebook(GET_POSTS_URL, self.token)['data']
selling_posts = []
sell_tag = '#tagpay #vendo'
for post in posts_on_page:
if post['message'].find(sell_tag) != -1:
post_id = post['id']
selling_posts.append(post_id)
return selling_posts
def get_bid_comments(self, post_id):
comments_on_post = get_comments(post_id, self.token)
bid_comments = []
pay_tag = '#pago'
for comment in comments_on_post:
message = comment['message'].split()
if pay_tag in message:
tag_position = message.index(pay_tag)
bid = Bid()
bid.payer_id = comment['from']['id']
bid.amount = message[tag_position + 1]
bid.comment_id = comment['id']
bid_comments.append(bid)
return bid_comments
def get_payments_on_selling_post(self, post_id):
payments = []
sell_tag = '#vendido'
for bid in self.get_bid_comments(post_id):
replies = get_comments(bid.comment_id, self.token)
for reply in replies:
if sell_tag in reply['message'].split() and reply['from']['id'] == self._id:
payments.append(bid)
return payments
def update_all_payments(self):
for post in self.get_selling_posts():
for payment in self.get_payments_on_selling_post(post):
# update payments
if payment.comment_id not in [p.comment_id for p in self.paid] and payment.comment_id not in [p.comment_id for p in self.payments]:
self.payments.append(payment)
def charge_payments(self):
payments_paid = []
for payment in self.payments:
payment_result_sucessfull = send_pay(self._id, payment.payer_id, payment.amount)
if payment_result_sucessfull:
self.paid.append(payment)
payments_paid.append(payment)
self.payments = [payment for payment in self.payments if payment not in payments_paid]
def print_payments(self):
print "==========="
print "paid: "
for bid in self.paid:
print "Comment %s indicate to pay R$ %s from user %s" % (bid.comment_id, bid.amount, bid.payer_id)
print "---------"
print "payments: "
for bid in self.payments:
print "Comment %s indicate to pay R$ %s from user %s" % (bid.comment_id, bid.amount, bid.payer_id)
print "==========="