Blame view

insight/postinsight.py 2.78 KB
fa7135c35   System Administrator   first commit
1
2
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
40c655346   Noah   getContextsFeedAl...
3

fa7135c35   System Administrator   first commit
4
5
  import requests
  import json
cd3f2b98c   Noah   링크 본문 댓글 포함 모든 ur...
6
  from url import Url
2e4d69eb8   Noah   본문, 댓글 http://bit...
7

fa7135c35   System Administrator   first commit
8

40c655346   Noah   getContextsFeedAl...
9
  class PostInsight() :
fa7135c35   System Administrator   first commit
10
11
12
  
      token = ""
      type = ""
cd3f2b98c   Noah   링크 본문 댓글 포함 모든 ur...
13
14
      link = ""
      comment = ""
c3f034c75   Noah   단일 컨텐츠를 수동 반영 가능
15
16
      created_time = ""
      message = ""
fa7135c35   System Administrator   first commit
17
      data = {}
a047e6666   Noah   ㅇㅇ
18
      insight_map = {}
fa7135c35   System Administrator   first commit
19
20
  
      def __init__(self,token):
c9e21b9c5   Noah   대시보드 프로젝트에서 분리
21
          self.token = token.getToken()
c99dc50c3   Noah   getContextType 빼먹...
22
          #print "Insight"
fa7135c35   System Administrator   first commit
23
24
  
      def setContentInsight(self,content_id):
c3f034c75   Noah   단일 컨텐츠를 수동 반영 가능
25
          url ='https://graph.facebook.com/v2.5/%s?fields=message,created_time,link,comments.limit(5),type,insights{values}'%content_id
fa7135c35   System Administrator   first commit
26
          txt = requests.get(url + self.token).text
2e4d69eb8   Noah   본문, 댓글 http://bit...
27
          #print url + self.token
fa7135c35   System Administrator   first commit
28
29
          self.data = json.loads(txt)
          self.setContentType(self.data)
2e4d69eb8   Noah   본문, 댓글 http://bit...
30
          self.setContentComment(self.data)
cd3f2b98c   Noah   링크 본문 댓글 포함 모든 ur...
31
          self.setLinkUrl(self.data)
2e4d69eb8   Noah   본문, 댓글 http://bit...
32
          #print self.data
40c655346   Noah   getContextsFeedAl...
33
34
35
36
37
38
39
          try:
              for i in self.data['insights']['data']:
                  name = i['id'].split("/")[2]
                  values = i['values'][0]
                  self.insight_map[name]= values
          except:
              return
a047e6666   Noah   ㅇㅇ
40

a047e6666   Noah   ㅇㅇ
41
      def getContentInsightByKey(self, key):
c9e21b9c5   Noah   대시보드 프로젝트에서 분리
42
          #subkey is double only
a047e6666   Noah   ㅇㅇ
43
44
  
          sub_key_list = key.split(',')
c9e21b9c5   Noah   대시보드 프로젝트에서 분리
45
46
47
          try:
              if len(sub_key_list) == 2:
                  ret = self.insight_map[sub_key_list[0]]['value'][sub_key_list[1]]
c9e21b9c5   Noah   대시보드 프로젝트에서 분리
48
49
              else :
                  ret = self.insight_map[sub_key_list[0]]['value']
c9e21b9c5   Noah   대시보드 프로젝트에서 분리
50
51
          except:
              return 0
40c655346   Noah   getContextsFeedAl...
52
53
54
55
          if ret == {} :
              return 0
  
          return ret
a047e6666   Noah   ㅇㅇ
56
57
58
  
      def getContentInsightAll(self):
          return self.insight_map
fa7135c35   System Administrator   first commit
59

c3f034c75   Noah   단일 컨텐츠를 수동 반영 가능
60
61
62
63
64
65
66
67
68
69
70
71
  
      def setContentCreated_time(self, data):
          try:
              self.created_time = data['created_time']
          except:
              self.created_time = ""
  
      def setContentMessage(self, data):
          try:
              self.message = data['created_time']
          except:
              self.message = ""
fa7135c35   System Administrator   first commit
72
      def setContentType(self, data):
40c655346   Noah   getContextsFeedAl...
73
74
75
76
          try:
              self.type = data['type']
          except:
              self.type = ""
fa7135c35   System Administrator   first commit
77

2e4d69eb8   Noah   본문, 댓글 http://bit...
78
79
      def setContentComment(self, data):
          try:
cd3f2b98c   Noah   링크 본문 댓글 포함 모든 ur...
80
81
              self.comment = Url().getText2bitly(data['comments']['data'][0]['message'])
              #print self.comment
2e4d69eb8   Noah   본문, 댓글 http://bit...
82
83
          except:
              self.comment = ""
cd3f2b98c   Noah   링크 본문 댓글 포함 모든 ur...
84
85
86
87
88
89
90
91
92
      def setLinkUrl(self, data):
          try:
              self.link = data['link']
              #print self.link
          except:
              self.link = ""
  
      def getLinkUrl(self):
          return self.link
2e4d69eb8   Noah   본문, 댓글 http://bit...
93

c99dc50c3   Noah   getContextType 빼먹...
94
      def getContentType(self):
fa7135c35   System Administrator   first commit
95
          return self.type
2e4d69eb8   Noah   본문, 댓글 http://bit...
96

cfdd3d396   Noah   본문, 댓글 http://bit...
97
      def getContentCommentUrl(self):
c3f034c75   Noah   단일 컨텐츠를 수동 반영 가능
98
99
100
101
102
103
104
105
106
107
108
109
110
111
          return self.comment
  
      def getContentCreated_time(self):
          try:
              return self.created_time.replace('T'," ").replace('+0000',"")
          except:
              return ""
  
      def getContentMessage(self):
          try:
              return self.message.replace('
  '," ").replace('\r'," ").replace('\'',"")
          except:
              return ""