-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg4g
More file actions
116 lines (110 loc) · 3.48 KB
/
g4g
File metadata and controls
116 lines (110 loc) · 3.48 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
#!/usr/bin/python
from bs4 import BeautifulSoup
import os, sys
Home = 'http://www.geeksforgeeks.org/'
Extensions = {'cpp':'.cpp', 'java':'.java', 'python':'.py'}
def HorizontalBreak(Number):
Line = '*'*Number
print Line
def GetExtension(pre):
for key in Extensions.keys():
if key in pre:
return Extensions[key]
def DisplayCodeHeadings(pres):
preCodes = ['+']
UnwantedCJava = ['//', '/*', '*/']
SNo = 1
print 'type numbers: [1 2 .. to save the code] or [* to save all the codes] or [0 to exit]'
for pre in pres:
if "brush: cpp" in str(pre):
preCodes.append(pre)
pre = pre.text.splitlines()
heading = ''
for line in pre:
if '//' in line or '/*' in line:
heading += line
if line.startswith('#include') or line.startswith('# include'):
break
for u in UnwantedCJava:
heading = heading.replace(u, '')
print str(SNo) + ') (cpp)' + heading
SNo += 1
elif "brush: java" in str(pre):
preCodes.append(pre)
pre = pre.text.splitlines()
heading = ''
for line in pre:
if '//' in line or '/*' in line:
heading += line
if line.startswith('import') or line.startswith('class'):
break
for u in UnwantedCJava:
heading = heading.replace(u, '')
print str(SNo) + ') (java)' + heading
SNo += 1
elif "brush: python" in str(pre):
preCodes.append(pre)
pre = pre.text.splitlines()
heading = ''
for line in pre:
if '#' in line:
heading += line
if line.startswith('def'):
break
heading = heading.replace('#', '')
print str(SNo) + ') (python)' + heading
SNo += 1
return preCodes
def DisplayContent(URL, StoreFlag):
os.system('curl -s -o .page.html "' + URL + '"')
Page = open('./.page.html')
Soup = BeautifulSoup(Page, 'html.parser')
pres = Soup.find_all('pre')
FileName= URL[len(Home):].replace('/', '')
i = 1
NoCode = 1
if StoreFlag == 1:
preCodes = DisplayCodeHeadings(pres)
if len(preCodes) > 1:
NoCode = 0
Numbers = raw_input('> ').split()
if '0' in Numbers:
print 'Bye!'
exit()
if '*' in Numbers:
Numbers = list(range(1, len(preCodes)))
else:
Numbers = map(int, Numbers)
for Number in Numbers:
Code = preCodes[Number].text.replace('\r', '')
TempFileName = str(FileName) + '-' + str(i) + GetExtension(str(preCodes[Number]))
i += 1
with open(TempFileName, 'w') as Temp:
Temp.write(Code)
print 'Saved ' + str(Number) + ' as => ' + TempFileName
if StoreFlag == 0:
for pre in pres:
if "brush" in str(pre):
NoCode = 0
Code = pre.text.replace('\r', '')
print Code
HorizontalBreak(65)
if NoCode == 1:
print 'Enter a link with code!'
if StoreFlag == 1:
print 'Download Successful!'
os.system('rm .page.html')
if __name__ == '__main__':
if len(sys.argv) >= 2:
StoreFlag = 0
if len(sys.argv) == 3:
if sys.argv[2] == '--store':
StoreFlag = 1
else:
print 'Invalid Command!'
if Home in sys.argv[1]:
DisplayContent(sys.argv[1], StoreFlag)
else:
print "I've been programmed to work only for " + Home
else:
print 'Better Luck Next Time'