-
Notifications
You must be signed in to change notification settings - Fork 24
/
main2.py
163 lines (135 loc) · 4.1 KB
/
main2.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# coding:utf-8
import configparser
from pygtrans import Translate
from bs4 import BeautifulSoup
import sys
import os
from urllib import request, parse
import urllib
# pip install pygtrans -i https://pypi.org/simple
# ref:https://zhuanlan.zhihu.com/p/390801784
# ref:https://beautifulsoup.readthedocs.io/zh_CN/latest/
# ref:https://pygtrans.readthedocs.io/zh_CN/latest/langs.html
# client = Translate()
# text = client.translate('Google Translate')
# print(text.translatedText) # 谷歌翻译
import hashlib
def get_md5_value(src):
_m = hashlib.md5()
_m.update(src.encode('utf-8'))
return _m.hexdigest()
import datetime
import time
from rfeed import *
import feedparser
def getTime(e):
try:
struct_time =e.published_parsed
except:
struct_time =time.localtime()
return datetime.datetime(*struct_time[:6])
def getSubtitle(e):
try:
sub =e.subtitle
except:
sub =""
return sub
class GoogleTran:
def __init__(self, url, source = 'auto', target = 'zh-CN'):
self.url = url
self.source= source
self.target=target
self.d = feedparser.parse(url)
self.GT = Translate()
def tr(self,content):
if self.source=='proxy': #代理
return content
tt = self.GT.translate(content,target=self.target,source=self.source)
try:
return tt.translatedText
except:
return ""
def get_newconent(self,max=2):
item_list = []
if len(self.d.entries) < max:
max = len(self.d.entries)
for entry in self.d.entries[:max]:
one = Item(
title=self.tr(entry.title),
link=entry.link,
description=self.tr(entry.summary),
guid=Guid(entry.link),
pubDate=getTime(entry))
item_list += [one]
feed=self.d.feed
newfeed = Feed(
title=self.tr(feed.title),
link=feed.link,
description=self.tr(getSubtitle(feed)),
lastBuildDate=getTime(feed),
items=item_list)
return newfeed.rss()
with open('test.ini', mode = 'r') as f:
ini_data = parse.unquote(f.read())
config = configparser.ConfigParser()
config.read_string(ini_data)
secs=config.sections()
def get_cfg(sec,name):
return config.get(sec,name).strip('"')
def set_cfg(sec,name,value):
config[sec][name]='"%s"'%value
def get_cfg_tra(sec):
cc=config.get(sec,"action").strip('"')
target=""
source=""
if cc == "auto":
source = 'auto'
target = 'zh-CN'
elif cc == "proxy":
source = 'proxy'
target = 'proxy'
else:
source = cc.split('->')[0]
target = cc.split('->')[1]
return source,target
BASE=get_cfg("cfg",'base')
try:
os.makedirs(BASE)
except:
pass
links=[]
def tran(sec):
out_dir= BASE + get_cfg(sec,'name')
url=get_cfg(sec,'url')
max_item=int(get_cfg(sec,'max'))
old_md5=get_cfg(sec,'md5')
source,target=get_cfg_tra(sec)
global links
links+=[" - %s [%s](%s) -> [%s](%s)\n"%(sec,url,(url),get_cfg(sec,'name'),parse.quote(out_dir))]
# new_md5= get_md5_value(url) # no use now
# if old_md5 == new_md5:
# return
# else:
# set_cfg(sec,'md5',new_md5)
c = GoogleTran(url,target=target,source=source).get_newconent(max=max_item)
with open(out_dir,'w',encoding='utf-8') as f:
f.write(c)
#print(c)
#f.write(content)
print("GT: "+ url +" > "+ out_dir)
for x in secs[1:]:
tran(x)
print(config.items(x))
with open('test.ini','w') as configfile:
config.write(configfile)
def get_idx(l):
for idx,line in enumerate(l):
if "## rss translate links" in line:
return idx+2
YML="README.md"
f = open(YML, "r+", encoding="UTF-8")
list1 = f.readlines()
list1= list1[:get_idx(list1)] + links
f = open(YML, "w+", encoding="UTF-8")
f.writelines(list1)
f.close()