/
app.py
79 lines (65 loc) · 2.19 KB
/
app.py
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
#!/usr/bin/env python3
import json
import urllib.parse
import re
import calverter
import requests
from bs4 import BeautifulSoup
from flask import Flask, request, Response
app = Flask(__name__)
@app.route('/uploadhelper-ir/<path:url>')
def tasnimgallery(url):
try:
if url.find('http://tasnimnews.com/') != 0:
raise Exception('Not supported link')
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
article = soup.select_one('body.photos article.media')
images = map(lambda x: {
'link': x['href'],
'thumb': x.find('img')['src']
}, article.select('.row a'))
result = {
'title': article.select_one('h1.title').text.strip(),
'reporter': article.select_one('h4.reporter').text.strip(),
'time': parsedate(article.select_one('time').text.strip()),
'lead': article.select_one('h3.lead').text.strip(),
'images': list(images),
'url': url
}
except Exception as e:
result = {"error": str(e)}
response = Response(
json.dumps(result, indent=1, ensure_ascii=False),
content_type='application/json;charset=utf8')
# TODO: This should be limited
response.headers['Access-Control-Allow-Origin'] = "*"
return response
@app.route('/uploadhelper-ir/tasnimcrop/<path:url>')
def tasnimcrop(url):
response = Response('', content_type='application/json;charset=utf8')
response.headers['Access-Control-Allow-Origin'] = "*"
return response
if __name__ == '__main__':
app.run()
cal = calverter.Calverter()
months = {
"فروردین": 1,
"اردیبهشت": 2,
"خرداد": 3,
"تیر": 4,
"مرداد": 5,
"شهریور": 6,
"مهر": 7,
"آبان": 8,
"آذر": 9,
"دی": 10,
"بهمن": 11,
"اسفند": 12
}
distance = ord('۰') - ord('0')
def parsedate(date):
date = re.sub(r'[۰-۹]', lambda x: chr(ord(x.group(0)) - distance), date)
m = re.match(r'(\d\d?) ([^ ]*) (\d{4}) - (\d\d?):(\d\d?)', date)
jd = cal.jalali_to_jd(int(m.group(3)), months[m.group(2)], int(m.group(1)))
greg = cal.jd_to_gregorian(jd)
return '%d-%d-%d' % greg + ' %s:%s' % (m.group(4), m.group(5))