forked from moegirlwiki/MoeGengxin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moeutils.py
53 lines (48 loc) · 1.65 KB
/
moeutils.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
import logging
import moefeeds, moeweibo
from google.appengine.api import urlfetch
from google.appengine.api import images
from BeautifulSoup import BeautifulSoup
def getImage(feed_url):
# Save IMG
page = urlfetch.fetch(feed_url)
if(page.status_code != 200):
return None
soup = BeautifulSoup(page.content)
content = soup.find("div",{"id":"bodyContent"})
img = None
for tag in content.findAll("img"):
if(tag["width"]!=None and ((int(tag["width"])<100) or (int(tag["width"])>800))):
continue
if(tag["height"]!=None and ((int(tag["height"])<100) or (int(tag["height"])>800))):
continue
img = tag
break
image_content = None
if(img != None):
image_remote=urlfetch.fetch(img["pagespeed_lazy_src"])
if(image_remote.status_code!=200):
return None
image_content = image_remote.content
if(img["width"]!=None and img["height"]!=None):
image_content = images.resize(image_content, int(img["width"]), int(img["height"]))
return image_content
def schedule():
feeds = moefeeds.parseFeed("http://zh.moegirl.org/index.php?title=Special:%E6%9C%80%E8%BF%91%E6%9B%B4%E6%94%B9&feed=atom&namespace=0")
lastFeed = moefeeds.getFeedFromFile() #Maybe None
# Find the last unsend feed
feed = moefeeds.findLast(feeds, lastFeed)
if(feed == None):
return "No feeds need to be sent"
feed = moefeeds.writeFeedToFile(feed)
msg = "Feed to be sent "+feed.title+"<br/>"
image = getImage(feed.link)
if(moeweibo.sendBySina(feed.title, feed.link, image)):
msg += "Sina: Success<br/>"
else:
msg += "Sina: Failure<br/>"
if(moeweibo.sendByTencent(feed.title, feed.link, image)):
msg += "Tencent: Success<br/>"
else:
msg += "Tencent: Failure<br/>"
return msg