-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
256 lines (201 loc) · 7.97 KB
/
app.py
File metadata and controls
256 lines (201 loc) · 7.97 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import anthropic
import base64
import os.path
from email.mime.text import MIMEText
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
import asyncio
import os
import anthropic
import atexit
import pandas as pd
# URL to navigate to
url = 'https://www.investing.com/currencies/streaming-forex-rates-majors'
# get element by 'data-test' attribute with value 'dynamic-table'
tag = '[data-test="dynamic-table"]'
# Check if the Chrome executable path exists
if not os.getenv("ANTHROPIC_API_KEY"):
raise Exception("ANTHROPIC_API_KEY environment variable is not set")
client = anthropic.Anthropic(
api_key=os.getenv("ANTHROPIC_API_KEY"),
)
print('Anthropic client initialized')
def get_credentials():
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
return creds
def email_message(service, to, message_text):
message = MIMEText(message_text)
message["to"] = to
message["subject"] = "Forex News"
# Encode the message and send it
raw = base64.urlsafe_b64encode(message.as_bytes()).decode()
message = {'raw': raw}
service.users().messages().send(userId='me', body=message).execute()
def send_email(message_text):
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = get_credentials()
try:
# Call the Gmail API
service = build("gmail", "v1", credentials=creds)
# send email
email_message(service, 'ramsha.bscsf19@iba-suk.edu.pk', message_text)
email_message(service, 'rajahassanali25@gmail.com', message_text)
print("Email sent successfully")
except HttpError as error:
# TODO(developer) - Handle errors from gmail API.
print(f"An error occurred: {error}")
# if __name__ == '__main__':
# asyncio.run(main()) # this will run the main function, which will run the send_data function every 30 minutes
import asyncio
from pyppeteer import launch
import asyncio
from playwright.async_api import async_playwright
#
#
# async def get_source_code(url):
# async with async_playwright() as p:
# # Launch a headless browser
# browser = await p.chromium.launch()
#
# # Open a new page
# page = await browser.new_page()
#
# # Go to the specified URL
# await page.goto(url)
#
# # Get the page content (HTML)
# content = await page.content()
#
# # Close the browser
# await browser.close()
#
# return content
#
#
# # Run the async function and print the result
# source_code = asyncio.run(get_source_code(url))
# print(source_code)
# from playwright.sync_api import sync_playwright
#
# with sync_playwright() as p:
# for browser_type in [p.chromium, p.firefox, p.webkit]:
# browser = browser_type.launch()
# page = browser.new_page()
# page.goto('http://playwright.dev')
# page.screenshot(path=f'example-{browser_type.name}.png')
# browser.close()
#
# import asyncio
# from playwright.async_api import async_playwright
async def get_data_with_scroll(url, selector):
async with async_playwright() as p:
browser = await p.chromium.launch(args=["--disable-infobars"])
page = await browser.new_page()
# await page.set_java_script_enabled(False)
await page.goto(url,wait_until="domcontentloaded")
# Scroll down to the bottom of the page to load more content
previous_height = await page.evaluate("document.body.scrollHeight")
while True:
await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
await page.wait_for_timeout(200000) # Adjust the delay as needed
new_height = await page.evaluate("document.body.scrollHeight")
if new_height == previous_height:
break
previous_height = new_height
# Extract content after scrolling
elements = await page.query_selector_all(selector)
data = [await element.text_content() for element in elements]
await browser.close()
return data
#
# # Run the async function and print the result
# selector = 'p' # Replace with the selector of the elements you want to extract data from
# data = asyncio.run(get_data_with_scroll(url, selector))
# print(data)
websocket_url = "ws://localhost:9222/devtools/browser/b0fdbb83-a425-472f-9372-76283320953a"
import websockets
import asyncio
from playwright.async_api import async_playwright
import bs4
async def get_page_content():
# Replace with your actual WebSocket Debugging URL
async with async_playwright() as p:
# Connect to the already running browser
browser = await p.chromium.connect_over_cdp(websocket_url)
# Get all open pages (tabs)
print(browser.contexts)
pages = browser.contexts[0].pages
while True:
# print( browser.contexts[0])
# Iterate through each page and print the title
print(pages)
for page in pages:
title = await page.title()
print(f'Tab Title: {title}')
# Select the first page (assuming it's the page you want)
# Change index if needed for different tabs
page = pages[0]
# for i in range(len(pages)):
# page = pages[i]
# if 'https://www.investing.com/currencies/streaming-forex-rates-majors' in page.url:
# break
# Get the page content (HTML)
# content = await page.content()
# get data-test="dynamic-table" selector
await page.wait_for_selector('[data-test="dynamic-table"]',
timeout=10000) # Timeout after 10 seconds if not found
# Get the content of the element with the attribute `data-test="dynamic-table"`
table_content = await page.eval_on_selector('[data-test="dynamic-table"]', 'element => element.outerHTML')
# print(table_content)
# Parse the HTML content using BeautifulSoup
soup = bs4.BeautifulSoup(table_content, 'html.parser')
# table
table = soup.find('table')
# print(table)
# print text
print(table.text)
csv_data = table.text
csv_data += ("Based on the above information, predict the potential impact on the Forex market and use the "
"volatility.")
# the message using the Claude model
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1000, # token
temperature=0.6,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": csv_data
}
]
}
]
)
send_email(message.content[0].text)
await asyncio.sleep(1800) # 30 minutes
# return content
# Run the async function and print the result
asyncio.run(get_page_content())
# print(content)