Subversion Repositories SmartDukaan

Rev

Rev 1786 | Rev 1789 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1746 vikas 1
#!/usr/bin/python
2
 
3
"""
4
 
5
 This script accepts Domain, Search String and Google Locale arguments, then returns
6
 which Search String results page for the Google Locale the Domain appears on.
7
 
8
 
9
 Usage example:
10
 
11
  rankcheck {domain} {searchstring} {locale}
12
 
13
 
14
 Output example:
15
 
16
  rankcheck geekology.co.za 'bash scripting' .co.za
17
   - The domain 'geekology.co.za' is listed in position 2 (page 1) for the search 'bash+scripting' on google.co.za
18
 
19
"""
20
 
21
__author__    = "Willem van Zyl (willem@geekology.co.za)"
22
__version__   = "$Revision: 1.5 $"
23
__date__      = "$Date: 2009/02/10 12:10:24 $"
24
__license__   = "GPLv3"
25
 
26
import sys, pycurl, re
27
import datetime
28
 
29
# Search Strings to be monitored
1782 vikas 30
SEARCH_STRINGS = [
31
'3g mobile phone',
32
'3g mobile phones',
33
'all mobile phones',
34
'android phone',
35
'android phones',
36
'best mobile phone',
37
'best smart phone',
38
'BLACKBERRY 8520 Unlocked Gemini',
39
'BLACKBERRY 9300 Curve 3G Curve 3G',
40
'BLACKBERRY 9780 Bold',
41
'BLACKBERRY 9800 Torch ',
42
'blackberry mobile phones',
43
'blackberry phones',
44
'business mobile phones',
45
'buy mobile phones',
46
'buy mobile phones online',
47
'cheap mobile phone',
48
'cheap mobile phones',
49
'cheapest mobile phone',
50
'cheapest mobile phones',
51
'compare mobile phone',
52
'compare mobile phones',
53
'Dell M01M Streak',
54
'Dell M01M Streak Black',
55
'dual sim mobile phone',
56
'HTC A7272 Desire Z',
57
'HTC A9191 Desire HD',
58
'htc mobile phone',
59
'htc mobile phones',
60
'htc phones',
61
'HTC S710E Incredible S',
62
'htc smart',
63
'latest mobile phone',
64
'latest mobile phones',
65
'LG A165 ',
66
'LG GS108 ',
67
'LG GS155 ',
68
'LG GU220 ',
69
'LG GX200 ',
70
'LG GX300 ',
71
'LG LG P520 ',
72
'lg mobile phone',
73
'lg mobile phones',
74
'LG P350 Optimus ME',
75
'LG P500 Optimus',
76
'lg phones',
77
'LG S310 ',
78
'LG T300 Cookie Joy',
79
'Micromax Q7 ',
80
'Micromax X-226+ ',
81
'Micromax X-265 ',
82
'Micromax X-410 ',
83
'mobile phone',
84
'mobile phone compare',
85
'mobile phone comparison',
86
'mobile phone models',
87
'mobile phone price',
88
'mobile phone prices',
89
'mobile phone reviews',
90
'mobile phones',
91
'mobile phones comparison',
92
'mobile phones prices',
93
'Motorola EX115 Starling',
94
'Motorola MB502 Charm ',
95
'motorola mobile phones',
96
'new mobile phones',
97
'Nokia 1280 ',
98
'Nokia 1616 ',
99
'Nokia 2700c ',
100
'Nokia 5130c ',
101
'Nokia 5233 ',
102
'Nokia C1-01 ',
103
'Nokia C1-02 ',
104
'Nokia C2-01 ',
105
'Nokia C3-00 ',
106
'Nokia C3-01 ',
107
'Nokia C5-00 ',
108
'Nokia C5-03 ',
109
'Nokia C6-00 ',
110
'Nokia C6-01 ',
111
'Nokia C7-00 ',
112
'Nokia E5-00 ',
113
'Nokia E7-00 ',
114
'Nokia N8 ',
115
'Nokia X-2 ',
116
'Nokia X2-01 ',
117
'Nokia X3-02 ',
118
'qwerty mobile phones',
119
'Samsung B7510 Galaxy Pro',
120
'Samsung B7722 Star DUOS ',
121
'Samsung C3010s ',
122
'Samsung C3200 Monte Bar ',
123
'Samsung C3222 Qwerty',
124
'Samsung C3303 Champ ',
125
'Samsung C3303i Champ Mega Cam',
126
'Samsung C3530 Metro',
127
'Samsung E1081',
128
'Samsung E1160',
129
'Samsung E1252',
130
'Samsung E2152-M with 1GB MMC',
131
'Samsung E2652 Champ Duos',
132
'Samsung i9003 Galaxy S (4GB) ',
133
'samsung mobile',
134
'samsung mobile phone',
135
'samsung mobile phones',
136
'Samsung P1000-Basic Galaxy Tablet ',
137
'Samsung P1000-Basic Galaxy Tablet  Chic White',
138
'samsung phones',
139
'Samsung S3310i Metro with HS and 2Gb',
140
'Samsung S3353 Trevi ',
141
'Samsung S3850 Corby-II',
142
'Samsung S5253 wo Card Wave Series (525)',
143
'Samsung S5263 Star-II',
144
'Samsung S5333 wo Card Wave -Side Slider',
145
'Samsung S5570 Galaxy POP ',
146
'Samsung S8530 Wave 2',
147
'samsung smart phone',
148
'smart mobile phone',
149
'smart mobile phones',
150
'smart phone',
151
'smartphones',
152
'Sony Ericsson E15i Xperia X8 ',
153
'Sony Ericsson E15i Xperia X8 Xperia',
154
'Sony Ericsson E15i Xperia X8 Xperia Dark Blue',
155
'Sony Ericsson E15i Xperia X8 Xperia Swing Pink',
156
'Sony Ericsson LT15i ARC',
157
'Sony Ericsson LT15i ARC Midnight Blue',
158
'Sony Ericsson LT15i ARC Misty Silver',
159
'sony ericsson mobile phone',
160
'sony ericsson mobile phones',
161
'sony ericsson phones',
162
'Sony Ericsson R800i Xperia Play',
163
'Sony Ericsson R800i Xperia Play Black',
164
'Sony Ericsson W100i Spiro ',
165
'Sony Ericsson W100i Spiro  Contrast Black',
166
'Sony Ericsson W100i Spiro  Stealth Black',
167
'Sony Ericsson W100i Spiro  Sunset Pink',
168
'Sony Ericsson W150i Yendo ',
169
'Sony Ericsson W150i Yendo  Black & Red',
170
'Sony Ericsson W150i Yendo  White Blue',
171
'Sony Ericsson W20i Zylo',
172
'Sony Ericsson W20i Zylo Chacha Silver',
173
'Sony Ericsson W20i Zylo Meteorite White',
174
'Sony Ericsson W20i Zylo Swing Pink',
175
'Sony Ericsson W20i Zylo ZAZZ BLACK',
176
'Sony Ericsson X10 Mini Pro Mini Pro (U20i)',
177
'Sony Ericsson Xperia Play R800i',
178
'Spice  G6550 ',
179
'Spice  G6550  Black',
180
'Spice  M5100 ',
181
'Spice  M5100  Black',
182
'Spice  M5570 ',
183
'Spice  M5570  Black',
184
'Spice  M5570  Blue',
185
'Spice  M5570  Red',
186
'Spice  M6350 ',
187
'Spice  M6350  Black & Golden',
188
'Spice  M6460 ',
189
'Spice  M6460  Brown',
190
'Spice  M6460  Gray',
191
'Spice  M9000 ',
192
'Spice  M9000  Black',
193
'Spice  MI310 ',
194
'Spice  MI310  Brown',
195
'Spice M4250 ',
196
'Spice M4250  Black + Red',
197
'Spice M4580 DV ',
198
'Spice M4580 DV  Black & Golden',
199
'Spice M5056 ',
200
'Spice M5056  Black',
201
'Spice M5161n ',
202
'Spice M5161n  Black',
203
'Spice M5161n  Black & Golden',
204
'Spice M5170 ',
205
'Spice M5170  Grey Black',
206
'Spice M5262 ',
207
'Spice M5262  Black',
208
'Spice M5262  Black Blue',
209
'Spice M5454 ',
210
'Spice M5454  Silver Blue',
211
'Spice M5454  Silver Red',
212
'Spice M5750 ',
213
'Spice M5750  Black & Red',
214
'Spice M6363 ',
215
'Spice M6363  Black-Orange',
216
'Spice M6464 ',
217
'Spice M6464  Black',
218
'Spice QT58 Mini ',
219
'Spice QT58 Mini  Black',
220
'Spice QT58 Mini  Red',
221
'Spice QT58 Mini  White',
222
'Spice QT61 Transforme Transformer',
223
'Spice QT61 Transforme Transformer Black',
224
'Spice QT68 ',
225
'Spice QT68  Black',
226
'Spice S1200 ',
227
'Spice S1200  Black',
228
'top mobile phones',
229
'touch screen mobile phones',
230
'touch screen phones'
231
]
232
 
