-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcountrycode.py
More file actions
47 lines (39 loc) · 1.23 KB
/
countrycode.py
File metadata and controls
47 lines (39 loc) · 1.23 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
#!/usr/bin/env python
#
# countrycode.py
# Author: Ershad K <ershad92@gmail.com>
# License: GPL Version 3
import urllib2
ccode = raw_input('Enter country code (Eg: +91): ')
page = urllib2.urlopen('http://www.countrycallingcodes.com/Reverse-Lookup.php?calling-code=%s' % ccode.replace('+',''))
pageContent = page.readlines()
flag = 0
for line in pageContent:
if line.find("""<TABLE BORDER='0'""") >= 0:
cline = line
flag = 1
if flag is 0:
print 'Invalid country code.'
exit(1)
countryName = cline.split('<i>')[1].split('<')[0]
print countryName
headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request('http://www.google.com/search?q=%s+time' % countryName, None, headers)
try:
timePageContent = urllib2.urlopen(req).readlines()
except:
print 'Time cannot be displayed'
exit(1)
tline = ''
for line in timePageContent:
if line.find('<b>Time</b> in <b>') >= 0:
tline = line
endIndex = tline.find('<b>Time</b> in <b>') - 3
startIndex = tline.find('e:medium">') + len('e:medium">')
timeString = ''
while startIndex <= endIndex:
timeString += tline[startIndex]
startIndex += 1
timeString = timeString.replace('<b>','')
timeString = timeString.replace('</b>','')
print 'Time: ' + timeString