-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_politan.py
133 lines (118 loc) · 4.98 KB
/
crypto_politan.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
import time
import requests
import xml.etree.ElementTree as ET
import re
from datetime import datetime
import summarize_api as sa
URL = "https://www.cryptopolitan.com/feed/"
def convert_to_xml(data_str):
root = ET.fromstring(data_str)
return root
def convert_date_from_string(date_str):
date_obj = datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z")
return date_obj.strftime("%Y-%m-%d %H:%M:%S")
def record_article_by_feed(interval):
sa.init_inserted_times()
while True:
try:
response = requests.get(URL)
feed = convert_to_xml(response.text)
print("article updated")
items = feed.findall('.//item')
for item in items:
pubDate = convert_date_from_string(
item.find('.//pubDate').text)
print(sa.registered(pubDate), pubDate)
if not sa.registered(pubDate):
title = item.find('.//title').text
print("title: ", title)
link = item.find('.//link').text
print("link: ", link)
dc_creator = item.find(
'{http://purl.org/dc/elements/1.1/}creator').text
print("author", dc_creator)
category = []
categories = item.findall('.//category')
for cate in categories:
category.append(cate.text)
category = ', '.join(category)
print("category: ", category)
content = item.find(
'{http://purl.org/rss/1.0/modules/content/}encoded').text
content = re.sub('<[^<]+?>', '', content)
summary = sa.summarize_large_text(content)
print(content)
print("#"*150)
article = {
"author": dc_creator,
"title": title,
"category": category,
"currency": "crypto",
"publish_date": pubDate,
"content": content,
"summary": summary,
"link": link
}
sa.insert_article(article)
time.sleep(0.2)
time.sleep(interval)
except requests.exceptions.ConnectionError as e:
print("Error: ", e)
print("Retrying in 5 seconds...")
time.sleep(5)
except Exception as e:
print("Extra Error: ", e)
return
def record_article_by_link(link, insertMode=True):
while True:
try:
article = {}
response = requests.get(link, headers={
"User-Agent": "PostmanRuntime/7.28.4",
})
from lxml import html
tree = html.fromstring(response.text)
title = tree.xpath(
"/html/body/div[1]/div/div/main/div/div[1]/article/div[2]/header/h1")[0].text_content().strip()
content = tree.xpath(
"/html/body/div[1]/div/div/main/div/div[1]/article/div[2]/div[3]/div[2]")[0].text_content().strip()
# content = content.split("Read more")[1]
if not insertMode:
return content
content = sa.summarize_large_text(content)
dc_creator = tree.xpath(
"/html/body/div[1]/div/div/main/div/div[1]/article/div[2]/header/div[2]/div[1]/a/span")[0].text_content().strip()
script = tree.xpath(
"//script[contains(., 'datePublished')]/text()")[0]
import json
data = json.loads(script)
pubDate = data['@graph'][0]['datePublished']
date_object = datetime.strptime(pubDate, "%Y-%m-%dT%H:%M:%S%z")
pubDate = date_object.strftime("%Y-%m-%d %H:%M:%S")
category_meta = tree.xpath('/html/head')[0]
meta_tags = [element for element in category_meta.iterdescendants()
if element.tag == 'meta']
category = []
for meta_tag in meta_tags:
if meta_tag.get('property') == 'article:tag' or meta_tag.get('property') == 'article:section':
category.append(meta_tag.get('content'))
category = ', '.join(category)
article = {
"author": dc_creator,
"title": title,
"category": category,
"currency": "crypto",
"publish_date": pubDate,
"summary": content,
"link": link
}
print("inserting article...")
sa.insert_article(article)
return article
except requests.exceptions.ConnectionError as e:
print("Error: ", e)
print("Retrying in 5 seconds...")
time.sleep(5)
except Exception as e:
print("Extra Error: ", e)
return