-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanong.py
78 lines (61 loc) · 2.12 KB
/
manong.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
# -*- coding: utf-8 -*-
import os
import shutil
import unicodedata
import webbrowser
import re
import requests
from wox import Wox,WoxAPI
from bs4 import BeautifulSoup
ROOT_URL = 'http://weekly.manong.io/'
ISSUE_URL = 'http://weekly.manong.io/issues/'
def full2half(uc):
"""Convert full-width characters to half-width characters.
"""
return unicodedata.normalize('NFKC', uc)
class Main(Wox):
def request(self,url):
#get system proxy if exists
if self.proxy and self.proxy.get("enabled") and self.proxy.get("server"):
proxies = {
"http":"http://{}:{}".format(self.proxy.get("server"),self.proxy.get("port")),
"https":"http://{}:{}".format(self.proxy.get("server"),self.proxy.get("port"))
}
return requests.get(url,proxies = proxies)
return requests.get(url)
def getLastestIssue(self,url):
r = self.request(url)
r.encoding = 'utf-8'
bs = BeautifulSoup(r.content, 'html.parser')
return bs.find('div', class_='menu').find('a')['href']
def query(self, param):
url = self.getLastestIssue(ROOT_URL)
if re.match('^\d+$', param.strip()):
url = ISSUE_URL + param.strip()
r = self.request(url)
r.encoding = 'utf-8'
bs = BeautifulSoup(r.content, 'html.parser')
posts = bs.select('h4')
if not param.strip() or int(param.strip()) > 110:
posts.remove(posts[0])
result = []
for p in posts:
ptitle = p.find('a').string
plink = p.find('a')['href']
user = p.find('small')
psubtitle = p.next_sibling.next_sibling.string
item = {
'Title': u'{subject} by {user}'.format(subject=full2half(ptitle), user=user.string if user else ""),
'SubTitle': psubtitle if psubtitle else "enter to open",
'IcoPath': os.path.join('img', 'manong.png'),
'JsonRPCAction': {
'method': 'open_url',
'parameters': [plink]
}
}
result.append(item)
return result
def open_url(self, url):
webbrowser.open(url)
if __name__ == '__main__':
Main()