1746 vikas 233
# some initial setup:
234
USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0)'
235
# USER_AGENT = 'Mozilla/5.0'
236
FIND_DOMAIN = 'www.saholic.com'
237
LOCALE = '.co.in'
238
MAX_PAGE = 1
239
NUM_PER_PAGE = 100
240
 
241
# define class to store result:
242
class RankCheck:
243
  def __init__(self):
244
    self.contents = ''
245
 
246
  def body_callback(self, buf):
247
    self.contents = self.contents + buf
248
 
249
 
250
def main():
1786 vikas 251
  for search_string in SEARCH_STRINGS:
1746 vikas 252
    find_google_position(search_string)
253
 
254
def init_curl(rankRequest, rankCheck):
255
  # set up curl:
256
  rankRequest.setopt(pycurl.USERAGENT, USER_AGENT)
257
  rankRequest.setopt(pycurl.FOLLOWLOCATION, 1)
258
  rankRequest.setopt(pycurl.AUTOREFERER, 1)
259
  rankRequest.setopt(pycurl.WRITEFUNCTION, rankCheck.body_callback)
260
  rankRequest.setopt(pycurl.COOKIEFILE, '')
261
  rankRequest.setopt(pycurl.HTTPGET, 1)
262
  rankRequest.setopt(pycurl.REFERER, '')
