forked from abigailhaddad/FASCLASS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullFromPythonAnywhere.py
More file actions
58 lines (50 loc) · 1.71 KB
/
pullFromPythonAnywhere.py
File metadata and controls
58 lines (50 loc) · 1.71 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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 7 11:43:31 2021
This is for pulling my FASCLASS xlsx files from pythonanywhere if you have the APIkey
@author: HaddadAE
"""
import os
import sys
import requests
import pandas as pd
def pullToken(fileName):
with open(fileName) as f:
lines = f.readlines()
token=lines[0][1:-1]
return(token)
def listOfFiles(token):
username='abigailhaddad'
pythonanywhere_host = "www.pythonanywhere.com"
url = f"https://{pythonanywhere_host}/api/v0/user/{username}/files/path/home/{username}/fasclassBetter/data/"
resp = requests.get(url,
headers={"Authorization": "Token {api_token}".format(api_token=token)}, verify=False)
listOfFiles=list(resp.json().keys())
filesToKeep=[i for i in listOfFiles if "textScrape" in i and ".xlsx" in i]
return(filesToKeep, url)
def pullFile(file, token, url):
print(file)
resp = requests.get(url+file,
headers={"Authorization": f"Token {token}"}, verify=False)
with open(file, 'wb') as output:
output.write(resp.content)
def pullFiles(fileName):
### pulls all files
os.chdir(os.getcwd().replace("code", "data"))
token=pullToken(fileName)
filesToKeep, url=listOfFiles(token)
for file in filesToKeep:
pullFile(file, token, url)
def readAggregate():
os.chdir(os.getcwd().replace("code", "data"))
files=[pd.read_excel(i) for i in os.listdir() if "textScrape" in i]
wholeDF=pd.concat(files)
return(wholeDF)
if __name__ == '__main__':
### Provide the APIkey file as arg 1 if omitted it will default to APIkey.txt.
try:
filename = sys.argv[1]
except:
filename='APIkey.txt'
pullFiles(filename)
df=readAggregate()