url.py 4.95 KB
   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
# -*- coding: utf-8 -*-

import requests
from base62 import Base62
import json
import re

class Url() :

server = "http://10.128.0.20/"

def url2dic(self, links):
data = []

if (True):
for link in links:

link = link.replace('\n'," ").replace('\r'," ").replace('\'',"")

if len(link) < 5:
continue

if self.isdeep(link) :

data.append({
'bitly_url' : "",
'origin_url': str(link),
'bitly_click': "0",
'piki_cid' : str(self.Url2Cid(link)),
'rpiki_click' : "0"
})

elif self.isrpiki(link) :
print "여기?"
print link
data.append({
'bitly_url' : "",
'origin_url': str(link),
'bitly_click': "0",
'piki_cid' : str(self.Url2Cid(link)),
'rpiki_click' : str(self.rpiki2click(link))
})

elif self.isbitly(link):

try:
link = "http://bit.ly/" + re.compile('[^./a-zA-Z0-9]+').sub("",link.split("//bit.ly/")[1].split(" ")[0])

link_meta = link + "+"
txt = requests.get(link_meta).text

source_tag_op = "\"long_url\": \""
source_tag_cl = "\""

clicks_tag_op = "\"user_clicks\": "
clicks_tag_cl = ","

source_bgn = txt.find(source_tag_op) + len(source_tag_op)
source_end = source_bgn + txt[source_bgn:(source_bgn + 500)].find(source_tag_cl)
clicks_bgn = txt.find(clicks_tag_op) + len(clicks_tag_op)
clicks_end = clicks_bgn + txt[clicks_bgn:(clicks_bgn + 50)].find(clicks_tag_cl)

except:
data.append({'bitly_url' : "",'origin_url': "",'bitly_click': "0",'piki_cid' : "0",'rpiki_click' : "0"})

try:
piki_url = str(txt[source_bgn:source_end]).split("cid=")[1].split("&")[0]
except:
piki_url = str(0)

if self.isrpiki(txt[source_bgn:source_end]) :
data.append({
'bitly_url' : str(link),
'origin_url': str(txt[source_bgn:source_end]),
'bitly_click': str(txt[clicks_bgn:clicks_end]),
'piki_cid' : str(Base62().decode(piki_url)),
'rpiki_click' : str(self.rpiki2click(txt[source_bgn:source_end]))
})

else:
#print link
data.append({
'bitly_url' : str(link),
'origin_url': str(txt[source_bgn:source_end]),
'bitly_click': str(txt[clicks_bgn:clicks_end]),
'piki_cid' : str(Base62().decode(piki_url)),
'rpiki_click' : "0"
})

if len(data) == 0:
data.append({'bitly_url' : "",'origin_url': "",'bitly_click': "0",'piki_cid' : "0",'rpiki_click' : "0"})

return data



def Url2Cid(self,url):

try:
if self.isrpiki(url):
return Base62().decode(url.split("cid=")[1].split("&")[0])
elif self.isdeep(url):
return requests.get(url).text.split("http://www.pikicast.com/share/")[1].split('"')[0]
except :
return "0"

def rpiki2click(self,url):
#print url
api = self.server + "contents_RPIKI_api/"

try:
fr = url.split("fr=")[1].split("&")[0]
except:
fr = ""

try:
cid = url.split("cid=")[1].split("&")[0]
m = url.split("m=")[1].split("&")[0]
c = url.split("c=")[1].split("&")[0]
v = url.split("v=")[1].split("&")[0]
t = url.split("t=")[1].split("&")[0]
data = json.loads(requests.get(api + cid + '_' + fr + '_' + m + '_' + c + '_' + v + '_' + t).text)
ret = data['data']['real']
if ret == "":
ret = "0"
except:
ret = "0"

return ret

def getText2bitly(self,text):
return "http://bit.ly/" + text.split("http://bit.ly/")[1].split(" ")[0]


def isdeep(self, url):

if url.find("//fb.me/") > 0 :
return True
else:
return False

def isrpiki(self, url):
if url.find("//r.pikicast.com/") > 0 :
return True
else:
return False

def isbitly(self, url):

if url.find("//bit.ly/") > 0 :
return True
else:
return False