263
 
264
def search_page(page, page_url):
265
  # instantiate curl and result objects:
266
  rankRequest = pycurl.Curl()
267
  rankCheck = RankCheck();
268
  init_curl(rankRequest, rankCheck)
269
  rankRequest.setopt(pycurl.URL, page_url + '&start=' + str(page * NUM_PER_PAGE))
270
  rankRequest.perform()
271
  # close curl:
272
  rankRequest.close()
273
 
274
  # collect the search results
275
  html = rankCheck.contents
276
  counter = page*NUM_PER_PAGE
277
  result = 0
278
 
279
  url=unicode(r'(<h3 class="r"><a href=")((https?):((//))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)')
280
  for google_result in re.finditer(url, html):
281
    # print m.group()
282
    this_url = google_result.group()
283
    this_url = this_url[23:]
284
    counter += 1
285
 
286
    google_url_regex = re.compile("((https?):((//))+([\w\d:#@%/;$()~_?\+-=\\\.&])*" + FIND_DOMAIN + "+([\w\d:#@%/;$()~_?\+-=\\\.&])*)")
287
    google_url_regex_result = google_url_regex.match(this_url)
288
    if google_url_regex_result:
289
      result = counter
290
      break
291
 
292
  return result
293
 
294
def find_google_position(search_string):
295
  ENGINE_URL = 'http://www.google' + LOCALE + '/search?q=' + search_string.replace(' ', '+') + '&num=' + str(NUM_PER_PAGE)
296
  # print ENGINE_URL
297
 
298
  # run curl:
299
  for i in range(0, MAX_PAGE):
300
    result = search_page(i, ENGINE_URL)
301
    if result != 0:
302
      break
303
 
1787 vikas 304
  f = open('/var/lib/tomcat6/webapps/db/googleranks/rank-' + datetime.datetime.now().strftime("%Y-%m-%d") + '.txt', 'a')
1785 vikas 305
 
1746 vikas 306
  # show results
307
  if result == 0:
1787 vikas 308
    f.write("{0:s}, {1:s}, {2:d}, {3:d}\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), search_string, NUM_PER_PAGE*MAX_PAGE, MAX_PAGE*NUM_PER_PAGE/10))
1746 vikas 309
  else:
1787 vikas 310
    f.write("{0:s}, {1:s}, {2:d}, {3:d}\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), search_string, result, result/10 + 1))
1746 vikas 311
 
312
 
313
# Run Main
314
main()