Download files through GitLab API that has been manually uploaded - gitlab

We have a self-hosted GitLab server and are working on automating our builds and releases. We have many old releases that we have built before using GitLab CI. Some of these should be included in a release package for a certain software. The releases are not located on any server that is easy to access, so it would be very easy if they could be accessed from our GitLab server.
It is possible to access tags from the API and to get artifacts from the build jobs. It doesn't seem possible to add build artifacts manually, so there is no way of using this for old releases.
It is possible to upload files to the release notes of a tag. These are very simple to download through the webpage, but I can't find any way of downloading these through the API. There is this API endpoint:
https://docs.gitlab.com/ee/api/projects.html#upload-a-file
but there is no "download-a-file".
Is there an easy way to upload files to our self-hosted GitLab and then to download them through the API?
All of our repositories has visibility set to private. If you try to access a link like this one, without being logged in:
http://www.example.com/group/my-project/uploads/443568a8641b1b48fc983daea27d36c0/myfile.zip
Then you get redirected to the login page.

Downloading files from the GitLab API is not possible. Here is GitLab's issue to track this feature:
https://gitlab.com/gitlab-org/gitlab/issues/25838
------
As a workaround for now, I am using a python script to simply log in to the page and get the file. This requires the username and password of a user. I set up a new user that only has access to the specific project where it is needed, so it should be safe enough to use in the build script of this project.
import argparse
import sys
import mechanize
def login_and_download(url, user, password):
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
br.select_form(id = 'new_user')
br['user[login]'] = user
br['user[password]'] = password
response = br.submit()
filename = url.split('/')[-1] #Last part of url
with open(filename, 'wb') as f:
f.write(response.get_data())
def main():
parser = argparse.ArgumentParser(description = 'Downloads files from a private git repo')
parser.add_argument('url', help = 'The URL to download')
parser.add_argument('user', help = 'The username to use')
parser.add_argument('password', help = 'The password to use')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
login_and_download(args.url, args.user, args.password)
if __name__ == '__main__':
main()

Here is another example of python script, still from issue 25838.
It translates the curl call:
curl --request GET 'http://127.0.0.1:3000/root/my-project/uploads/d3d527ac296ae1b636434f44c7f57272/IMG_3935.jpg' \
--header 'Authorization: Bearer YOUR_API_TOKEN'
import requests
from bs4 import BeautifulSoup
with requests.Session() as http_session:
# my login page url
login_url = [LOGIN_URL]
login_page = http_session.get(login_url)
login_inputs = BeautifulSoup(login_page.content, features='html.parser').find_all('input')
# get the authenticity token from the login page
for login_input in login_inputs:
if 'token' in login_input['name']:
auth_token = login_input['value']
break
# send some user-agent header just in case
headers = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
# see your login page source for the required data ...
data = {
"username": [MYUSERNAME],
"password": [MYPASSWORD],
"authenticity_token": auth_token,
"remember_me": "1",
"commit": "Sign in"
}
# ... as well as the login endpoint
login_endpoint = [LOGIN_ENDPOINT_URL]
# authenticate
http_session.post(login_endpoint, data=data, headers=headers)
# myfile_url is the url for my wanted file
myfiledata = http_session.get(myfile_url).content

Related

fetch html resources using urllib with authentication

There is one public html webpage where some content is available. With urllib I am able to fetch contents. But there another version where if I am logged in as user, additional content is available. in order to fetch logged-in page I use solution provided on portal and also urllib basic auth
import urllib.request
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib.request.HTTPBasicAuthHandler(passman)
opener = urllib.request.build_opener(authhandler)
urllib.request.install_opener(opener)
res = urllib.request.urlopen(url)
res_body = res.read()
res_body.decode('utf-8')
When I check contents I do not see what is additionally available for logged-in user . I would like to know how to correct this . I am using python 3.
Thanks

How to figure out an Instagram username from the user id

