-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
60 lines (55 loc) · 1.91 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import sys
import time
from os import getenv
from macrumors import Macrumors
from neowin import Neowin
from translator import Translate
from wordpress import Wordpress
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
url = getenv("URL", None)
assert url is not None, "Please set URL"
login = getenv("LOGIN", None)
assert login is not None, "Please set LOGIN"
password = getenv("PASSWORD", None)
assert password is not None, "Please set PASSWORD"
if __name__ == "__main__":
max_post_from_adapter = 10
while True:
n = Neowin()
m = Macrumors()
t = Translate()
w = Wordpress(url, login, password)
adapters = [n, m]
for a in adapters:
new_posts_urls = a.get_new_posts()
i = 0
for post_url in new_posts_urls:
if i >= 10:
break
try:
soup = a.get_context(post_url)
title = t.go(a.get_title(soup))
description = t.go(a.get_description(soup))
image_url = a.get_cover_image(soup)
content = t.go(a.get_content(soup))
image_upload = w.upload_image(image_url, title, description)
post_created = w.create_post(
title=title,
content=content,
excerpt=description,
featured_media=image_upload["id"],
status="publish",
)
except Exception as e:
logging.warning(e)
logging.warning(sys.exc_info())
finally:
w.save_url_in_cache(post_url)
i += 1
logging.info("Sleep 1 hour")
time.sleep(60 * 60 * 1)