Skip to content

Commit 9c95755

Browse files
authored
Merge pull request #7 from saisyam/create-github-gist
add github gist feature for a file
2 parents 35d6b54 + 1b3d7fa commit 9c95755

File tree

4 files changed

+1506
-3
lines changed

4 files changed

+1506
-3
lines changed

app.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
from flask import Flask
2+
from flask import render_template
3+
from flask import Markup
4+
from gitparse import *
25

36
app = Flask(__name__)
47

5-
@app.route('/')
6-
def index():
7-
return "Flask App Done!"
8+
@app.route('/<path:url>')
9+
def index(url):
10+
filename = url.split("/")[-1]
11+
rawurl = url.replace("blob", "raw")
12+
document = parsegit(url)
13+
return render_template('gistify.html', document=Markup(document), filename=filename, rawurl=rawurl, url=url)
814

915
if __name__ == '__main__':
1016
app.run(host='0.0.0.0', port=8000)

gitparse.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from bs4 import BeautifulSoup
2+
import requests
3+
import random
4+
5+
HEADERS_LIST = [
6+
'Mozilla/5.0 (Windows; U; Windows NT 6.1; x64; fr; rv:1.9.2.13) Gecko/20101203 Firebird/3.6.13',
7+
'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
8+
'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201',
9+
'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16',
10+
'Mozilla/5.0 (Windows NT 5.2; RW; rv:7.0a1) Gecko/20091211 SeaMonkey/9.23a1pre'
11+
]
12+
13+
HEADER = {'User-Agent': random.choice(HEADERS_LIST)}
14+
15+
def parsegit(url):
16+
#url = "https://github.com/saisyam/python-flask/blob/master/simple/app.py"
17+
response = requests.get(url, headers=HEADER)
18+
soup = BeautifulSoup(response.text, "lxml")
19+
div = soup.find_all("div", {"itemprop" : "text"})
20+
div[0].find('details').decompose()
21+
return str(div[0])

0 commit comments

Comments
 (0)