I am working on a program that needs to scrape information from the public Instagram API.
My code is using the endpoint https://instagram.com/{username}/?__a=1 to get information about a user and in order to uniquely identify Instagram accounts even after name changes I store the "id" parameter that always stays the same.
Up to this point, it was possible to use another Instagram API Endpoint https://i.instagram.com/api/v1/users/{id}/info/ to find out the current username connected to a given account but it has been removed by Instagram a few days / weeks ago.
I'd highly appreciate if anyone knows another way of getting a username from a user id since I was not able to find one myself nor find a solution by someone else online.
Ig has changed this endpoint behavior and it's been secured. However, the hint here is the error message
{
"message": "useragent mismatch",
"status": "fail"
}
Passing a valid useragent you can skip this new security-check. Let me share a piece of python code
import requests
def get_user_by_user_id(user_id):
user = {}
if user_id:
base_url = "https://i.instagram.com/api/v1/users/{}/info/"
#valid user-agent
headers = {
'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)'
}
try:
res = requests.get(base_url.format(user_id),headers=headers)
user_info = res.json()
user = user_info.get('user',{})
except Exception as e:
print("getting user failed, due to '{}'".format(e.message))
return user
user_id=<your_user_id>
get_user_by_user_id(user_id) #ta-dah!
There should be some pluggin that allow you change headers in order to get results throught a browser request...

Python3 download document from web

I am new in Python3 and I am trying to download a doc after login to a website.
I have 2 url which can let me instantly login to the page and download the doc. after login which are:
https://www.xxxcompany.com/login.action?loginname=name&password=psw
https://www.xxxcompany.com/doc_download_all.action?ID=37887&edition=PD&Year=2018&Month=10&Day=5&&CLI=&transferNumber=&inOut=C&deviceType=A&minDuration=0&maxDuration=0&sortType=0&sortAsc=1&showAdv=0&viewtype=0&subPage=M&RMID=-1&updateRMID=&updateRecordID=&customField1=
Here is my code. Its definitely not work and it doesn't print me the status code. Did I misunderstand some concept? Please hep me to solve the problem. Thank you so much!
from lxml import html
import webbrowser
import requests
def login():
with requests.session() as s:
# fetch the login page
s.get(url1)
print(s.status_code) #check whether its successfully login
s.get(url2) #download the doc
You need to write data to file.
url = "http://www.xxxx.com/xxx/xxxx/sample.doc"
import requests
with requests.Session() as se:
req = se.get(url)
with open(url.split("/")[-1],"wb") as doc:
doc.write(req.content)

Adding credential(username and password) and fetching URL in python

How can I add credential(username and password) and then fetch the URL of that particular website in python?
I need help to implement this in my project
Example: suppose the first page is the login page and when we provide the credential and validate that and then we are re-directed into the homepage and then pick the url of that homepage
You can use package os
import os
url = os.environ['HTTP_HOST']
uri = os.environ['REQUEST_URI']
return url+uri

Spotipy - Cannot log in to authenticate (Authorization Code Flow)

I am working with the Spotipy Python library to connect to the Spotify web API. I want to get access to my Spotify's user account via Authorization Code Flow. I am using Python 3.5, Spotipy 2.4.4, Google Chrome 55.0.2883.95 (64-bit) and Mac OS Sierra 10.12.2
First, I went to the Spotify Developer website to register the program to get a Client ID, a Client Secret key and enter a redirect URI (https://www.google.com) on my white-list.
Second, I set the environment variables from terminal:
export SPOTIPY_CLIENT_ID='my_spotify_client_id'
export SPOTIPY_CLIENT_SECRET='my_spotify_client_secret'
export SPOTIPY_REDIRECT_URI='https://www.google.com'
Then I try to run the example code typing 'python3.5 script.py my_username' from terminal. Here is the script:
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-read'
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print "Usage: %s username" % (sys.argv[0],)
sys.exit()
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print track['name'] + ' - ' + track['artists'][0]['name']
else:
print "Can't get token for", username
When running this code, it takes me to a log in screen on my browser. I enter my Spotify's credential to grant access to my app. But when I finally click on 'log in' (or 'Iniciar sesiĆ³n' in Spanish) nothing happens. I tried to log in with my Facebook account but it does not work either. It seems that I get a Bad Request every time that I click on 'log in'. Here is a capture of Chrome:
The process is incomplete because when trying to enter the redirect URI back on terminal I get another Bad Request:
raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request
I tried to restart my computer, clean my browser cookies, use another different browser but did not work. It seems that I am not the only one having this problem. Is it maybe a bug? Please, avoid answers like "read the documentation of the API going here". Thank you.
Solved it. In order to run the Authorization Code Flow example code provided in the Spotipy's documentation correctly, I specified the redirect URI in line 13 of the example script when calling util.prompt_for_user_token, even when I had done this previously when setting environment variables:
token = util.prompt_for_user_token(username, scope, redirect_uri = 'https://example.com/callback/')
Likewise, do not use https://www.google.com or similar web address as your redirect URI. Instead, try 'https://example.com/callback/' or 'http://localhost/' as suggested here. Do not forget that the redirected URL once you are logged in must have the word code included.
Cheers,

Resources