Spaces:
Runtime error
Runtime error
Commit
·
449b7fd
1
Parent(s):
9319c5f
Update app.py
Browse files
app.py
CHANGED
@@ -1,122 +1,90 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
# # st.set_page_config(
|
6 |
-
# # page_title="TRACING INSIGHTS",
|
7 |
-
# # page_icon=None,
|
8 |
-
# # layout="wide",
|
9 |
-
# # #initial_sidebar_state="expanded",
|
10 |
-
# # # menu_items={
|
11 |
-
# # # 'Get Help': 'https://www.extremelycoolapp.com/help',
|
12 |
-
# # # 'Report a bug': "https://www.extremelycoolapp.com/bug",
|
13 |
-
# # # 'About': "# This is a header. This is an *extremely* cool app!"
|
14 |
-
# # # }
|
15 |
-
# # )
|
16 |
-
|
17 |
-
# st.write("Hello world!")
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
# GITHUB_PAT = os.environ['GITHUB']
|
22 |
-
# api_key = os.environ['api_key']
|
23 |
-
# secret_api_key = os.environ['secret_api_key']
|
24 |
-
# access_token = os.environ['access_token']
|
25 |
-
# secret_access_token = os.environ['secret_access_token']
|
26 |
-
|
27 |
-
# # if not os.path.exists('repo_directory'):
|
28 |
-
# # Repo.clone_from(f'https://tracinginsights:{GITHUB_PAT}@github.com/TracingInsights/translator.git', 'repo_directory' )
|
29 |
-
|
30 |
-
# # from repo_directory import translator
|
31 |
-
# # translator.auth(api_key,secret_api_key,access_token,secret_access_token)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
# import translators as ts
|
37 |
-
# import translators.server as tss
|
38 |
-
# import random
|
39 |
-
# import tweepy
|
40 |
-
# import time
|
41 |
-
# import json
|
42 |
-
|
43 |
-
# # works with tweets and replies too
|
44 |
-
|
45 |
-
# def bulk_trans(text: str, ):
|
46 |
-
# """
|
47 |
-
# Will translate str into num_trans different languages and then back into original language
|
48 |
-
# :param text: str, must
|
49 |
-
# :return: str
|
50 |
-
# """
|
51 |
-
# curr = tss.google(text, to_language="en", sleep_seconds=0.051)
|
52 |
-
|
53 |
-
# return curr
|
54 |
-
|
55 |
-
# def check_or_write(filename:str, s1:str):
|
56 |
-
# """
|
57 |
-
# Will check if s1 is in a single line within filename and also write
|
58 |
-
# the tweet to filename if it is not inside. Returns true when s1 is
|
59 |
-
# not inside, returns false when s1 is found. Keeps line length of text
|
60 |
-
# file less than 22
|
61 |
-
# :param filename: str(must)
|
62 |
-
# :param s1: str(must)
|
63 |
-
# :return: bool
|
64 |
-
# """
|
65 |
-
# #checks to see if s1 is already inside
|
66 |
-
# with open(filename, 'r', encoding='utf8') as f1:
|
67 |
-
# f1.seek(0)
|
68 |
-
# lines=f1.readlines()
|
69 |
-
# l=0
|
70 |
-
# for line in lines:
|
71 |
-
# l+=1
|
72 |
-
# if s1 in line:
|
73 |
-
# return False
|
74 |
-
# #makes sure that there is not more than 20 lines in txt file
|
75 |
-
# if l>20:
|
76 |
-
# while l>20:
|
77 |
-
# del lines[0]
|
78 |
-
# l-=1
|
79 |
-
# new_file=open(filename, "w+", encoding='utf8')
|
80 |
-
# for line in lines:
|
81 |
-
# new_file.write(line)
|
82 |
-
# new_file.close()
|
83 |
-
|
84 |
-
# #returns true and also writes down text into file at the end
|
85 |
-
# with open(filename, 'a', encoding='utf8') as f1:
|
86 |
-
# f1.write(s1 + '\n')
|
87 |
-
# f1.close()
|
88 |
-
# return True
|
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 |
|
|
|
1 |
+
# from git import Repo
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# st.set_page_config(
|
6 |
+
# page_title="TRACING INSIGHTS",
|
7 |
+
# page_icon=None,
|
8 |
+
# layout="wide",
|
9 |
+
# #initial_sidebar_state="expanded",
|
10 |
+
# # menu_items={
|
11 |
+
# # 'Get Help': 'https://www.extremelycoolapp.com/help',
|
12 |
+
# # 'Report a bug': "https://www.extremelycoolapp.com/bug",
|
13 |
+
# # 'About': "# This is a header. This is an *extremely* cool app!"
|
14 |
+
# # }
|
15 |
+
# )
|
16 |
+
|
17 |
+
st.write("Hello world!")
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
GITHUB_PAT = os.environ['GITHUB']
|
22 |
+
api_key = os.environ['api_key']
|
23 |
+
secret_api_key = os.environ['secret_api_key']
|
24 |
+
access_token = os.environ['access_token']
|
25 |
+
secret_access_token = os.environ['secret_access_token']
|
26 |
+
bearer_token = os.environ['bearer_token']
|
27 |
+
|
28 |
+
# if not os.path.exists('repo_directory'):
|
29 |
+
# Repo.clone_from(f'https://tracinginsights:{GITHUB_PAT}@github.com/TracingInsights/translator.git', 'repo_directory' )
|
30 |
+
|
31 |
+
# from repo_directory import translator
|
32 |
+
# translator.auth(api_key,secret_api_key,access_token,secret_access_token)
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
import translators as ts
|
38 |
+
import translators.server as tss
|
39 |
+
import random
|
40 |
+
import tweepy
|
41 |
+
import time
|
42 |
+
import json
|
43 |
+
|
44 |
+
# works with tweets and replies too
|
45 |
+
|
46 |
+
def bulk_trans(text: str, ):
|
47 |
+
"""
|
48 |
+
Will translate str into num_trans different languages and then back into original language
|
49 |
+
:param text: str, must
|
50 |
+
:return: str
|
51 |
+
"""
|
52 |
+
curr = tss.google(text, to_language="en", sleep_seconds=0.051)
|
53 |
+
|
54 |
+
return curr
|
55 |
|
56 |
+
|
57 |
+
def translate(bearer_token,api_key,secret_api_key,access_token,secret_access_token):
|
58 |
+
# update users when you want to include more accounts
|
59 |
+
users = ["1568348454619070465","2755544640", "1030481714", "407048032","227265199","190632194", "149542215","127854979", "106170617"]
|
60 |
+
|
61 |
+
|
62 |
+
api = tweepy.Client(bearer_token=bearer_token, consumer_key=api_key,
|
63 |
+
consumer_secret=secret_api_key, access_token=access_token,
|
64 |
+
access_token_secret=secret_access_token,wait_on_rate_limit=True
|
65 |
+
)
|
66 |
+
|
67 |
+
#loops endlessly(60 sec interval) and checks,translates,and posts tweets
|
68 |
+
# use this to get user id https://tweeterid.com/
|
69 |
+
latest_tweet_id = "1604906630918705152" # change this if error or restart
|
70 |
+
while True:
|
71 |
+
# Get the first 20 tweets on the home timeline
|
72 |
+
tweets = api.get_home_timeline(max_results=30,exclude='retweets',expansions="author_id", since_id=latest_tweet_id)
|
73 |
+
if tweets.data is not None:
|
74 |
+
latest_tweet_id = tweets.meta['newest_id']
|
75 |
+
for tweet in tweets.data:
|
76 |
+
if tweet.author_id in users:
|
77 |
+
curr = bulk_trans(tweet.text)
|
78 |
+
status = f"{curr[:254]}"
|
79 |
+
print(f"tweet id is {tweet.id}")
|
80 |
+
try:
|
81 |
+
api.create_tweet(text=status, quote_tweet_id=f"{tweet.id}")
|
82 |
+
except:
|
83 |
+
continue
|
84 |
+
print(tweet.text)
|
85 |
+
time.sleep(60)
|
86 |
+
|
87 |
+
translate(bearer_token,api_key,secret_api_key,access_token,secret_access_token)
|
88 |
|
89 |
|
90 |
|