Spaces:
Runtime error
Runtime error
Commit ·
19d1256
1
Parent(s): 5a0e2a3
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ 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",
|
|
@@ -22,8 +22,94 @@ secret_api_key = os.environ['secret_api_key']
|
|
| 22 |
access_token = os.environ['access_token']
|
| 23 |
secret_access_token = os.environ['secret_access_token']
|
| 24 |
|
| 25 |
-
if not os.path.exists('repo_directory'):
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
from repo_directory import translator
|
| 29 |
-
translator.auth(api_key,secret_api_key,access_token,secret_access_token)
|
|
|
|
| 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",
|
|
|
|
| 22 |
access_token = os.environ['access_token']
|
| 23 |
secret_access_token = os.environ['secret_access_token']
|
| 24 |
|
| 25 |
+
# if not os.path.exists('repo_directory'):
|
| 26 |
+
# Repo.clone_from(f'https://tracinginsights:{GITHUB_PAT}@github.com/TracingInsights/translator.git', 'repo_directory' )
|
| 27 |
+
|
| 28 |
+
# from repo_directory import translator
|
| 29 |
+
# translator.auth(api_key,secret_api_key,access_token,secret_access_token)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
import translators as ts
|
| 35 |
+
import translators.server as tss
|
| 36 |
+
import random
|
| 37 |
+
import tweepy
|
| 38 |
+
import time
|
| 39 |
+
import json
|
| 40 |
+
|
| 41 |
+
# works with tweets and replies too
|
| 42 |
+
|
| 43 |
+
def bulk_trans(text: str, ):
|
| 44 |
+
"""
|
| 45 |
+
Will translate str into num_trans different languages and then back into original language
|
| 46 |
+
:param text: str, must
|
| 47 |
+
:return: str
|
| 48 |
+
"""
|
| 49 |
+
curr = tss.google(text, to_language="en", sleep_seconds=0.051)
|
| 50 |
+
|
| 51 |
+
return curr
|
| 52 |
+
|
| 53 |
+
def check_or_write(filename:str, s1:str):
|
| 54 |
+
"""
|
| 55 |
+
Will check if s1 is in a single line within filename and also write
|
| 56 |
+
the tweet to filename if it is not inside. Returns true when s1 is
|
| 57 |
+
not inside, returns false when s1 is found. Keeps line length of text
|
| 58 |
+
file less than 22
|
| 59 |
+
:param filename: str(must)
|
| 60 |
+
:param s1: str(must)
|
| 61 |
+
:return: bool
|
| 62 |
+
"""
|
| 63 |
+
#checks to see if s1 is already inside
|
| 64 |
+
with open(filename, 'r', encoding='utf8') as f1:
|
| 65 |
+
f1.seek(0)
|
| 66 |
+
lines=f1.readlines()
|
| 67 |
+
l=0
|
| 68 |
+
for line in lines:
|
| 69 |
+
l+=1
|
| 70 |
+
if s1 in line:
|
| 71 |
+
return False
|
| 72 |
+
#makes sure that there is not more than 20 lines in txt file
|
| 73 |
+
if l>20:
|
| 74 |
+
while l>20:
|
| 75 |
+
del lines[0]
|
| 76 |
+
l-=1
|
| 77 |
+
new_file=open(filename, "w+", encoding='utf8')
|
| 78 |
+
for line in lines:
|
| 79 |
+
new_file.write(line)
|
| 80 |
+
new_file.close()
|
| 81 |
+
|
| 82 |
+
#returns true and also writes down text into file at the end
|
| 83 |
+
with open(filename, 'a', encoding='utf8') as f1:
|
| 84 |
+
f1.write(s1 + '\n')
|
| 85 |
+
f1.close()
|
| 86 |
+
return True
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# from translator github
|
| 90 |
+
def auth(api_key,secret_api_key,access_token,secret_access_token):
|
| 91 |
+
# Authenticate to Twitter
|
| 92 |
+
auth = tweepy.OAuthHandler(api_key, secret_api_key)
|
| 93 |
+
auth.set_access_token(access_token, secret_access_token)
|
| 94 |
+
api = tweepy.API(auth, wait_on_rate_limit=True)
|
| 95 |
+
|
| 96 |
+
#loops endlessly(60 sec interval) and checks,translates,and posts tweets
|
| 97 |
+
# use this to get user id https://tweeterid.com/
|
| 98 |
+
while True:
|
| 99 |
+
# get twitter user_id at https://tweeterid.com/ by typing username
|
| 100 |
+
timeline = api.user_timeline(user_id="1568348454619070465", #testing case using englishgiuly,
|
| 101 |
+
#["2755544640", "1030481714", "407048032","227265199","190632194", "149542215","127854979", "106170617"],
|
| 102 |
+
count=5, tweet_mode="extended",)
|
| 103 |
+
for tweet in timeline:
|
| 104 |
+
if check_or_write('tweet.txt', " ".join(tweet.full_text.splitlines())):
|
| 105 |
+
curr = bulk_trans(tweet.full_text)
|
| 106 |
+
status = f"{curr[:254]} https://twitter.com/1/status/{tweet.id}" # each link is converted to 23 characters
|
| 107 |
+
api.update_status(status) # doesn't matter what username we use
|
| 108 |
+
print(tweet.full_text)
|
| 109 |
+
time.sleep(60)
|
| 110 |
+
|
| 111 |
+
auth(api_key,secret_api_key,access_token,secret_access_token)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
|
|
|
|
|
|