-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeautyptt.py
114 lines (100 loc) · 3.32 KB
/
beautyptt.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
import sys
import json
import os
import time
import re
import requests
from io import BytesIO
from configparser import ConfigParser
from PyPtt import PTT
from Facebooker import facebook
ptt_bot = PTT.API(log_level=PTT.log.level.SILENT)
fb = facebook.API()
config = ConfigParser()
config.read('config.ini')
fb_account = config.get('facebook','account')
fb_password = config.get('facebook','password')
fanpage_id = config.get('facebook','fanpage_id')
ptt_account = config.get('PTT','account')
ptt_password = config.get('PTT','password')
fb.login(fb_account, fb_password)
try:
ptt_bot.login(ptt_account, ptt_password)
except PTT.exceptions.LoginError:
ptt_bot.log('登入失敗')
sys.exit()
except PTT.exceptions.WrongIDorPassword:
ptt_bot.log('帳號密碼錯誤')
sys.exit()
except PTT.exceptions.LoginTooOften:
ptt_bot.log('請稍等一下再登入')
sys.exit()
ptt_bot.log('登入成功')
Beauty_board = 'Beauty'
posts = []
post_history = {}
newest_index = ptt_bot.get_newest_index(
PTT.data_type.index_type.BBS,
board=Beauty_board
)
start_index = newest_index / 2
if os.path.isfile('posts.json'):
with open('posts.json', 'r') as f:
post_history = json.load(f)
start_index = int(post_history['latest_post'])
end_index = start_index + 1000
for i in range(start_index,end_index):
print('%d/%d'%(i+1-start_index, end_index-start_index))
try:
post_info = ptt_bot.get_post(
Beauty_board,
post_index=i,
query=True
)
except Exception:
continue
try:
if post_info.title.find('[投稿]') >= 0 or post_info.title.find('[公告]') >= 0 :
continue
if post_info.push_number == '爆':
posts.append(post_info)
elif int(post_info.push_number) >= 30:
posts.append(post_info)
except:
pass
for post in posts:
if post.aid in post_history:
continue
try:
post = ptt_bot.get_post(
Beauty_board,
post_aid=post.aid,
)
except Exception:
ptt_bot.login(ptt_account, ptt_password)
post = ptt_bot.get_post(
Beauty_board,
post_aid=post.aid,
)
fb_post = '原文連結:%s\n'%post.web_url + \
'標題:%s\n'%post.title + \
'作者:%s\n'%post.author + \
post.content
match = re.search('http(s|)://(i\.|)imgur.com/(.+/|)[a-zA-Z0-9]+(\.jpg|)', post.content)
print(post.title)
if match:
img_url = match.group(0)
if img_url.find('.jpg') < 0:
img_url += '.jpg'
print('match image:%s'%img_url)
response = requests.get(img_url)
if response.status_code == 200:
image = BytesIO(response.content)
fb.fanpage_post_photo(fb_post, image, fanpage_id)
else:
fb.fanpage_post(fb_post, fanpage_id)
post_history[post.aid] = post.content
post_history['latest_post'] = post.index
with open('posts.json', 'w') as f:
json.dump(post_history, f, ensure_ascii=False)
time.sleep(600)